VKinfotek Inc.
  • Us
    About Us
    Contact Us
  • FAQs
  • Ready Software
    Ready ERP ASP Core Software for Azure Cloud
    Ready Web ERP Software
    Ready ASP.Net Azure Software
    Ready C# SQL Server Accounting Software
    Ready ASP.Net MVC and EF Software
First slide
Earn $100/hour in USA! Click Here

POCO class, Entity Framework in Enterprise Applications

Often, programmers struggle with using Plain Old CLR Objects in enterprise applications. This article and the related articles in this section attempt to demystify using these classes while developing enterprise applications. POCO is an abbreviation for Plain Old CLR Objects. This class does not inherit functionality of any specific framework or technology. It is a simple public class and just contains properties and methods. These methods do not implement persistent logic such as saving and retrieving data from the database. This is the reason why we call these classes as Persistence Ignorant classes.

This class can be written in any .NET supported language such as C# and VB.Net, and can be created manually or using templates provided by Microsoft.

Each class is also referred to as domain object because it implements the domain logic of the application. Each method of the class implements validations and business logic and not the persistence logic of the application. This difference is important.

Earlier to the introduction of Entity Framework technology, we were used to developing a Business Layer using plain classes and Data Access Layer (DAL) based on ADO.Net technology. This same plain class which we have been using in all our applications till now, becomes the POCO class in Entity Framework. But with a difference. The difference is these classes do not implement persistent logic whereas the regular classes contain methods which implement persistence logic along with validations and business logic. So, do not think that this is a new feature, instead it is an existing feature and is supported in EF 4.

Enterprise applications are usually developed on multi-tier architecture pattern. In a multi-tier architecture pattern, the application is divided into Presentation layer, Business layer and Data access layer and Data layer (database).

Where do Entity framework and POCO fit in the Multi-Tier application?
Simply put, Entity Framework becomes the DAL of the multi-tier application. We have discussed in the article - Create entity data model using ado.net entity framework designer in visual studio that EF generates the EDM from the database. The EF and the EDM become a part of the DAL.

We know that, in a multi-tier application, the BL and PL should not be aware of DAL. You must also be knowing that classes are generated when we use O/RM tools such as Entity Framework or LINQ to SQL. The generated classes are aware of their persistence, which in simple words means that the classes know how to save and with which data store (data store can be - database or web service, XML file or memory object) the application is interacting. If we use EF generated classes to implement business logic of the applictaion, these classes will be aware of EF. And also, these classes are persistence dependant. So, these classes cannot be used for developing enterprise applications.

On the other hand, POCO classes are persistent ignorant which means that they are not aware of where and how to save data. These classes will make use of an available persistent infrastructure. These classes implement only the domain business logic of the application. You may be wondering by now, where should we write persistent methods. The answer is repositories. If we start using these classes with repositories, we can appreciate the power of Plain Old CLR Objects.

Now that we know where EF and these classes fit into an enterprise application, let us try and understand the advantages of using these classes in enterprise applications.

Briefly, the advantages are:

We can always replace the data access technology from EF with any other data access technology. We need not change or rewrite the Business Layer and Presentation Layer. By doing this, we reduce the dependency between layers.

Suppose, we want to convert the business logic using these classes in a web service, we can easily do it.

I recommend that if you are developing enterprise applications using EF, better use POCO classes in the application. You can refer this book titled: Develop ERP software to learn how to develop BL using simple classes and DAL using ADO.Net technology.

Ultimately, these classes need to use some persistent infrastructure to persist themselves into the database. To use the EF as the persistent infrastructure, we have to follow a few rules while creating these classes.

One additional benefit we get by using this technology is that passing them with data between layers becomes easy because of its light weight and can be easily serialized and deserializerd.

Some programmers prefer to use DTOs (Data Transfer Objects) with these classes to pass data between layers. They feel that when POCOs are used to pass information between layers, they become heavy which is unnecessary. So, they use DTOs which are also classes. The main difference between DTOs and POCOs is that DTOs do not contain any methods. They just contain public members. However, my personal preference is to use these classes without using DTOs, to pass data between layers. This is because these are lightweight objects, serializing them and sending data through them is also easy.

Recently, Microsoft added new functionalities into EF. These functionalities are tracking changes entities(POCO), Lazy Loading and fixing up relationships.

The following code defines a POCO class of a Customer.

POCO class code snippet

Observe that, the word virtual is added for every property in the class. Why we do this is discussed in the coming articles.

The final conclusion is that the Microsoft has built required functionality into Entity Framework for us to develop enterprise applications.

More Resources
  • POCO vs Entity Objects with comparison table
  • How to create a POCO class, integrate with ObjectContext, executing a query with POCOs
  • How to load related POCO entities - loading patterns and their differences in their usage with examples
  • Snapshot change tracking with DetectChanges() method
  • Change tracking with proxies, How to create proxies?
  • Instantiate POCO classes using ObjectContext.CreateObject method
  • Entity Framework EntityKey object
  • Retrieving a single entity with GetObjectByKey method of ObjectContext using EntityKey
  • How to create an EntityKey in Entity Framework
  • Loading Related entities in EF
  • How to change the state of an entity using ChangeObjectState method of ObjectStateManager
  • ChangeState method of ObjectStateEntry class
  • Update an entity in a disconnected scenario using ApplyCurrentValues method
  • Using ApplyOriginalValues method
  • ObjectStateEntry class
  • Entity state in Entity Framework - what, how, why?
  • AcceptAllChanges and SaveChanges methods in Entity Framework
Azure Q & A
Azure Platform
Grid-View
GridView CommandField example
Details-View
GridView and DetailsView Master/Detail page using SqlDataSource control
POCO
POCO overview and advantages - POCO class, Entity Framework in Enterprise Applications
Entity Framework
Query entity data model using linq to entities
Array List
Difference between arraylist and list collection
Web Services
How to create a Web service using Visual Studio.net
Form-View
FormView DataBound Event
Object Oriented Programming
Calling base class constructor in C#
Linq
Convert a sequence to a generic list using ToList()method
Project Ideas
Project ideas for students
AccountingSoftware
Accounting Software
MVC
Creating an ASP.Net MVC 3 application
.Net
Using assemblies in .net applications
ASP .Net
How to implement form validation using ASP.Net 2.0 Validation Controls
VB .Net
Constructors in Visual Basic.Net


  • About Us
  • SITEMAP
  • Terms of use
  • Testimonials

Back to top

Copyright © 2016 - All Rights Reserved - VKInfotek.com