面向接口开发的一个架构(三)
讲述一个基于接口开发的架构模式,业务逻辑层
业务逻辑层
一、IBusinessFacade:定义业务逻辑接口
IBusinessBase.cs
1
namespace IBusinessFacade2


{3
public interface IBusinessBase4

{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![]()
![]()
{
3
public interface ILog : IBusinessBase
4![]()
{
5
6
}
7
}
namespace IBusinessFacade2


{3
public interface ILog : IBusinessBase4

{5
6
}7
}
二、BusinessLogic:业务实现层
1、BaseBuiness.cs
1
namespace BusinessLogic2


{3
public class BaseBusiness : IBusinessBase4

{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()14

{15
assembly = null;16
obj = null;17
}18

19
public BaseBusiness(string businessType)20

{21
Init(businessType);22
}23

24

IBusinessBase Members#region IBusinessBase Members25

26
public void Init(string businessType)27

{28
if (string.IsNullOrEmpty(businessType))29

{30
throw new Exception("BusinessType should not be empty!");31
}32
BusinessType = businessType;33
assembly = Assembly.Load("DataAccess");34
if (assembly == null)35

{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)41

{42
throw new Exception(FAILED_CREATE_CLASS);43
}44
}45

46
public DataSet Get()47

{48

MethodInfo getMethod = assembly.GetType(BusinessType).GetMethod("Get",new Type[]
{});49
Object ret = getMethod.Invoke(obj,null);50
if (ret == null)51

{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)59

{60

MethodInfo getMethod = assembly.GetType(BusinessType).GetMethod("Get",new Type[]
{typeof(int)});61

Object ret = getMethod.Invoke(obj, new object[]
{id});62
if (ret == null)63

{64
throw new Exception(FAILED_ACCESS_STR);65
}66
DataSet ds = (DataSet)ret;67
return ds;68
}69

70
public void Add(DataSet ds)71

{72
MethodInfo getMethod = assembly.GetType(BusinessType).GetMethod("Add");73

Object ret = getMethod.Invoke(obj, new object[]
{ds});74
if (ret == null)75

{76
throw new Exception(FAILED_ACCESS_STR);77
}78
int result = (int)ret;79
if (result == 0)80

{81
throw new Exception("no record has effected");82
}83
}84

85
public void Delete(int id)86

{87
MethodInfo getMethod = assembly.GetType(BusinessType).GetMethod("Delete");88

Object ret = getMethod.Invoke(obj, new object[]
{ id });89
if (ret == null)90

{91
throw new Exception(FAILED_ACCESS_STR);92
}93
}94

95
public void Update(DataSet ds)96

{97
MethodInfo getMethod = assembly.GetType(BusinessType).GetMethod("Update");98

Object ret = getMethod.Invoke(obj, new object[]
{ ds });99
if (ret == null)100

{101
throw new Exception(FAILED_ACCESS_STR);102
}103
}104

105
#endregion106
}107
}
2、LogBusiness.cs
1
namespace BusinessLogic
2![]()
![]()
{
3
public class LogBusiness :BaseBusiness, ILog
4![]()
{
5
6
}
7
}
namespace BusinessLogic2


{3
public class LogBusiness :BaseBusiness, ILog4

{5
6
}7
}
ILog和LogBusiness类都是空的,是用来将来方法扩展使用。

浙公网安备 33010602011771号