Skip to content

Instantly share code, notes, and snippets.

@marzoukali
Created July 3, 2017 10:59
Show Gist options
  • Save marzoukali/ed5a42aa4c1181dcf3fe8ae61943ecca to your computer and use it in GitHub Desktop.
Save marzoukali/ed5a42aa4c1181dcf3fe8ae61943ecca to your computer and use it in GitHub Desktop.
unittest-example-8.cs
// 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;
public PrintInvoiceCommand(
IDatabase database,
IInvoiceWriter writer,
IIdentityService identity)
{
_database = database;
_writer = writer;
_identity = identity;
}
public void Execute(int invoiceId)
{
var invoice = _database.GetInvoice(invoiceId);
_writer.Write(invoice);
invoice.LastPrintedBy = _identity.GetUserName();
_database.Save();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment