Limiting abstractions: The key is in the infrastructure
Join the DZone community and get the full member experience.
Join For Freein my previous post, i discussed actual refactoring to reduce abstraction, and i showed two very interesting methods, query() and executecommand(). here is the code in question:
[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; }
}
what are they so important? mostly because those methods [and similar, like raise(event) and executelater(task)] are actually the back bone of the application. they are the infrastructure on top of which everything rests.
those methods basically accept an argument (and optionally return a value). their responsibility are:
- setup the given argument so it can run.
- execute it.
- return the result (if there is one).
here is an example showing how to implement executecommand:
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;
}
i have code very much like that in production , because i know that in this system, there are actually only one or two dependencies that a command may want.
there are very few other dependencies, because of the limited number of abstractions that we have. this makes things very simple to write and work with.
because we abstract away any dependency management, and because we allow only very small number of abstractions, this works very well. the amount of complexity that you have is way down, code reviewing this is very easy, because there isn’t much to review, and it all follows the same structure. the implementation of the rest are pretty much the same thing.
there is just one thing left to discuss, because it kept showing up on the comments for the other posts. how do you handle testing?
Published at DZone with permission of Oren Eini, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments