Jacek Laskowski 57 Posted October 22, 2019 I now do the injection by the constructor, but this causes some constructors to be very complex. Is injecting by [inject] attribute directly into the object field a good practice? Is it better not to do it? Share this post Link to post
Stefan Glienke 2002 Posted October 22, 2019 (edited) No, it's not a good practice - write your classes in a way that you could do pure DI. If you do field injection that code can't run without a DI container or some other ways to do such injection. If you have too many ctor arguments you are suffering from so called constructor over-injection and there is a way to solve this: refactor your code and respect the single responsibility principle - see https://blog.ploeh.dk/2010/02/02/RefactoringtoAggregateServices/ Always think of a DI container as something like an automated assembling plant - all the screws and pieces that are put together there by robots don't know of the assembling plant. You can as well grab a screwdriver or some other tool yourself and put the pieces together manually with a lot of work and sweat but it's possible. Edited October 22, 2019 by Stefan Glienke 3 Share this post Link to post