Proper Component Isolation with DIP

I think that anyone can agree that good code is S.O.L.I.D. code, and Dependency Inversion Principle (DIP) is a very important one.

The principle states that:
A. High-level modules should not depend on low-level modules. Both should depend
on abstractions.
B. Abstractions should not depend upon details. Details should depend upon
abstractions.

DIP is very useful when you want to isolate a more abstract type from it’s dependencies (details) by usage of interfaces which receive a concrete type via Dependency Injection pattern. So instead of having a direct dependency like this…

directdependency

…you put an interface between the two components like this:

withinterface

Noting too special until now but what about the physical location of the interface?

Continue reading

Unit Testing Made Easy with Custom Model Binders

In ASP.NET MVC 3, Microsoft has made unit testing controller actions quite easy. Generally you follow 3 basic steps:

  • setup your dependencies
  • call the controller action you want to test
  • assert something on the properties(usually the Model property) of the returned ActionResult derived object.

However there still is a pain point when the action under test makes use of so called ASP.NET intrinsic objects (Request, Response, Session, Cache, Server etc.) which are hold in the HttpContext instance.

Continue reading