Decoupling Business Logic Layer from the User Interface Layer using C#
page 5 of 9
by Joydip Kanjilal
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 51217/ 81

Implementing the Framework

The classes and interfaces involved in this framework are:

·         IBusiness

·         IBusinessLogic

·         IBusinessEntity

·         BusinessLogic

·         BusinessService

·         BusinessEntity

·         BLFactory

·         BEFactory

IBusiness is a top level interface that contains the CRUD methods implemented by all the Entities and Business Logic classes. The IBusinessLogic is base interface from which the BusinessLogic base class is implemented. All business logic classes that you create should inherit the BusinessLogic base class and implement the IBusinessLogic interface of this framework. The IBusinessEntity is the base interface for the BusinessEntity base class. All your entities will derive from this BusinessEntity class and implement the IBusinessEntity interface.

The following code listings show the IBusiness, IBusinessLogic and the IBusinessEntity interfaces.

Listing 1

using System;
using System.Data;
using System.Collections;
 
namespace Definitions
{
    public interface IBusiness
    {
        bool Create(int hashCode);
        bool Delete(int hashCode);
        bool Update(int hashCode);
        int Read();
     }
}

Listing 2

using System;
using System.Reflection;
 
namespace Definitions
{
    public interface IBusinessLogic : IBusiness
    {
        int HashCode
        {
            get;
            set;
        }
 
        int Read(int hashCode);
    }
}

Listing 3

using System;
namespace Definitions
{
    public interface IBusinessEntity
    {
        int Key
        {
            get;
            set;
        }
    }
}

The BusinessLogic class is the business logic base class from which all the business logic classes should derive. Similarly, the BusinessEntity class is the base class for all BusinessEntity classes. The BusinessService class is based on the Façade design pattern and is the gateway to this framework from the user interface’s perspective. The Presentation layer of the application interacts with this class to pass the necessary information. The BusinessService class uses dependency injection technique to decouple the business logic layer from the user interface layer of the application.

The following code listings show the BusinessLogic, BusinessEntity and the BusinessService classes.

Listing 4

using System;
using System.Data;
using System.Web;
using Definitions;
 
namespace Framework
{
    public class BusinessLogic : IBusinessLogic, IBusiness
    {
        private IBusinessEntity iBusinessEntity = null;
        private int hashCode = -1;
 
        protected BusinessLogic()
        {
 
        }
 
        public IBusinessEntity BusinessEntity
        {
            get
            {
                return iBusinessEntity;
            }
 
            set
            {
                iBusinessEntity = value;
            }
        }
 
        #region IBusiness Members
 
        bool IBusiness.Create(int hashCode)
        {
            return true;
        }
 
        bool IBusiness.Update(int hashCode)
        {
            return true;
        }
 
        bool IBusiness.Delete(int hashCode)
        {
            return true;
        }
 
        int IBusiness.Read()
        {
            return -1;
        }
        
        #endregion
 
        #region IBusinessLogic Members
 
      
        int IBusinessLogic.Read(int hashCode)
        {
            return -1;
        }
 
        public int HashCode
        {
            get
            {
                return hashCode;
            }
            set
            {
                hashCode = value;
            }
        }
 
        #endregion
    }
}

Listing 5

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Data;
using Definitions;
 
namespace Framework
{
    public abstract class BusinessEntity : IBusinessEntity
    {
        private int key;
 
        public int Key
        {
            get
            {
                return this.key;
            }
            set
            {
                this.key = value;
            }
        }
    }
}

Listing 6

using System;
using Definitions;
 
namespace Framework
{
    public enum OperationType
    {
        Create, Update, Read, Delete
    }
 
   public class BusinessService
    {
       private IBusinessLogic iBusinessLogic = null;
       private IBusinessEntity iBusinessEntity = null;
 
       public IBusinessLogic BusinessLogic
       {
           get
           {
               return iBusinessLogic;
           }
 
           set
           {
               iBusinessLogic = value;
           }
       }
 
       public IBusinessEntity BusinessEntity
       {
           get
           {
               return iBusinessEntity;
           }
 
           set
           {
               iBusinessEntity = value;
           }
       }
 
       public int Execute(OperationType operationType)
       {
           int hashCode = -1;
           //Code to register the business entity 
           //instance in the DTORegister. On doing so,
           //the hash code of the instance is returned 
           //which is used to pass data across
           //the layers of the application.
           switch (operationType)
           {
               case OperationType.Create:
                   iBusinessLogic.Create(hashCode);
                   break;
 
               case OperationType.Update:
                   iBusinessLogic.Create(hashCode);
                   break;
               case OperationType.Delete:
                   iBusinessLogic.Delete(hashCode);
                   break;
               case OperationType.Read:
                   hashCode = iBusinessLogic.Read(hashCode);
                   break;              
           }
 
           return hashCode;
       }
    }
}

You need to insert the code to register the business entity instance in the DTORegister, a hash table containing DTO instances. You can find my article on Data Transfer Object here. I have commented the portion of the code in the Execute() method where you need to insert it. You should also un-register the DTO instance once the CRUD operation is over.

We have two factory classes called BusinessLogicObjectFactory and BusinessEntityObjectFactory. These classes are based on the Factory design pattern and are responsible for returning the required business logic and business entity instances. The following code listings illustrate the two factory classes, BLFactory and BEFactory.

Listing 7

using System;
using System.Text;
using System.Collections.Generic;
using Definitions;
using Framework;
using BusinessObjects;
 
namespace ObjectFactory
{
    public enum BLObjectType
    {
        Employee, Department
    }
 
    public class BLFactory
    {
        public static IBusinessLogic GetBLObject(BLObjectType bLObjectType)
        {
            IBusinessLogic iBusinessLogic = null;
 
            switch (bLObjectType)
            {
                case BLObjectType.Employee:
                    iBusinessLogic = new EmployeeBO();
                    break;
                case BLObjectType.Department:
                    iBusinessLogic = new DepartmentBO();
                    break;
                default:
                    return null;
            }
 
            return iBusinessLogic;
        }
 
        public static IBusinessLogic GetBLObject(string bLObjectType)
        {
            IBusinessLogic iBusinessLogic = null;
 
            switch (bLObjectType)
            {
                case "EmployeeBO":
                    iBusinessLogic = new EmployeeBO();
                    break;
                case "DepartmentBO":
                    iBusinessLogic = new DepartmentBO();
                    break;
                default:
                    return null;
            }
 
            return iBusinessLogic;
        }
    }
}
 

Listing 8

using System;
using System.Text;
using System.Collections.Generic;
using Definitions;
using Framework;
using DataObjects;
 
namespace ObjectFactory
{
    public enum BEObjectType
    {
        Employee, Department
    }
 
    public class BEFactory
    {
        public static IBusinessEntity GetBEObject(BEObjectType bEObjectType)
        {
            IBusinessEntity iBusinessEntity = null;
 
            switch (bEObjectType)
            {
                case BEObjectType.Employee:
                    iBusinessEntity = new EmployeeDO();
                    break;
                case BLObjectType.Department:
                    iBusinessLogic = new DepartmentDO();
                    break;
                default:
                    return null;
            }
 
            return iBusinessEntity;
        }
 
        public static IBusinessEntity GetBEObject(string bEObjectType)
        {
            IBusinessEntity iBusinessEntity = null;
 
            switch (bEObjectType)
            {
                case "EmployeeDO":
                    iBusinessEntity = new EmployeeDO();
                    break;
                case "DepartmentDO":
                    iBusinessLogic = new DepartmentDO();
                    break;
                default:
                    return null;
            }
 
            return iBusinessEntity;
        }
    }
}

The following code listings show the EmployeeBO and the EmployeeDO classes. While the former is the custom business logic class, the later is the custom business entity class. As mentioned earlier, both these classes are instantiated using the respective factory classes shown earlier.

Listing 9

using System;
using System.Data;
using System.Data.Common;
using System.Collections.Generic;
using System.Text;
using Definitions;
using Framework;
 
namespace BusinessObjects
{
    public class EmployeeBO : BusinessLogic,IBusinessLogic
    {
        bool IBusiness.Create(int hashCode)
        {
            return true;
        }
 
        bool IBusiness.Update(int hashCode)
        {
            return true;
        }
 
        int IBusinessLogic.Read(int hashCode)
        {
            return -1;
        }
 
        int IBusiness.Read()
        {
            return -1;
        }
 
        bool IBusiness.Delete(int hashCode)
        {
            return true;
        }
    }
}

Listing 10

using System;
using Definitions;
using Framework;
 
namespace DataObjects
{
    [Serializable]
    public class EmployeeDO : BusinessEntity
    {
        private string _name;
        private string _address;
 
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }
 
        public string Address
        {
            get { return _address; }
            set { _address = value; }
        }
    }
}

View Entire Article

User Comments

No comments posted yet.






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-05-13 1:47:42 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search