Pages

SPONSORED

Showing posts with label object oriented. Show all posts
Showing posts with label object oriented. Show all posts

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.

What do you mean by Inheritance?

Inheritance is a process through which one object can inherit the characteristics of another object. This is similar to the parent-child relationship in real time scenario where a child can inherit the characteristics of parent.

Inheritence  helps in setting up relationships among objects and can help in reusing of code. Implementations of the parent can be simply inherited by the child. The child need not re-implement the functionality unless any change in behaviour is expected.This also impleves maintainability by avoiding duplication of code and hence reduces defects as well as development time. This in turn can improve code quality.

In inheritance, objects are defined by classes. The existing class is also called as– super class, parent class, base class and ancestral class. The inherited class is called as – sub class, child class, heir class or derived class.

 
Inheritence can be depicted by a simpe example -
To represent different kinds of geometric shapes, such as circles or rectangles programmatically we can use inheritance. We can create separate classes for each shape, but these different kinds of shapes share a number of things in common. We can define a type called "Shapes" to represent any of these different kinds of figures (circles or rectangles). We could then say that circles and rectangles are a kind of figure. A given type includes all of the record components specific to it, plus all the record components of its ancestors, so a square has all the information a rectangle would.

We'll also need to define some operations on these types, such as "Draw", "Area", and "Perimeter". Each calculated in a different way for each of the shape. Pictorial representation of this example is shown below.



Inheritance Example
 
 
We have seen how and when to use inheritence. We have also discussed its advantages. This does not mean that we implement inheritence in all scenarios. Do not use inheritence just for the sake of reuse. Avoid using it if there is no sub type relationship. Overuse of implementation inheritance can break all the subclasses, if the superclass is modified. Use composition for code reuse in such cases.

Quality of code can be achieved when the developer is smart enough to decide on the feature to put into use from OOP.