Is this Better than Constructor Injection?
So after I finished my post on property injection I thought of something, maybe I shouldn’t use constructor injection for my classes. Maybe, I can just use lazy properties that would call my IoC (Ninject) Kernel to instantiate the object. So instead of defining a constructor like this:
public IAccountService Service { get; set; } [Inject] public AccountController(IAccountService service) { Service = service; }
I could just have a property Service do the work like this:
public IAccountService Service { get { return (IAccountService) Kernel.Get(typeof (IAccountService)); } }
If I get rid of all my constructor parameters then I won’t need to change my ControllerFactory to use the Ninject Factory. This keeps looking better and better.
What do you think? I keep reading everywhere that construction injection is the way to go. But why complicate my constructor when I can have the properties do the heavy lifting? I would love to hear what you think.








Fri, Aug 29, 2008
ASP.NET MVC