14

My current project is leveraging Spring, and our architect has decided to let Spring manage Services, Repositories and Factory objects, but NOT domain objects. We are closely following domain driven design. The reasoning behind not using spring for domain objects is primarily that spring only allows static dependency injection. What i mean by static dependency injection is that dependencies are specified inside xml configuration and they get "frozen".

I maybe wrong, but my current understanding is that even though my domain only leverages interfaces to communicate with objects, but spring's xml configuration forces me to specify a concrete dependency. hence all the concrete dependencies have to be resolved at deployment time. Sometimes, this is not feasible. Most of our usecases are based on injecting a particular type based on the runtime data or a message received from an end user.

Most of our design is following command pattern. hence, when we recieve a command, we would like to construct our domain model and based on data received from a command, we inject particular set of types into our aggregate root object. Hence, due to lack of spring's ability to construct a domain model based on runtime data, we are forced to use static factory methods, builders and Factory patterns.

Can someone please advise if spring has a problem to the above scenario ?

I could use AOP to inject dependencies, but then i am not leveraging spring's infrastructure.

skaffman
  • 398,947
  • 96
  • 818
  • 769
Jimm
  • 8,165
  • 16
  • 69
  • 118

3 Answers3

10

I suggest you read the section in the Spring docs concerning Using AspectJ to dependency inject domain objects with Spring.

It's interesting that you said "I could use AOP to inject dependencies, but then i am not leveraging spring's infrastructure, " considering that AOP is a core part of Spring's infrastructure. The two go very well together.

The above link allows you to have Spring's AOP transparently inject dependencies into domain objects that are creating without direct reference to the Spring infrastructure (e.g. using the new operator). It's very clever, but does require some deep-level classloading tinkering.

skaffman
  • 398,947
  • 96
  • 818
  • 769
  • +1. Depending on complexity of this particular scenario, however, it may be feasible (and easier) to use FactoryBeans instead of AOP. – ChssPly76 Sep 17 '09 at 17:10
  • I agree, I normally wouldn't bother. However, if the application is hard-core DDD, then factories may not be desirable. – skaffman Sep 17 '09 at 17:17
6

Spring's dependency injection/configuration is only meant for configuring low-level technical infrastructure, such as data sources, transaction management, remoting, servlet mount-points and so forth.

You use spring to route between technical APIs and your services, and inside those services you just write normal Java code. Keeping Spring away from your domain model and service implementations is a good thing. For a start , you don't want to tie your application's business logic to one framework or let low-level technical issues "leak" into your application domain model. Java code is much easier to modify in the IDE than spring's XML config, so keeping business logic in java let's you deliver new features more rapidly and maintain the application more easily. Java is much more expressive than spring's XML format so you can more clearly model domain concepts if you stick to plain Java.

Nat
  • 9,820
  • 3
  • 31
  • 33
  • just because Spring CAN do a lot of things doesn't mean the right answer is to make your app use Spring for all of those things. – M1EK Sep 17 '09 at 18:13
  • While this is true for the standard anemic domain model, IMO it's alright to do these things in a domain driven design (which the OP says he's using). – Alex Beardsley Sep 22 '09 at 20:19
  • This answer applies very much (probably more so) to non-anaemic domain models that have a clean object-oriented design and implement domain logic as methods on the domain-model objects. And anaemic domain models are very much not standard. They are something to be *avoided*. – Nat Sep 23 '09 at 09:10
2

Spring's dependency injection (and dependency injection in general) is basically for wiring together Services, Repositories and Factories, etc. It's not supposed to directly handle things that need to be done dynamically in response to commands, etc., which includes most stuff with domain objects. Instead, it provides control over how those things are done by allowing you to wire in the objects you want to use to do them.

ColinD
  • 108,630
  • 30
  • 201
  • 202
  • 2
    That's selling Spring a bit short. It is capable of these things, it just takes a bit more effort. – skaffman Sep 17 '09 at 16:44