On my previous post, I explained about the value of pushing as much as possible to the infrastructure, and then show some code that showed how to do so. First, let us look at the business level code:
[AcceptVerbs(HttpVerbs.Post)] public ActionResult Register(string originUnlocode, string destinationUnlocode, DateTime arrivalDeadline) { var trackingId = ExecuteCommand(new RegisterCargo { OriginCode = originUnlocode, DestinationCode = destinationUnlocode, ArrivalDeadline = arrivalDeadline }); return RedirectToAction(ShowActionName, new RouteValueDictionary(new { trackingId })); } public class RegisterCargo : Command<string> { public override void Execute() { var origin = Session.Load<Location>(OriginCode); var destination = Session.Load<Location>(DestinationCode); var trackingId = Query(new NextTrackingIdQuery()); var routeSpecification = new RouteSpecification(origin, destination, ArrivalDeadline); var cargo = new Cargo(trackingId, routeSpecification); Session.Save(cargo); Result = trackingId; } public string OriginCode { get; set; } public string DestinationCode { get; set; } public DateTime ArrivalDeadline { get; set; } }
And the infrastructure code, now:
protected void Default_ExecuteCommand(Command cmd) { cmd.Session = Session; cmd.Execute(); } protected TResult Default_ExecuteCommand<TResult>(Command<TResult> cmd) { ExecuteCommand((Command) cmd); return cmd.Result; }
You might have noticed a problem in the way we are named things, the names on the action and the infrastructure code do not match. What is going on?
Well, the answer is quite simple. Let us look at how our controller looks like ( at least, the important parts ):
public class AbstractController : Controller { public ISession Session; public Action<Command> AlternativeExecuteCommand { get; set; } public Func<Command, object> AlternativeExecuteCommandWithResult { get; set; } public void ExecuteCommand(Command cmd) { if (AlternativeExecuteCommand!= null) AlternativeExecuteCommand(cmd); else Default_ExecuteCommand(cmd); } public TResult ExecuteCommand<TResult>(Command<TResult> cmd) { if (AlternativeExecuteCommandWithResult != null) return (TResult)AlternativeExecuteCommandWithResult(cmd); return Default_ExecuteCommand(cmd); } protected void Default_ExecuteCommand(Command cmd) { cmd.Session = Session; cmd.Execute(); } protected TResult Default_ExecuteCommand<TResult>(Command<TResult> cmd) { ExecuteCommand((Command)cmd); return cmd.Result; } }
What?! You do mocking by hand and inject them like that? That is horrible! It is much easier to use a mocking framework and ….
Yes, it would be, if I was trying to mocking different things all the time. But given that I have very few abstractions, it make sense to not only build this sort of infrastructure, but to also build infrastructure for those things _in the tests_.
For example, let us write the test for the action:
[Fact] public void WillRegisterCargo() { ExecuteAction<CargoAdminController>( c=> c.Register("US", "UK", DateTime.Today) ); Assert.IsType<RegisterCargo>( this.ExecutedCommands[0] ); }
The ExecuteAction method belongs to the test infrastructure, and it setups the controller to be run under the test scenario. Which allows me to not execute the command, but to actually get it.
From there, it is very easy to get to things like:
[Fact] public void WillCreateNewCargoWithNewTrackingId() { SetupQueryResponse<NextTrackingIdQuery>("abc"); ExecuteCommand<RegisterCargo>( new RegisterCargo { OriginCode = "US", DestinationCode= "UK", ArrivalDeadline = DateTime.Today }); var cargo = Session.Load<Cargo>("cargos/1"); Assert.Equal("abc", cargo.TrackingId); }
This is important, because now what you are testing is the actual interaction. You don’t care about any of the actual dependencies, we just abstracted them out, but without creating ton of interfaces, abstractions on top of abstractions or any of that.
In fact, we kept the number of abstractions to a minimum, and we can change pretty much every part of the system with very little fear of cascading change.
We have similar lego pieces, all of them move together and interact with one another with complete freedom, and we don’t have to have a Abstract Factory Factory Façade Factory.