POCO
A Plain Old CLR Objects (POCO) is a class that doesn't depend on any framework-specific base class. It is like any other normal .Net class; that is why they are called “Plain Old CLR Objects”. These POCO entities (also known as persistence-ignorant objects) support most of the same LINQ queries as Entity Object derived entities. These classes (POCO classes) implement only the domain business logic of the application.
Some developers use Data Transfer Objects (DTOs) with classes to pass the data between layers because POCOs are also used to pass data between layers, but they become heavy. So they use DTOs that are also classes.
The main difference between DTO and POCO is that DTOs do not contain any methods. They only contain public members. So sending data using a DTO is easy because they are lightweight objects.
A Plain Old CLR Objects (POCO) is a class that doesn't depend on any framework-specific base class. It is like any other normal .Net class; that is why they are called “Plain Old CLR Objects”. These POCO entities (also known as persistence-ignorant objects) support most of the same LINQ queries as Entity Object derived entities. These classes (POCO classes) implement only the domain business logic of the application.
Some developers use Data Transfer Objects (DTOs) with classes to pass the data between layers because POCOs are also used to pass data between layers, but they become heavy. So they use DTOs that are also classes.
The main difference between DTO and POCO is that DTOs do not contain any methods. They only contain public members. So sending data using a DTO is easy because they are lightweight objects.
The following code defines a POCO class.
If you want to create POCO classes instead of entity classes or a default entity object then you can create POCO entity classes.
To create POCO classes we first need to disable auto create classes or auto create code generation that generate Context classes entity code in Model1.designer.cs. To disable this code generation, right-click on model1.edmx (ADO.Net data modal) and click on property and then you will see the value of "Custom Tool" As "EntityModelCodeGenerator " and you remove this value.

After removing the value “Custom tool” you will see that in modal1.edmx there is no Model1.designer class. Now we must create properties (Context and Entities ) so for this we need to create POCOs classes.
Now double-click on Modal1.edmx and right-click on the designer surface and click on the code generation Items. A screen will open from where you select "ADO.NET POCO Entity Generator" and click "Add".

To create POCO classes we first need to disable auto create classes or auto create code generation that generate Context classes entity code in Model1.designer.cs. To disable this code generation, right-click on model1.edmx (ADO.Net data modal) and click on property and then you will see the value of "Custom Tool" As "EntityModelCodeGenerator " and you remove this value.

After removing the value “Custom tool” you will see that in modal1.edmx there is no Model1.designer class. Now we must create properties (Context and Entities ) so for this we need to create POCOs classes.
Now double-click on Modal1.edmx and right-click on the designer surface and click on the code generation Items. A screen will open from where you select "ADO.NET POCO Entity Generator" and click "Add".

After clickinig on the add button you will see 2 classes, one is modal1.context.tt and another is modal1.tt.

Model1.Context.tt is a context file and Model1.tt is an entities file. You can modify this file if you want to generate your template. The Model1.Context.cs file has a context class and .cs files under Model1.tt are entity classes.
Entity classes have all the properties as "Virtual". In other words these entities fulfill the requirements of POCO Proxy entities . These entities can be used as POCO entities or POCO Proxy entities. By default it will be behave as POCO Proxy entities, but you can disable proxy creation by setting a property "ObjectContext.ContextOptions.ProxyCreationEnabled = false".
Important:
- If you want to write a unit test for the context then replace ObjectSet<> to IObjectSet<>.
- If you are not able to see "ADO.NET POCO Entity Generator" then you must install the NuGet Package Library.

No comments:
Post a Comment