Pages

SPONSORED

Custom Entity Classes

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.

Decision to use custom entity or datasets should not be based on the theoretical concepts we read. Multiple factors must be considered like memory available, objects created etc. No design can be tagged good or bad. Good design is the one which considers all the functional and non functional aspects around the application.

Dynamic HTML (DHTML)

DHTML, Dynamic HTML sounds like extension to HTML. It isn’t just DHTML; DHTML is not a language, it is a technology. It is collection of JavaScript, CSS and DOM technologies. HTML creates static web pages. Static means the pages remain constant throughout. HTML pages can still be made dynamic with the help of server side scripts or AJAX. However this is on request response basis.

Does this mean that it is possible to create dynamic pages without using request response technique? Yes, using DHML. DHTML helps in creating dynamic pages after that page is fully loaded. It can alter the page when the user is viewing the page. The scripting language associated with it (JavaScript) alters the variables and changes the content on the page and makes it dynamic. DHTML controls HTML codes and commands. This feature of DHML has made it helpful in creating interactive and animated web pages.

Example:
<html>
<head>
<script language="javascript">
function changeText(){
document.getElementById("test").value ="DHTML example..."
}
</script>
</head>
<body>
<input type="button" id="test" value="   Click Here  " onClick="changeText()" />
</body>
</html>


By now you would have realised that this not something new! You have been using it in your day-to-day coding. We generally call it client side coding, it collectively referred as DTHML in technical language.

Ajax is one step ahead of this. It is DHTML plus server side call. Concept still remains the same, we dynamically create pages but with a call to server. I will not get into details of Ajax as it not in scope of this post.