Exclusively for you!! Trial version of Google Apps, Apply Coupon-CNHPGX6T6QRWGV!!

POCO vs Custom Classes/DTO

OOPS-class-object
OOPS-class-object @Image courtesy of digitalart / FreeDigitalPhotos.net

POCO vs Custom Classes/DTO

Welcome to CodeSpread!

When I started working on Entity Framework, I didn’t catch the idea of POCO initially. I started with custom classes and later came to know the advantages of POCO over custom classes.

What is POCO?

POCO stands for Plain Old CLR Object .It provides freedom to define object model in which objects does not inherit from specific base classes. This enhances control over the design and implementation.It basically refers to an object that does not have any persistence concerns and is not aware of persistence framework. POCO mainly supports serialization.

Persistence ignorance means that business logic layer and other layers are not dependent on the design of the database, type of database,type of database framework . The flow of data between layers should be independent of any predefined process. To achieve POCO entities, these are the rules required to be followed.

  • No more inheriting from a base class.
  • No interface Implementation.
  • No need for metadata or mapping attributes.

Modern development approaches such as DDD (Domain-Driven Design) and TDD (Test-Driven Development) prefer clear separation of concerns which implies persistence ignorance. The main advantage is that applications will follow a conceptual model that is independent of the physical/storage model

Example: Create POCO entity for Category and Product


public class Category {

public int CategoryID

{ get; set; }

public string CategoryName

{ get; set; }

public string Description

{ get; set; }

public List<Product> Products { get; set; }

}

public class Product {

public int ProductID

{ get; set; }

public string ProductName

{ get; set; }

public Category Category

{ get; set; }

}

In this example, We have a Category entity which contains properties to define it and also a navigation property List. For this Navigation Property, we have one more entity as Product.

What is a Custom Class/DTO?

DTO usually refers to Data Transfer Objects. A DTO can be described as a  collection of properties which has no behavior of its own. It is a lightweight container used for moving data between layers. It can be simplified as a representation of entities in the application so that if at anytime ,Entity got changed or modified, there will be  no effect on application as the DTO will not change. A DTO’s only purpose is to transfer state, and should have no behavior. data will get exchanged between the layers with the help of containers like DTOs and application will be free from any underlying complexity of the related entity.

Example: EmployeeDTO


public EmployeeDTO()

{

EmployeeId = Int_NullValue;

Name = String_NullValue;

Email = String_NullValue;

Address = String_NullValue;

City = String_NullValue;

State = String_NullValue;

ZipCode = Int_NullValue;

}

What is Entity?

As entity is a real word thing which can have some properties to define it or some behavior associated with it.

What is the difference or advantage of one over other?

The classes generated by Entity Model gets inherited from Entity Framework Base class. As we have already said earlier, POCO doesn’t inherit from any Base class. While inheriting from Entity Framework base class, the logic behind change tracking is hidden  and all the logic is stored inside the context that’s holding references to the objects. While it is the preferred approach in a connected environment where entities are always available.

But If we talk about disconnected architecture like working with web services, the entities which are moving between servers are not a part of a particular context and have to track the state themselves. For this scenario, POCO is the preferred option as these entities can mover between layers and servers without depending on the changing context.

We can also treat POCOs like DTOs but DTOs should be designed to transfer data plus they can not represent the true structure of the business object. DTO cannot contain behavior and will always be treated as a container.

Author: hershey

A passion for knowledge drives me to do programming, A passion for programming drives me to create something different, A passion for creation drives me to spread the knowledge.

Share This Post On

0 Comments

  1. nice superb explaination

    Post a Reply
  2. thanks for the tutorial
    I am having a problem.
    The feature “Create SQL server database” isn’t available , I can’t select it.
    Can you help, please ?

    Post a Reply

Submit a Comment

Your email address will not be published. Required fields are marked *

More from CodeSpread:

  • EDMX file.EDMX file.Few lines about Entity Framework The Entity Framework is an object-relational mapping framework or O/RM.For more info on O/RM refer Magic Wand: Object-relational mapping. It was released with .NET...
  • Try one ORM: Entity FrameworkTry one ORM: Entity FrameworkAs promised in the last article, We are going to share our experience of Entity Framework Ver 4.1. First of all, we have to add and configure Entity Framework in visual studio. We are going to u...
  • Agile Encounters UnpredictabilityAgile Encounters UnpredictabilityWhat is Agile? Wikipedia says “Agile software development is a group of software development methods based on iterative and incremental development, where requirements and solutions evolve through...
  • Freeze Business Logic Layer.Freeze Business Logic Layer.We regularly work on three-layer architecture for application development and always use some standard data layer and UI layer guidelines for design but what we always ignore is business logic laye...
  • Lean in ITLean in ITWikipedia says ‘Lean IT is the extension of lean manufacturing and lean services principles to the development and management of information technology (IT) products and services.’ What are these ...
  • Magic Wand: Object-relational mappingMagic Wand: Object-relational mappingAs a developer, we are always looking for reusable components which can ease our life and I can suggest ORM as the developer's magic wand because it wipes off the overhead of dealing directly with ...
  • How To: Cross Page PostingHow To: Cross Page PostingWhat is a POST? Wikipedia says,The POST request method is designed to request that a web server accept the data enclosed in the request message's body for storage.It is often used when uploading a...
  • Best Practices: Does Professional Database exists?Best Practices: Does Professional Database exists?What is Database? Database is a structured set of data, which facilitates storage,search and retrieval. Databases revolves around tables and relationships between them. Imagine you have a truck lo...