LTE Mars Rover - Designing Logger
This file contains 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 ItemRepository | |
{ | |
private readonly string _connectionString; | |
// By adding the dependency here, there's no way you that | |
// an ItemRepository could be created without specifying | |
// a connection string. | |
public ItemRepository(string connectionString) | |
{ | |
_connectionString = connectionString; | |
} | |
} | |
// Example usage | |
// If the connectionString isn't provided, then | |
// the code doesn't compile! | |
var itemRepository = new ItemRepository("Data Source=localhost;Initial Catalog=AdventureWorks;Integrated Security=True") |
This file contains 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 PurchaseWorkflow | |
{ | |
// By adding the NotificationStrategy here, | |
// there's no way that a developer can call | |
// the Complete method without specifying | |
// a way to notify the customer. | |
public void Complete(Order order, INotificationStrategy strategy) | |
{ | |
string receiptDetails = CreateReceipt(order); | |
strategy.SendNotification(order.Customer, receiptDetails); | |
} | |
} | |
// Example Usage | |
var order = new Order("Cameron", 22.50); | |
var workflow = new PurchaseWorkflow(); | |
// If a NotificationStrategy isn't passed in, this code won't compile. | |
workflow.Complete(order, new TextNotificationStrategy()); |
This file contains 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 PurchaseWorkflow | |
{ | |
// By having the notification be set as a property, | |
// developer can set this dependency once and then | |
// only change it when needed. | |
// However, there's nothing forcing a developer to | |
// set this property. | |
public INotificationStrategy NotificationStrategy {get; set;} | |
public void Complete(Order order) | |
{ | |
string receiptDetails = CreateReceipt(order); | |
NotificationStrategy.SendNotification(order.Customer, receiptDetails); | |
} | |
} | |
// The Mistake | |
var order = new Order("Cameron", 22.50); | |
var workflow = new PurchaseWorkflow(); | |
workflow.Complete(order); // This will cause a NullReferenceException to be thrown | |
// Proper Usage | |
var order = new Order("Cameron", 22.50); | |
var workflow = new PurchaseWorkflow(); | |
// As long as you remember to set this property, things should work! | |
workflow.NotificationStrategy = new TextNotificationStrategy(); | |
workflow.Complete(order); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment