Tuesday, July 11, 2006

Injecting into domain objects

As a proponent that a model should be rich and expressive I struggle in most project to avoid the anemic domain model anti-pattern. The post will use Eric Evans domain driven design terminology.

I've sometimes participate in DDD forum and the some of the most recurring question is whether using Repositories (aka DAO but from a model POV) inside Entites is a good thing. My belief is that most of the time it is a necessary thing if you want to avoid using services for everything but the simplest of operations inside your model. So if your are using an ORM and Spring (defacto web app architecture nowadays) the problem arises on how to inject the repository implementation (the DAO implementation) inside the instantiated objects hibernate or another ORM is doing the instantiation instead of Spring. I will not detail on how to do this but just point to 2 blog entries that explain this better than I do.

If you are using the newest Spring 2.0 this issue is actually addressed and is explained here and here. There are some things to note if you're using hibernate.
If you are using an older version on Spring you have other options which are discussed here

Hope it helps someone out there. I know it will help me again to have the information readily available here :)

No comments: