September 22, 2006 12:09 AM

Passing Data through Layers

Design and Implement a Custom Business Entity Class
DevConnections
Rating: (0)

asp:Feature

LANGUAGES: C#

ASP.NET VERSIONS: 2.0

 

Passing Data through Layers

Design and Implement a Custom Business Entity Class

 

 

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

 

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

 

Use the following code for the IBusinessEntity interface:

 

using System;

 

namespace Definitions

{

     public interfaceIBusinessEntity

     {

           object ID

           {

                 get;

                 set;

           }

     }

}

 

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

 

using System;

using System.Collections;

using Definitions;

 

namespace BusinessEntity

{

     public abstract classBusinessEntityBase :

     IBusinessEntity

     {

           private objectid;

 

           public objectID

           {

                 get

                 {

                       return this.id;

                 }

 

                 set

                 {

                        this.id = value;

                 }

           }     

      }

 }

Figure 1: Code forthe abstract class BusinessEntityBase.

 

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

 

public abstract class BusinessEntityCollectionBase :

CollectionBase

 {

      publicBusinessEntityCollectionBase()

      {

       

      }

 

       public voidCopyTo(IBusinessEntity[]

       iBusinessEntity,intindex)

       {

             List.CopyTo(iBusinessEntity,index);

       }

 

       public boolContains(IBusinessEntity

       iBusinessEntity)

       {

             returnList.Contains(iBusinessEntity);

       }

 

       public intIndexOf(IBusinessEntity

       iBusinessEntity)

       {

             returnList.IndexOf(iBusinessEntity);

        }

 

       public new intCount

       {

             get

             {

                   returnbase.Count;

             }

       }

 

       public intAdd(IBusinessEntity

       iBusinessEntity)

       {

             returnList.Add(iBusinessEntity);

       }

 

       public overridestring ToString()

       {

             returnbase.ToString ();

       }

 

       public voidRemove(IBusinessEntity

       iBusinessEntity)

       {

             List.Remove(iBusinessEntity);

       }

 }

Figure 2: Code forthe abstract class BusinessEntityCollectionBase.

 

using System;

using BusinessEntity;

using Definitions;

 

public class Customer : BusinessEntityBase

{

     private string name;

     private stringaddress;

      

     public string Name

     {

           get

           {

                 returnthis.name;

           }

 

           set

           {

                 this.name= value;

           }

     }

 

     public string Address

     {

           get

           {

                 returnthis.address;

           }

 

           set

           {

                 this.address = value;

           }

     }

}

Figure 3: Code forthe Customer class.

 

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

 

public class CustomerCollection :

 BusinessEntityCollectionBase

{

     public Customerthis[int index]

     {

           get

           {

                 return(Customer)base.InnerList[index];

           }

           set

           {

                 base.InnerList[index] = value;

           }

     }

}

Figure 4: The CustomerCollectionclass inherits the BusinessEntityCollectionBase class.

 

The following code snippet shows how we can make use ofthe 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 CustomBusiness Entity class and a Custom Business Entity Collection class that we canuse in our applications. Use of the Custom Business Entity class in lieu of thetraditional way of passing an instance of the DataSet class can boost theperformance of our application, as it would consume much less memory resourcescompared to the use of a DataSet instance.

 

Working extensively in Microsoft technologies for more than 10years, Joydip Kanjilal is a SeniorProject Leader for a company in a Hyderabad,India. His programmingskills include C, C++, Java, C#, VB, VC++, ASP.NET, XML, and UML. He has workedwith .NET and C# for more than five years. Reach Joydip at mailto:joydipkanjilal@yahoo.com.

 

 

 

 

Add a Comment

There are no comments to display. Be the first one!
You must log on before posting a comment.

Are you a new visitor? Register Here

advertisement




Comments from the DevConnections Community

Join our community of development pros.

Windows problem

I all, I have a problem on my Windows Vista that began afetr the purchase of an external Hard Disk Freecom. A few days afetr the purchase I discon...

Most Recent Posts

GOOGLE LINKS
SPONSORED LINKS
FEATURED LINKS