Alexander Petrov
Alexander Petrov

Reputation: 9492

Spring Data and DDD - domain model agnostic of persistence

Hello having separate persistence model and domain model agnostic of persistence is a common pattern. I would like to understand how is this pattern addressed in Spring Data.

The way I imagine it, if we were using regular hibernate without spring data, is that we will have repositories that will work with domain objects that will be internaly mapped to the ORM model and then the persistence will be triggered.

How can this be achieved using Spring Data and if it is not achievable what alternative mechanisms can be used in such way that we have Domain model that is agnostic of the persistence? Thanks.

Upvotes: 3

Views: 1718

Answers (2)

Jens Schauder
Jens Schauder

Reputation: 81882

Spring Data gets you 95% of the way, but it does make some compromises.

Spring Data offers Repositories which are independent of the underlying persistence technology. You can tell because they are contained in Spring Data Commons the base module all other modules are built on top. An example is the CrudRepository. So basic crud operations are covered.

Also, query derivation does not leak information about the persistence store.

Since all you do is specify these interfaces you get almost what you want, because Spring Data is creating the actual implementation.

But there are limitations to that.

  1. Your Entities typically need annotations. These are store dependent. Many people consider that an acceptable compromise since the annotations don't actually contain store dependent code, they are just saying "Hey if you happen to use this with the <persistence technology x> map it in the following way ...".

  2. More complex queries are often implemented by putting @Query annotations on repository methods. These are persistent store dependent. If you don't like it you can always provide custom implementations in separate classes leaving your interfaces persistence technology agnostic again.

  3. With some technologies (i.e. JPA) storage specific behavior leaks into your application by the very way they work, due to lazy loading and dirty checking.

If you want to go 100% store agnostic I guess there is no way but rolling your own persistence, possibly by copying your domain model into transport objects and persisting those using Spring Data.

But this creates a lot of work for the developer and also the JVM at runtime for a somewhat artificial benefit.

Upvotes: 6

Alexander Petrov
Alexander Petrov

Reputation: 9492

When combining technologies such as REST,Hazelcast, JPA I find it excessive to annotate model with Hibernate, then put DataSerializeable on top and then possibly reuse it in the context of REST by marking it with some JSon attributes.

In order to strip some of the dependencies one way to go would be to use ORM.xml and implement custom Hazelcast Serializers. This way your end model will not be dependent on either Hibernate, nor Hazelcast.

Alternative iis to use some mappers and add a second layer of domain objects. As Jens already stated there would be an overhead related to this.

Upvotes: 0

Related Questions