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.