Skip to content

Instantly share code, notes, and snippets.

@nafg
Forked from mikehadlow/ClassWithDependency.csx
Last active August 29, 2015 14:27
Show Gist options
  • Select an option

  • Save nafg/f998409f9ef263d5e085 to your computer and use it in GitHub Desktop.

Select an option

Save nafg/f998409f9ef263d5e085 to your computer and use it in GitHub Desktop.

Revisions

  1. @mikehadlow mikehadlow revised this gist Aug 7, 2015. 1 changed file with 29 additions and 0 deletions.
    29 changes: 29 additions & 0 deletions FunctionalUnitTest.csx
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    [Fact]
    public void FunctionalTest()
    {
    // arrange
    var expectedCustomer = new Customer("[email protected]");
    var expectedReportBody = "the report body";

    Func<IEnumerable<Customer>> getCustomersForCustomerReport =
    () => new[] {expectedCustomer};

    Func<Customer, Report> createCustomerReport =
    customer => new Report(expectedCustomer.Email, expectedReportBody);

    var actualToAddress = "";
    var actualBody = "";

    Action<string, string> sendEmail = (toAddress, body) =>
    {
    actualToAddress = toAddress;
    actualBody = body;
    };

    // act
    Functional.RunCustomerReportBatch(getCustomersForCustomerReport, createCustomerReport, sendEmail);

    // assert
    Assert.Equal(expectedCustomer.Email, actualToAddress);
    Assert.Equal(expectedReportBody, actualBody);
    }
  2. @mikehadlow mikehadlow revised this gist Aug 7, 2015. 1 changed file with 7 additions and 0 deletions.
    7 changes: 7 additions & 0 deletions FunctionalComposition.csx
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    public static Action Compose()
    {
    return () => RunCustomerReportBatch(
    GetCustomersForCustomerReport,
    CreateCustomerReport,
    SendEmail);
    }
  3. @mikehadlow mikehadlow revised this gist Aug 7, 2015. 1 changed file with 13 additions and 0 deletions.
    13 changes: 13 additions & 0 deletions RunCustomerReportsBatch.csx
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    public static void RunCustomerReportBatch(
    Func<IEnumerable<Customer>> getCustomersForCustomerReport,
    Func<Customer, Report> createCustomerReport,
    Action<string, string> sendEmail)
    {
    var customers = getCustomersForCustomerReport();

    foreach (var customer in customers)
    {
    var report = createCustomerReport(customer);
    sendEmail(report.ToAddress, report.Body);
    }
    }
  4. @mikehadlow mikehadlow revised this gist Aug 7, 2015. 1 changed file with 18 additions and 0 deletions.
    18 changes: 18 additions & 0 deletions StaticMethodsReplaceClasses.csx
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    public static IEnumerable<Customer> GetCustomersForCustomerReport()
    {
    // pretend to do data access
    yield return new Customer("[email protected]");
    yield return new Customer("[email protected]");
    yield return new Customer("[email protected]");
    }

    public static Report CreateCustomerReport(Customer customer)
    {
    return new Report(customer.Email, $"This is the report for {customer.Email}!");
    }

    public static void SendEmail(string toAddress, string body)
    {
    // pretend to send an email here
    Console.Out.WriteLine("Sent Email to: {0}, Body: '{1}'", toAddress, body);
    }
  5. @mikehadlow mikehadlow revised this gist Aug 7, 2015. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions ThingComposition.csx
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    var dependency = new ThingThatImplementsIDependency();

    // RelyOnThing is something that that takes Action<string> as a dependency.
    Action relyOnThing = () => RelyOnThing(arg => DoThing(dependency, arg));
  6. @mikehadlow mikehadlow revised this gist Aug 7, 2015. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions DoThing.csx
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    public static void DoThing(IDependency dependency, string arg) { }
  7. @mikehadlow mikehadlow revised this gist Aug 7, 2015. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions ClassWithDependency.csx
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    public class Thing : IThing
    {
    public Thing(IDependency dependency) { }

    public void Do(string arg) { }
    }
  8. @mikehadlow mikehadlow renamed this gist Aug 4, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.txt → ObjectOrientedTest.csx
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    [Fact]
    public void ObjectOrientedTest()
    public void RunCustomerReportBatchShouldSendReports()
    {
    // Arrange
    var customerDataMock = new Mock<ICustomerData>();
  9. @mikehadlow mikehadlow revised this gist Aug 4, 2015. 1 changed file with 28 additions and 0 deletions.
    28 changes: 28 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    [Fact]
    public void ObjectOrientedTest()
    {
    // Arrange
    var customerDataMock = new Mock<ICustomerData>();
    var reportBuilderMock = new Mock<IReportBuilder>();
    var emailerMock = new Mock<IEmailer>();

    var expectedCustomer = new Customer("[email protected]");
    var expectedReportBody = "the report body";

    customerDataMock.Setup(x => x.GetCustomersForCustomerReport())
    .Returns(new[] { expectedCustomer });

    reportBuilderMock.Setup(x => x.CreateCustomerReport(expectedCustomer))
    .Returns(new Report(expectedCustomer.Email, expectedReportBody));

    var sut = new ReportingService(
    customerDataMock.Object,
    reportBuilderMock.Object,
    emailerMock.Object);

    // Act
    sut.RunCustomerReportBatch();

    // Assert
    emailerMock.Verify(x => x.Send(expectedCustomer.Email, expectedReportBody));
    }
  10. @mikehadlow mikehadlow revised this gist Aug 4, 2015. 1 changed file with 8 additions and 0 deletions.
    8 changes: 8 additions & 0 deletions ObjectOrientedComposition.csx
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    public static ReportingService Compose()
    {
    return new ReportingService(
    new CustomerData(),
    new ReportBuilder(),
    new Emailer()
    );
    }
  11. @mikehadlow mikehadlow revised this gist Aug 4, 2015. 1 changed file with 24 additions and 0 deletions.
    24 changes: 24 additions & 0 deletions ReportingService.csx
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    public class ReportingService
    {
    public ReportingService(ICustomerData customerData, IReportBuilder reportBuilder, IEmailer emailer)
    {
    CustomerData = customerData;
    ReportBuilder = reportBuilder;
    Emailer = emailer;
    }

    public ICustomerData CustomerData { get; private set; }
    public IReportBuilder ReportBuilder { get; private set; }
    public IEmailer Emailer { get; private set; }

    public void RunCustomerReportBatch()
    {
    var customers = CustomerData.GetCustomersForCustomerReport();

    foreach (var customer in customers)
    {
    var report = ReportBuilder.CreateCustomerReport(customer);
    Emailer.Send(report.ToAddress, report.Body);
    }
    }
    }
  12. @mikehadlow mikehadlow revised this gist Aug 4, 2015. 1 changed file with 27 additions and 0 deletions.
    27 changes: 27 additions & 0 deletions Implementations.csx
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    public class CustomerData : ICustomerData
    {
    public IEnumerable<Customer> GetCustomersForCustomerReport()
    {
    // pretend to do data access
    yield return new Customer("[email protected]");
    yield return new Customer("[email protected]");
    yield return new Customer("[email protected]");
    }
    }

    public class ReportBuilder : IReportBuilder
    {
    public Report CreateCustomerReport(Customer customer)
    {
    return new Report(customer.Email, $"This is the report for {customer.Email}!");
    }
    }

    public class Emailer : IEmailer
    {
    public void Send(string toAddress, string body)
    {
    // pretend to send an email here
    Console.Out.WriteLine("Sent Email to: {0}, Body: '{1}'", toAddress, body);
    }
    }
  13. @mikehadlow mikehadlow created this gist Aug 4, 2015.
    14 changes: 14 additions & 0 deletions Interfaces.csx
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    public interface ICustomerData
    {
    IEnumerable<Customer> GetCustomersForCustomerReport();
    }

    public interface IReportBuilder
    {
    Report CreateCustomerReport(Customer customer);
    }

    public interface IEmailer
    {
    void Send(string toAddress, string body);
    }