Question

I have read about Persistence Ignorance principle, The ebook give some examples of violations of this principle, but I don't understand. Could you please help me to give some explain?

Some examples of violations of this principle include:
• A required base class
• A required interface implementation
• Classes responsible for saving themselves (such as the Active Record pattern)
• Required default constructor
• Properties requiring virtual keyword
• Properties forced to use certain types (for example, collection properties must expose
ICollection, not just IEnumerable)
• Persistence-specific required attributes
Was it helpful?

Solution

This seems like "Don't use ORMs list" than anything else. As most of those "violations" are requirements to use ORMs like nHibernate and Entity Framework.

• Classes responsible for saving themselves (such as the Active Record pattern)

This one is obvious, as class knowing how to persist itself is not persistence-ignorant. And is how ORM worked in Ruby on Rails (initially from what I remember).

• A required base class

In older version of Entity Framework, each entity, that was meant to be persisted to database, had to inherit from Entity Framework's base class. This was widely criticized and people didn't really use it.

• Required default constructor

• Properties requiring virtual keyword

• Properties forced to use certain types (for example, collection properties must expose ICollection, not just IEnumerable)

These are requirements for a class to be persistable using nHibernate (and 'newer' version of Entity Framework). This is because nHibernate creates a wrapper class and adds change listeners to all properties, thus requiring them to be virtual, and also needs to be able to create the class itself, thus need for parameterless constructor.

Most people take requirements imposed by nHibernate as necessary evil, as only alternative is rolling out your own persistence layer or duplicating the entities within the persistence layer as dumb DTOs.

Licensed under: CC-BY-SA with attribution
scroll top