Is this Better than Constructor Injection?
August 29, 2008
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.
Posted in

content rss

Add New Comment
Viewing 6 Comments
Thanks. Your comment is awaiting approval by a moderator.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Do you already have an account? Log in and claim this comment.
Add New Comment
Trackbacks