I use property injection only for dependencies that are not required for the proper working of the class but could add some functionality if the user sets them. Example of such functionality is logging. So you could have a property which represents a logger where the user can supply his own implementation and if he doesn't the class continues to work normally but it simply doesn't log.
For everything else I use constructor injection. This way you indicate to the consumer that this class has a required dependency on some other service. So to answer your question about the property injection I would simply have: public SomeClass { public IUserService UserService { get; set; } public void SomeMethodWhichDoesntEnforceUserService() { if (UserService!
= null) { // Provide some additional functionality } } } and if your class cannot function properly without the user service: public SomeClass { private readonly IUserService _userService; public SomeClass(IUserService userService) { _userService = userService; } public void SomeMethodWhichRequiresTheService() { _userService.DoSomething(); } } So in both cases no reference to any DI specifics. That's what Inversion of Control is all about.
And what do you do when BaseClass has for example 3 interfaces injected in constructor and InheritedClass has additional 2. Do you make constructor with 5 parameters? – LukLed Feb 16 at 18:03 @LukLed, yes, but in general I try to reduce the number of dependencies.5 seems pretty large.It means that your class is doing too much things and you should probably consider splitting it into different classes.
– Darin Dimitrov Feb 16 at 18:05 Too much? IPrincipal is injected, IRepository is injected, IApplicationCache is injected in BaseClass. These are basic items, used in all services.
I just try to inject as much, as I can, instead of referencing directly. That makes a lot of parameters in constructor. Ok, I can handle it with aggregate services.
– LukLed Feb 16 at 18:13.
It may indicate an issue with the design. To avoid direct reference to the DependencyResolver you could implement some form of abstraction of a Service Locator over the DI framework, e.g. CommonServiceLocator, but as the answer to this question indicates, such abstractions shouldn't be necessary when doing DI correctly. Instead you should adjust the design of the application.
DependencyResolver is some kind of abstraction over DI library. This class is not related to Ninject. I am using Inject attribute in base classes, because I don't want to add parameters to constructor of every inheriting class.
I also had some circular reference issues. – LukLed Feb 16 at 17:56 Ah apologies, I'm not too familiar with ninject but automatic constructor injection isn't an option if you're concerned with polluting code with ninject references? Kohari.Org/2008/06/08/… – ppejovic Feb 16 at 18:06 Constructor injection is an option, but I don't want to have huge constructors.
Ok. I probably do too much thinking, when there is not much to think about. – LukLed Feb 16 at 18:34.
I believe the ninject.web. Mvc version for mvc3 now supports constructor injection on filter attributes. Have you tried it?
If you don't specify all required constructor parameters, code won't even compile. – LukLed Feb 16 at 18:27 @LukLed I haven't had the time to try it but it looks like it's possible...github.Com/ninject/ninject.web. Mvc/tree/master/mvc3/src/… seems like he's using a normal attribute as a marker to inject the filterattribute.
– dotjoe Feb 16 at 22:48 the complicated shit you have to go through to do DI on a filter attribute.... :| – Shawn Mclean Jun 2 at 18:57.
I use property injection only for dependencies that are not required for the proper working of the class but could add some functionality if the user sets them. Example of such functionality is logging. So you could have a property which represents a logger where the user can supply his own implementation and if he doesn't the class continues to work normally but it simply doesn't log.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.