Understanding Persistence Ignorance Principle
Before understanding what is Persistence Ignorance principle, we need to understand the difference between DTO and POCO.
As a programmer we should know about DTOs represent as Data Transfer Objects A class object which contains state but not contains any behavior. It means a class contains only properties where it holds the data but it doesn’t contains any methods.
public class EmployeeDTO { public String FirstName public String LastName public String Email public String PhoneNo }
Similarly, POCO represents as Plain Old C# objects. A class object which contains both state and behavior is known as POCO. It means a class contains both properties declared to hold the state as well as methods to manage the behavior.
public class EmployeePOCO { public String FirstName public String LastName public String Email public String PhoneNo public decimal GetLeavesCount() {} }
Now, where does Persistence Ignorance principle suites here? Exactly, as we learn a POCO class can contain a behavior but as per this principle, this behavior shouldn’t be related to any persistence framework like Entity Framework, NHibernate etc..,
public class EmployeePOCO { public String FirstName public String LastName public String Email public String PhoneNo public decimal GetLeavesCount() {} //This is not allowed as it should interact with persistence Framework }
It means, no method should be in a class which performs CRUD operations. This principle will be useful in DDD (Domain Driven Development) model development where we need to follow it to build Domain Model layer.