POCO, DTO and Persistence Ignorance Overview

What is a Data Transfer Object (DTO)

Data transfer object is a light weight class which only contains properties, and we can get and set these properties of class.

DTO doesn’t contain any behavior and custom logic.

Use of DTO

DTO is for transferring data between layers.

Why make an object that simple?

For making a type container just for collecting data without any custom logic and due to this light weight, it can easily transfer between layers.

  1. public class ClientDTO  
  2. {  
  3.     public String FirstName  
  4.     public String LastName  
  5.     public String Email  
  6.     public String PhoneNo  
  7.     public String MobileNo  
  8.     public String County  
  9.     public String City  
  10.     public String Address  
  11. }  
What’s a POCO?

POCO stands for Plain Old CLR Object, POCO is a business object which contains data, validation and custom or business logic but it doesn’t contain persistence logic, which  means logic which is related to data stores or database, so due to this POCO are persistent ignorant.

For example

Suppose we have “ClientPOCO” class, so it can contains properties, business logic and validation but
It doesn’t contain persistence logic such as logic related to the dat, for  example SaveClient() or GetClientById(). This means POCO class only contains properties, validation and business logic but doesn’t contain any data logic.

Persistence Ignorance

Persistence ignorance means ignoring persistence logic; this means logic related to data store,

In EF, POCO class doesn’t contains logic which is related to data stores, like saving data into data store or fetching data from data stores.

Below is the code for my BAL.ClientPOCO class which is a POCO.
  1. public class ClientPOCO   
  2. {  
  3.     Public String FirstName  
  4.     Public String LastName  
  5.     Public String Email  
  6.     Public String PhoneNo  
  7.     Public String MobileNo  
  8.     Public String County  
  9.     Public String City  
  10.     Public String Address  
  11.     Public List < ClientPOCO > Retrieve()  
  12.     {  
  13.         // code which access component of Data access layer, and then data access layer interact with data stores, POCO classes not contains data access code or persistence code.   
  14.     }  
  15. }  
Difference between POCO and DTO

 

  1. POCO classes have state and behavior but DTO only has state, no behavior.

Summary

So when you are working on business layers with business objects, POCO class will consider which contains properties and business logic but without any persistence logic, and POCO class is not dependent on DB structure. And DTO is just a light weight class which  contains properties only and is used to transfer data between layers or between applications.


Similar Articles