POCO :- POCO
class is the class which doesn’t depend on any framework specific base class.
It is like any other normal .net class that is why it is 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 DTOs
(Data Transfer Objects) with classes to pass the data b/w layers., Because POCOs
are also used to pass data b/w layers ,but they become heavy. So they use DTOs
those are also classes.
The main difference
b/w DTO and POCO is that DTOs do not contain any methods. They only contain
public members. So sending data through DTO is easy because this is lightweight
Objects .
The below code defines a POCO class.
If you want to
create POCO classes instead of entity classes or default entity object, then
you can create POCO entity classes.
To create POCOs
classes first we have to disable auto create classes or auto create code
generation, Which generate Context classes entity code in Model1.designer.cs.
For disable this code generation right click on model1.edmx(ado.net data modal)
and click on property then you will see the
value of ‘Custom Tool’ As ‘EntityModelCodeGenerator ’ you remove this
value.
After removing value of “Custom tool” you will se that in
modal1.edmx there is no Model1.designer.cs class means now we have to
create properties (Context and Entities ) so for this we have to create POCOs
classes.
Now double click on
Modal1.edmx and right click on designer surface and click on Code generation
Items. There a screen will open from there you select ‘ADO.NET POCO Entity
Generator’ and click ‘Add’.
After click on add button you will see 2 classes one is modal1.context.tt and other is modal1.tt
Model1.Context.tt is a context file and Model1.tt is
entities file. You can modify this file if you want to generate your template.
Model1.Context.cs file has a context class and .cs files under Model1.tt are
entity classes.
Entity classes have all properties as "Virtual". Means
that these entities fulfil 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 context then replace ObjectSet<> to IObjectSet<>
- If you are not able to see ‘ADO.NET POCO Entity Generator’ then you install NuGet package library.