views:

262

answers:

2

Is it a good idea to not inject Domain Models in Spring. It saves some XML, and what is the use of injecting domain model anyway.

For services and DAO, I want to decouple so I use DI, but Domain Model usually flows all the way from Web to DAO - sort of a vertical layer running through the horizontals.

What do you think? Good or bad? Are there any other recommended best practices to sensibly use Spring instead of adding to XML lines?

I was recommended to use factory pattern at service layer, but then that beats the whole idea behind a DI framework, doesn't it? Factory pattern is a good idea, but that would result in boiler plate code, so it then becomes boiler plate code Vs XML.

Any suggestions on sensibly using Spring on other areas welcome!!

+1  A: 

Well, you should not inject concrete model objects. this is not what DI is about. What you might do is inject a factory for model objects as someone told you. This way you can mock the "important" parts that you don't want to test now or you can switch implementations.

DI makes sense for external dependencies like database, services, ...

Using DI to "create" model objects is possible but IMHO bad.

Patrick Cornelissen
+1  A: 

See this question's top answer:

http://stackoverflow.com/questions/1304245/spring-and-the-anemic-domain-model

It (and especially the article it cites) gives some really interesting ways to do what you want without a factory using things like AOP or Hibernate Interceptors.

Alex Beardsley