面向接口开发的一个架构(三)

业务逻辑层

一、IBusinessFacade:定义业务逻辑接口

IBusinessBase.cs

ContractedBlock.gif ExpandedBlockStart.gif IBusinessBase
 1namespace IBusinessFacade
 2ExpandedBlockStart.gifContractedBlock.gif{
 3    public interface IBusinessBase
 4ExpandedSubBlockStart.gifContractedSubBlock.gif    {
 5        void Init(string type);
 6        DataSet Get();
 7        DataSet Get(int id);
 8        void Add(DataSet ds);
 9        void Add(string[] temp);
10        void Delete(int id);
11        void Update(DataSet ds);
12    }

13}

 

ILog.cs

1 namespace  IBusinessFacade
2 ExpandedBlockStart.gifContractedBlock.gif {
3    public interface ILog : IBusinessBase
4ExpandedSubBlockStart.gifContractedSubBlock.gif    {
5        
6    }

7}

 

二、BusinessLogic:业务实现层

1、BaseBuiness.cs

ContractedBlock.gif ExpandedBlockStart.gif IBusinessBase
  1namespace BusinessLogic
  2ExpandedBlockStart.gifContractedBlock.gif{
  3    public class BaseBusiness : IBusinessBase
  4ExpandedSubBlockStart.gifContractedSubBlock.gif    {
  5        public string BusinessType = string.Empty;
  6
  7        private Assembly assembly;
  8        private Object obj;
  9        private const string FAILED_LOAD_ASSEMBLY = "Can't load assembly.";
 10        private const string FAILED_CREATE_CLASS = "Can't create class";
 11        private const string FAILED_ACCESS_STR = "Failed access DAL. return null value.please checked BusinessType or make sure DataAccess assembly in your application folder.";
 12
 13        public BaseBusiness()
 14ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 15            assembly = null;
 16            obj = null;
 17        }

 18
 19        public BaseBusiness(string businessType)
 20ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 21            Init(businessType);
 22        }

 23
 24ContractedSubBlock.gifExpandedSubBlockStart.gif        IBusinessBase Members#region IBusinessBase Members
 25
 26        public void Init(string businessType)
 27ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 28            if (string.IsNullOrEmpty(businessType))
 29ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 30                throw new Exception("BusinessType should not be empty!");
 31            }

 32            BusinessType = businessType;
 33            assembly = Assembly.Load("DataAccess");
 34            if (assembly == null)
 35ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 36                throw new Exception(FAILED_LOAD_ASSEMBLY);
 37            }

 38            BusinessType = string.Format("DataAccess.{0}DA",businessType);
 39            obj = assembly.CreateInstance(BusinessType);
 40            if (obj == null)
 41ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 42                throw new Exception(FAILED_CREATE_CLASS);
 43            }

 44        }

 45
 46        public DataSet Get()
 47ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 48ExpandedSubBlockStart.gifContractedSubBlock.gif            MethodInfo getMethod = assembly.GetType(BusinessType).GetMethod("Get",new Type[]{});
 49            Object ret = getMethod.Invoke(obj,null);
 50            if (ret == null)
 51ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 52                throw new Exception(FAILED_ACCESS_STR);
 53            }

 54            DataSet ds = (DataSet)ret;
 55            return ds;
 56        }

 57
 58        public System.Data.DataSet Get(int id)
 59ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 60ExpandedSubBlockStart.gifContractedSubBlock.gif            MethodInfo getMethod = assembly.GetType(BusinessType).GetMethod("Get",new Type[]{typeof(int)});
 61ExpandedSubBlockStart.gifContractedSubBlock.gif            Object ret = getMethod.Invoke(obj, new object[]{id});
 62            if (ret == null)
 63ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 64                throw new Exception(FAILED_ACCESS_STR);
 65            }

 66            DataSet ds = (DataSet)ret;
 67            return ds;
 68        }

 69
 70        public void Add(DataSet ds)
 71ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 72            MethodInfo getMethod = assembly.GetType(BusinessType).GetMethod("Add");
 73ExpandedSubBlockStart.gifContractedSubBlock.gif            Object ret = getMethod.Invoke(obj, new object[]{ds});
 74            if (ret == null)
 75ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 76                throw new Exception(FAILED_ACCESS_STR);
 77            }

 78            int result = (int)ret;
 79            if (result == 0)
 80ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 81                throw new Exception("no record has effected");
 82            }

 83        }

 84
 85        public void Delete(int id)
 86ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 87            MethodInfo getMethod = assembly.GetType(BusinessType).GetMethod("Delete");
 88ExpandedSubBlockStart.gifContractedSubBlock.gif            Object ret = getMethod.Invoke(obj, new object[] { id });
 89            if (ret == null)
 90ExpandedSubBlockStart.gifContractedSubBlock.gif            {
 91                throw new Exception(FAILED_ACCESS_STR);
 92            }

 93        }

 94
 95        public void Update(DataSet ds)
 96ExpandedSubBlockStart.gifContractedSubBlock.gif        {
 97            MethodInfo getMethod = assembly.GetType(BusinessType).GetMethod("Update");
 98ExpandedSubBlockStart.gifContractedSubBlock.gif            Object ret = getMethod.Invoke(obj, new object[] { ds });
 99            if (ret == null)
100ExpandedSubBlockStart.gifContractedSubBlock.gif            {
101                throw new Exception(FAILED_ACCESS_STR);
102            }

103        }

104
105        #endregion

106    }

107}

 

2、LogBusiness.cs

1 namespace  BusinessLogic
2 ExpandedBlockStart.gifContractedBlock.gif {
3    public class LogBusiness :BaseBusiness, ILog
4ExpandedSubBlockStart.gifContractedSubBlock.gif    {
5       
6    }

7}

 

ILog和LogBusiness类都是空的,是用来将来方法扩展使用。

转载于:https://www.cnblogs.com/disappearwind/archive/2009/10/20/1586877.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值