There are too many discussions on multiple sites on why custom entities, custom entites vs data sets etc. In my view every solution has its own advantages and disadvantages. However in this blog, i prefer to discuss on custom entities and i speak in favour of custom entities.
What are custom Entities?
Custom entities are objects, just like some other objects which represent the business domain. They map the data to our application business. If i have to explain in simple .Net terms, custom entities are classes which have properties. This is typically used in n tier architecture.
For example, if we are developing an application on online shopping we can have entity like product which becomes our class. It can have properties like product name, cost, discount, size, weight etc.
'Visual Basic .NET
Public Class Product
#Region "Fields and Properties"
Private _weight As Integer
Private _productName As String
Private _size As Integer
Public Property Size() As Integer
Get
Return _size
End Get
Set(ByVal Value As Integer)
_size = Value
End Set
End Property
Public Property ProductName() As String
Get
Return _productName
End Get
Set(ByVal Value As String)
_productName = Value
End Set
End Property
Public Property Weight() As Integer
Get
Return _weight
End Get
Set(ByVal Value As Integer)
_weight = Value
End Set
End Property
#End Region
#Region "Constructors"
Public Sub New()
End Sub
Public Sub New( name As String, size As Integer, weight as Integer)
Me.ProductName = name
Me.Size = size
Me.Weight = weight
End Sub
#End Region
End Class
Why Custom Entities?
Custom entities are -
· Object oriented. DataSets are not object oriented. We cannot add functionality to it. For example, if we need to change the format of some string before displaying on the screen, we will need to do it every time we display. If we use custom entity we can do this in the setter of the corresponding property.
· More maintainable. Datasets lack abstraction which means they are tightly coupled with data model. If we use them in presentation layer and column definition changes from back end we will need to make changes to all our layers. This makes enhancements and bug fix difficult.
· Strongly Typed. What i mean is, they are detected during compile time as we will have the entity class defined with us. DataSets are weakly types and any errors in them are detected only during run time.
· More Secure. If we are sharing our API with any third party developers, it is better we do not expose our underlying structure. Custom Entity hide our data model and show only the business view of it. Hence it is mostly preferred when we have web services. Another point to note here is that the datasets are not optimised on serialization as well.
· Developer Friendly. Custom Entities will get the help if intellisense and become easier to code. We don’t even need to track the column index or names, all are business friendly.