Skip navigation

Passing Data through Layers - 30 Oct 2009

Design and Implement a Custom Business Entity Class

asp:Feature

LANGUAGES: C#

ASP.NET VERSIONS: 2.0

 

Passing Data through Layers

Design and Implement a Custom Business Entity Class

 

By Joydip Kanjilal

 

Many times we pass data through the layers in an application using a DataSet object without taking advantage of some of the other features that this class offers (beyond simply being a data container). In this article I ll demonstrate an alternative to this technique by designing a Custom Business Entity class.

 

To get started, let s design an interface named IBusinessEntity. An abstract class named BusinessEntity would implement this interface. Why abstract? Because we need not instantiate the BusinessEntity class. Rather, we would sub-class the same based on the type of the BusinessEntity we require and instantiate it to facilitate reusability.

 

Use the following code for the IBusinessEntity interface:

 

using System;

namespace Definitions

{

     public interface IBusinessEntity

     {

           object ID

           {

                 get;

                 set;

           }

     }

}

 

The abstract class BusinessEntityBase implements the interface IBusinessEntity and contains one property named ID, which in turn is of type object. This is because we require a primary key for our Custom Business Entity, the type of which can be determined at run time. The code for BusinessEntityBase is shown in Figure 1.

 

using System;

using System.Collections;

using Definitions;

namespace BusinessEntity

{

     public abstract class BusinessEntityBase :

     IBusinessEntity

     {

           private object id;

           public object ID

           {

                 get

                 {

                       return this.id;

                 }

                 set

                 {

                        this.id = value;

                 }

           }     

      }

 }

Figure 1: Code for the abstract class BusinessEntityBase.

 

The code in Figure 2 shows how we can make use of the concepts learned so far to design a Custom Business Collection Base class. This would be required to bind data to the controls in our application, such as the DataGrid, DataView, etc. The code for the Custom Business Entity class named Customer is shown in Figure 3.

 

public abstract class BusinessEntityCollectionBase :

CollectionBase

 {

      public BusinessEntityCollectionBase()

      {

       

      }

       public void CopyTo(IBusinessEntity[]

       iBusinessEntity,int index)

       {

             List.CopyTo(iBusinessEntity,index);

       }

       public bool Contains(IBusinessEntity

       iBusinessEntity)

       {

             return List.Contains(iBusinessEntity);

       }

       public int IndexOf(IBusinessEntity

       iBusinessEntity)

       {

             return List.IndexOf(iBusinessEntity);

        }

       public new int Count

       {

             get

             {

                   return base.Count;

             }

       }

       public int Add(IBusinessEntity

       iBusinessEntity)

       {

             return List.Add(iBusinessEntity);

       }

       public override string ToString()

       {

             return base.ToString ();

       }

       public void Remove(IBusinessEntity

       iBusinessEntity)

       {

             List.Remove(iBusinessEntity);

       }

 }

Figure 2: Code for the abstract class BusinessEntityCollectionBase.

 

using System;

using BusinessEntity;

using Definitions;

public class Customer : BusinessEntityBase

{

     private string name;

     private string address;

      

     public string Name

     {

           get

           {

                 return this.name;

           }

           set

           {

                 this.name = value;

           }

     }

     public string Address

     {

           get

           {

                 return this.address;

           }

           set

           {

                 this.address = value;

           }

     }

}

Figure 3: Code for the Customer class.

 

Similar to the code in Figure 3 is the CustomerCollection class, which inherits the BusinessEntityCollectionBase class (see Figure 4).

 

public class CustomerCollection :

 BusinessEntityCollectionBase

{

     public Customer this[int index]

     {

           get

           {

                 return (Customer)base.InnerList[index];

           }

           set

           {

                 base.InnerList[index] = value;

           }

     }

}

Figure 4: The CustomerCollection class inherits the BusinessEntityCollectionBase class.

 

The following code snippet shows how we can make use of the Custom Business Entity class designed earlier:

 

Customer customer = new Customer();

customer.ID = 1;

customer.Name = "Joydip Kanjilal";

customer.Address = "Hyderabad,India";

 

We can now pass this object across the layers in our application. If used in lieu of a DataSet, this object would save a lot of memory resources.

 

This article has discussed implementation of a Custom Business Entity class and a Custom Business Entity Collection class that we can use in our applications. Use of the Custom Business Entity class in lieu of the traditional way of passing an instance of the DataSet class can boost the performance of our application, as it would consume much less memory resources compared to the use of a DataSet instance.

 

Working extensively in Microsoft technologies for more than 10 years, Joydip Kanjilal is a Senior Project Leader for a company in a Hyderabad, India. His programming skills include C, C++, Java, C#, VB, VC++, ASP.NET, XML, and UML. He has worked with .NET and C# for more than five years. Reach Joydip at mailto:[email protected].

 

 

 

 

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish