Domain Driven Design Approach without ORM

domain-driven-design

Looking into incorporating ideas of Domain Driven Design into an application however our data model is not really a good fit for ORM like Hibernate. Very old database / data model, lots of compound / natural keys, lack of foreign keys, etc.

The item I am struggling with is how to design the domain layer without ORM since ORM would handle fetching items lazy or eager depending on how mappings are configured. I don't see how to avoid that without having DI framework (Spring in my case) inject the repository into the Domain objects, or have the repository fully initialize the domain object, which in itself may be overly wasteful.

Best Answer

The item I am struggling with is how to design the domain layer without ORM since ORM would handle fetching items lazy or eager depending on how mappings are configured.

Typically, the domain components operate on in memory state, without integrating with the ports and adapters in your system. Think onion architecture.

A second important idea is to make sure that the domain model is as explicit as possible about the information that is needed. This is usually done with the abstraction of a repository, but one where the contract is specific to the domain use case. Udi Dahan (2007) touched on the idea.

So the domain asks the repository for the absolute minimum that it needs to satisfy its own requirements, and the implementation under the covers figures out how to retrieve the necessary state and from it create an in memory model of that state.

Yes, in the pathological case, you might end up with a lot of repository implementations that carve specific data out of Oracle. It happens.

DI framework really doesn't figure into it unless you are intending to run the domain model in a lot of different environments; you just need repository discovery, which is to say a factory that provides the correct implementation of the repository on demand (aka the "repository" repository).