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
[TestFixture] | |
public class PrintInvoiceCommandTests | |
{ | |
private PrintInvoiceCommand _command; | |
private AutoMoqer _mocker; | |
private Invoice _invoice; | |
private const int InvoiceId = 1; | |
private const string UserName = "mrenze"; |
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
// The solution is to refactor the GOF class to be instance class and let it use interface to make it easy to inject | |
// Then use the IOC/DI singleton way to ensue using only one instance of the object. | |
// The Interface | |
public interface ISecurity | |
{ | |
string GetUserName(); | |
bool IsAdmin(); | |
} |
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
// This class will be used to create a simple GOF singletone object. | |
public class Security | |
{ | |
private static Security _instance; | |
private string _userName; | |
private bool _isAdmin; | |
public static Security GetInstance() | |
{ |
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
[TestFixture] | |
public class PrintInvoiceCommandTests | |
{ | |
private PrintInvoiceCommand _command; | |
private AutoMoqer _mocker; | |
private Invoice _invoice; | |
private const int InvoiceId = 1; | |
private const string UserName = "mrenze"; |
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
// We solving the first issue by injected the real dependencies that the class depend on directly, and not injecting the whole | |
// ServiceLocator. | |
// And we solving the 2nd issue by implementing an IdentityService that we depend on to get the UserName directly. | |
public class PrintInvoiceCommand | |
{ | |
private readonly IDatabase _database; | |
private readonly IInvoiceWriter _writer; | |
private readonly IIdentityService _identity; |
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
// The Code which have issues before refactoring | |
// First: issue is passing the whole container which known as service locator pattern (Anti-Pattern) because it will | |
// let us lost the value of mocking the real dependencies that the class is depend on and also the Auto Moqing with a tool like AutoMoqer | |
// will be no longer available. | |
// Second: There is an issue in LastPrintedBy as been shown below we violating the the law of demeter by calling object that we depend on | |
// then from it we call another object then another which made the testing difficult specially if those classes has no interface to implement which | |
// may let us swap the real objects with mocks easly. | |
public class PrintInvoiceCommand | |
{ |
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
[TestFixture] | |
public class InvoiceWriterTests | |
{ | |
private InvoiceWriter _writer; | |
private AutoMoqer _mocker; | |
private Invoice _invoice; | |
[SetUp] | |
public void SetUp() | |
{ |
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 interface IInvoiceWriter | |
{ | |
void Write(Invoice invoice); | |
} | |
public class InvoiceWriter : IInvoiceWriter | |
{ | |
private readonly IPrinter _printer; | |
private readonly IPageLayout _layout; |
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
// PrintInvoiceCommand Class | |
public class PrintInvoiceCommand | |
{ | |
private readonly IDatabase _database; | |
private readonly IPrinter _printer; | |
public PrintInvoiceCommand( | |
IDatabase database, |
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
[TestFixture] | |
public class PrintInvoiceCommandTestsWithAutoMoqer | |
{ | |
private PrintInvoiceCommand _command; | |
private AutoMoqer _mocker; | |
private Invoice _invoice; | |
private const int InvoiceId = 1; | |
private const decimal Total = 4.50m; | |
private static readonly DateTime Date = new DateTime(2001, 2, 3); |
NewerOlder