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.
- public class ClientDTO
- {
- public String FirstName
- public String LastName
- public String Email
- public String PhoneNo
- public String MobileNo
- public String County
- public String City
- public String Address
- }
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.
- public class ClientPOCO
- {
- Public String FirstName
- Public String LastName
- Public String Email
- Public String PhoneNo
- Public String MobileNo
- Public String County
- Public String City
- Public String Address
- Public List < ClientPOCO > Retrieve()
- {
-
- }
- }
Difference between POCO and DTO
- 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.