While reading a book, I came across DI(dependency Injector) and the subsequent DI Container tool. Previously, I developed an application following a tutorial on asp.net website which never used such tool. So, my question can be summed-up in following two concerns :-
- How often do you use DI Container ?
- What requirements make you do so ?
EDIT : Examples with and without DI Container. I have written the codes to understand which is better approach.
Without DI Container --
LinqValueCalculator lc = new LinqValueCalculator();
ShoppingCart sc = new ShoppingCart(lc);
decimal total = sc.CalculateStockValue();
Console.WriteLine("Total: {0:c}", total);
With DI Container -- (Ninject is used in this example)
IKernel ninjectKernel = new StandardKernel();
ninjectKernel.Bind<IValueCalculator>().To<LinqValueCalculator>();
IValueCalculator calcImpl = ninjectKernel.Get<IValueCalculator>();
ShoppingCart cart = new ShoppingCart(calcImpl);
Console.WriteLine("Total: {0:c}", cart.CalculateStockValue());
I will be honest, I feel that writing first code was easier and seemed natural. But, your views are what counts as i am just learning the MVC.
3 Answers 3
How often do I use a DI Container?
Very often. Close to always.
What requirements make me do so?
In ASP.NET MVC I always use a container, because when one uses Constructor Injection in Controllers one breaks the default convention of having default constructors. This means that a custom IControllerFactory is required, and while it's possible to write and maintain one by hand, it's more work. Using a DI Container that supports convention-based configuration, one can use Constructor Injection in a convention-based manner and less maintenance is required.
Can we do without them?
Yes, but at the cost of more maintenance of infrastructure code: https://stackoverflow.com/questions/5667801/arguments-against-inversion-of-control-containers/5668093#5668093
Can we do without them?
Yes, but you lose the ability to easily unit test your application. Mocking the different layers/tiers of your application are much easier when you use DI.
How often do I use them?
Always in an MVC app. StructureMap (for me) has made it so easy to easily set up default conventions that I really don't lose any development time for the gains that it gets me.
BTW, I do DI like this, using Constructor Injection (make unit testing easier):
public class HomeController : Controller
{
private readonly IService _service;
public HomeController(IService service)
{
_service = service;
}
public ActionResult Index()
{
return View(_service.Method());
}
}
I used to write C# asp.net application with all these best practices previously. DI is very usefull for C# language, that is out of the question. With proper design of your classes and interfaces you can make it less coupled and test / develop easier.
Year ago I switched to dynamic languages like ruby and python. My first idea was to find a DI libraries for them, but then I gave up cause I 've realized that DI is a perfect pattern for solving static language problems.