Last active
December 16, 2019 09:15
-
-
Save mikehadlow/474172cbf6452bce91e3 to your computer and use it in GitHub Desktop.
C#: Only Use Static Methods
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Thing : IThing | |
{ | |
public Thing(IDependency dependency) { } | |
public void Do(string arg) { } | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void DoThing(IDependency dependency, string arg) { } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static Action Compose() | |
{ | |
return () => RunCustomerReportBatch( | |
GetCustomersForCustomerReport, | |
CreateCustomerReport, | |
SendEmail); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface ICustomerData | |
{ | |
IEnumerable<Customer> GetCustomersForCustomerReport(); | |
} | |
public interface IReportBuilder | |
{ | |
Report CreateCustomerReport(Customer customer); | |
} | |
public interface IEmailer | |
{ | |
void Send(string toAddress, string body); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static ReportingService Compose() | |
{ | |
return new ReportingService( | |
new CustomerData(), | |
new ReportBuilder(), | |
new Emailer() | |
); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Fact] | |
public void RunCustomerReportBatchShouldSendReports() | |
{ | |
// 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)); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var dependency = new ThingThatImplementsIDependency(); | |
// RelyOnThing is something that that takes Action<string> as a dependency. | |
Action relyOnThing = () => RelyOnThing(arg => DoThing(dependency, arg)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, line 3 of ThingComposition.csx contains a small typo:
that that
. :-)