Jump to content
Jacek Laskowski

Spring4D, DI and attribute inject

Recommended Posts

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

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 by Stefan Glienke
  • Like 3

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×