4

I'm trying to implement fully valid persistence ignorance with little effort. I have many questions though:

The simplest option

It's really straightforward - is it okay to have Entities annotated with Spring Data annotations just like in SOA (but make them really do the logic)? What are the consequences other than having to use persistance annotation in the Entities, which doesn't really follow PI principle? I mean is it really the case with Spring Data - it provides nice repositories which do what repositories in DDD should do. The problem is with Entities themself then...

The harder option

In order to make an Entity unaware of where the data it operates on came from it is natural to inject that data as an interface through constructor. Another advantage is that we always could perform lazy loading - which we have by default in Neo4j graph database for instance. The drawback is that Aggregates (which compose of Entities) will be totally aware of all data even if they don't use them - possibly it could led to debugging difficulties as data is totally exposed (DAO's would be hierarchical just like Aggregates). This would also force us to use some adapters for the repositories as they doesn't store real Entities anymore... And any translation is ugly... Another thing is that we cannot instantiate an Entity without such DAO - though there could be in-memory implementations in domain... again, more layers. Some say that injecting DAOs does break PI too.

The hardest option

The Entity could be wrapped around a lazy-loader which decides where data should come from. It could be both in-memory and in-database, and it could handle any operations which need transactions and so on. Complex layer though, but might be generic to some extent perhaps...? Have a read about it here

Do you know any other solution? Or maybe I'm missing something in mentioned ones. Please share your thoughts!

0

4 Answers 4

14

I achieve persistence ignorance (almost) for free, as a side effect of proper domain modeling.

In particular:

  • if you correctly define each context's boundary, you will obtain small entities without any need for lazy loading (that, actually becomes an antipattern/code smell in a DDD project)
  • if you can't simply use SQL into your repository, map a set of DTO to your db schema, and use them into factories to initialize entity classes.

In DDD projects, persistence ignorance is relevant for the domain model itself, not for repositories, factories and other applicative code. Indeed you are very unlikely to change the ORM and/or the DB in the future.

The only (but very strong) rational behind persistence ignorance of the domain model is separation of concerns: in the domain model you should express business invariants only! Persistence is an infrastructural concern!

For example without persistence ignorance (and with lazy loading) the domain model should handle possible exceptions from the db, it's complexity grows and business rules are buried under technological details.

7
  • 1
    Could you please write example domain object, factory and repository that applies to what you're saying? It's quite difficult for me to get the concept without seeing the (pseudo) code. Did you mean Domain Objects are simple classes with normal set of fields? How to water/extract data from them then? A hibernate mapping as someone suggested?
    – kboom
    Aug 21, 2013 at 8:22
  • 3
    Technically speaking, domain objects should be simple POJO/POCO. Otherwise you will eventually end to mix different concerns. Persistence should be an effect of events observed by repositories. Aug 21, 2013 at 9:57
  • So as I see it once the object is retrieved from the database there is no need to do something like repository.save(domainObject) anymore because it should post an event itself when some of its data gets modified which the repository will instantly handle?
    – kboom
    Aug 21, 2013 at 10:59
  • 1
    Exactly. I've tested this approach in a few complex real-world DDD projects and it worked fine. However we have custom infrastructure to make it cheap. Aug 21, 2013 at 12:53
  • Do you mean something like this ?
    – kboom
    Aug 21, 2013 at 13:50
7

Personally I find it near impossible to achieve a clean domain model when trying to use the same entities as the ORM.

My solution is to model my domain entities as I see fit and ensure that any ORM entities don't leak outside of the repositories. This means that my repositories accept and return domain entities.

This means you lose "most of your ORM goodness" and end up "using your ORM for simple CRUD operations".

Both of these trade-offs are fine for me, I would rather have a clean domain model that I can use, rather than one polluted with artefacts from my DB or ORM. It also cuts down the amount of time I spend "wrestling with my ORM" to zero.

As a side-note, I find document databases a much better fit for DDD.

5
  • How about a graph database like Neo4j?
    – kboom
    Aug 21, 2013 at 10:58
  • Sounds good, I've never seen Neo4j before, but it looks like the same concept as most document database (where your average document DB would flatten the object graph into JSON). Aug 21, 2013 at 11:20
  • Graph databases are really powerful. Maybe whole Persistence Ignorance concept needs reconsideration in this light...
    – kboom
    Aug 21, 2013 at 12:13
  • 1
    how do you handle simple entities that need to be read from the persistence store? Why not expose DAOs in situations where you're only reading data?
    – n3wb
    Feb 13, 2014 at 22:50
  • If I'm using EF, I call GetThing() method on a repository, usually via a service. If I'm using a document database, then I can query from whatever layer I please, as my database contains serialised aggregate roots, so there's not a need for DAOs or Repositories at all. Feb 14, 2014 at 10:36
1

Once you will provide persistence mapping in you domain model:

  • your code depends on framework. If you decided to change this framework, you want to change persistence layer and model layer source code - more work, more changes, more merging of code etc.
  • your domain model jar file depends on spring/nhibernate jars etc.
  • your classes become larger and larger how business code and persistence related code grows

I've to admit that I dont understand harder and hardest option.

We used separated interfaces and implementations for domain entities. Provide separated mapping files using Hibernate along with repositories.

Entities are created using factory (or repository later), identifier is generated within persistence layer, entity does not need it until it's being persisted.

Lazy loading is provided by special implementation of List once:

  1. mapping of an entity contains it
  2. entity/aggregate is fetched from persistence layer

The only issue is related to transaction as when you use lazy-loaded collection out of transaction scope, it fails.

6
  • Okay so what you're saying is that Entities should be absolutely normal classes with no concept of external data at all (they should just have normal fields) and I should make a special xml mapping file for every Entity yeah? And Hibernate should handle the mapping, right? Could you please provide me with reliable link?
    – kboom
    Aug 21, 2013 at 7:54
  • yes, I like that approach :-) You sometimes need to adjust domain model entity, e.g. because of hibernate requirements of a class (e.g. private parameterless constructor). I don't like approach of external data as IMHO you mean to provide some DTOs, right? This could be performance issue to create and than GC entities. Look at docs.jboss.org/hibernate/orm/4.3/manual/en-US/html_single/… it start with definition of mapping types Aug 21, 2013 at 8:44
  • I believe mapping has also some performance impact :). Anyways, I love the concept of having normal, not-annotated domain classes. I meant DAOs by behind the interface (so it's entity). One more thing - should I name my domain objects by term of entities, aggregates and value objects or do in in the database?
    – kboom
    Aug 21, 2013 at 9:07
  • I rather use repository pattern when use DDD approach, see stackoverflow.com/questions/8550124/…. I do not use these terms in class names. Why do you want to do that? Typically entity has identifier but value object is defined by properties/fields - you can see it within first glance. Typically you have few aggregates, as roots of your domain object hierarchy, so this is "well known information" within your team. If you'd like to mark your classes by these term, you should use comments. Aug 21, 2013 at 9:17
  • Understood :). So in my Domain project I should include: all domain objects with all domain logic (with simple data inside them just like there was no database), abstract repositories (interfaces, allowing me to do something like add a domain object and retrieve it by something related to it) and a factory which... well - it should be abstract just like interfaces or shall I implement it...? What about testing then?
    – kboom
    Aug 21, 2013 at 9:37
1

I would follow the simplest option unless I ran into a stone wall. There are also pitfalls such as this when you adopt pi principle.

Somtimes some compromises are acceptable.

public class Order {
    private String status;//my orm does not support enum

    public Status status() {
        return Status.of(this.status);
    }

    public is(Status status) {
        return status() == status;//use status() instead of getStatus() in domain model
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.