POCO stands for "Plain Old CLR Object". POCO is simply a regular object with no references to any specific framework and no adherence to their interfaces or constraints.
POCO classes are persistence-independent objects that work with any Entity Framework.
POCO classes enables a simple data storage mechanism and simplifies serialization and data transfer between layers.
🚩 POCO Classes are unaware of the underlying database design.
Instead of having your objects derive from specific base classes, POCO classes idiomatically define your object model.
POCO Example in C#
class DumpTruck {
public DumpTruck() { }
public DumpTruck(string platenumber, double tareweight, double capacity)
{
PlateNumber = platenumber;
TareWeight = tareweight;
Capacity = capacity;
}
public string PlateNumber { get; set; }
public double TareWeight { get; set; }
public double Capacity { get; set; }
public override string ToString()
{
return $"{PlateNumber.ToUpper()} ({TareWeight}tons): {Capacity}";
}
}
📌 POCO is Plain Old CLR Object and it comes from POJO plain old Java Object. A POCO is a class that holds data and has no behaviours.
DTO Example in C#
class DumpTruck {
public string PlateNumber { get; set; }
public double TareWeight { get; set; }
public double Capacity { get; set; }
}
🚨 All DTOs are POCOs, but not all POCOs are DTOs.
Benefits of using POCO
There are many benefits to using POCO classes over other types of classes. POCO classes are a great way to model your domain objects in an efficient and easy-to-use manner. Additionally, POCO classes can be easily mapped to other languages and frameworks, making them an ideal choice if you plan on using them elsewhere in your project.
🚀 One great advantage of using classes is that they can be easily tested and optimized.
All In All
With the help of this article, you should now have a better understanding of POCO classes and how they can be used in an application. Now go out there and start creating your own POCO classes!
Hi, I’m Wajid Khan. I am trying to explain computer stuff in a simple and engaging manner, so that even non-techies can easily understand, and delivered to your inbox weekly.