DataRabbit 轻量的ORM框架(17)-- 使用DataRabbit的最佳实践

DataRabbit 轻量的数据访问框架(12)-- 将DataRabbit融入架构 一文中介绍了如何将DataRabbit与三层架构结合起来,但是,在实际的项目中,每一层是如何使用DataRabbit的了?本文将展示作者在项目中使用DataRabbit的标准做法。

我们以CompanyUser表和MemberShip表为例说明如下:

比如我们有个虚构的业务流程(仅用于示例)如下:根据UserID找到对应的MemberShip记录,然后根据MemberShip记录的UserState项的值来决定是否要删除CompanyUser表对应的用户记录。

对于此业务逻辑,涉及到两个BEM类和一个BL类,首先来看两个BEM:MembershipBEM和CompanyUserBEM。

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> public class MembershipBEM
{
private TransactionScopetransactionScope;

public MembershipBEM(TransactionScopescope)
{
this .transactionScope = scope;
}

public MembershipGetOne( string userID)
{
IOrmAccesser
< Membership > accesser = this .transactionScope.NewOrmAccesser < Membership > ();

return accesser.GetOne( new Filter(Membership._UserID,userID));
}
}

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->publicclassCompanyUserBEM
{
privateTransactionScopetransactionScope;

publicCompanyUserBEM(TransactionScopescope)
{
this.transactionScope=scope;
}

publicvoidDelete(stringuserID)
{
IOrmAccesser
<CompanyUser>companyUserOrmAcceser=this.transactionScope.NewOrmAccesser<CompanyUser>();
companyUserOrmAcceser.Delete(
newFilter(CompanyUser._UserID,userID));
}

}

大家可以看到:

(1)所有的BEM类的构造函数都接收一个TransactionScope类型作为参数,并将其保存在成员变量中。

(2)所有的数据库访问器实例,都是由TransactionScope相应的New操作产生(如NewOrmAccesser()方法创建一个ORM访问器)

(3)所有的数据库操作由TransactionScope产生的访问器来进行(关于更多的访问器,可参考本系列前面的文章)。

ok,针对目标业务逻辑,BEM层的动作就这么多。BL层将会把相关的BEM操作整合成一个业务流

我们来看针对示例业务逻辑的BL实现:

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> public class CompanyUserBL
{
#region TransactionScopeFactory
private TransactionScopeFactorytransactionScopeFactory;
public TransactionScopeFactoryTransactionScopeFactory
{
set {transactionScopeFactory = value;}
}
#endregion

public void DeleteIfInvalidUser( string userID)
{
using (TransactionScopescope = this .transactionScopeFactory.NewTransactionScope( false ))
{
MembershipBEMmembershipBEM
= new MembershipBEM(scope);
Membershipmember
= membershipBEM.GetOne(userID);
if (member.State == UserState.Invalid)
{
CompanyUserBEMcompanyUserBEM
= new CompanyUserBEM(scope);
companyUserBEM.Delete(userID);
}
scope.Commit();
}
}
}

大家看到:

(1)所有的BL都有一个注入属性——TransactionScopeFactory,表明目标业务流程将针对哪个数据库进行操作。

(2)在一个业务流程中,所有的BEM使用同一个TransactionScope实例,如此,便可以在一个Transaction中将相应的所有的BEM操作封装为一个事务支持的业务流。

(3)要启用Transaction,只需要将TransactionScopeFactory的NewTransactionScope()方法传入的参数改为true即可,如:

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->using(TransactionScopescope=this.transactionScopeFactory.NewTransactionScope(true))

{

//。。。。。。

}

(4)无论是否启用了Transaction,离开using的最后一句一定是scope.Commit();用以提交操作。

以上便是使用DataRabbit结合3层架构的最佳实践做法(我所知道的:))。

最后,简单说一下TransactionScopeFactory,实际上,我们只需要为每个数据库配置一个TransactionScopeFactory实例即可,BL类可以公用这一个实例。比如,我使用Spring配置一个TransactionScopeFactory实例:

<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--> < object name = " dataConfiguration " type = " DataRabbit.DataConfiguration,DataRabbit " >
< propertyname = " DataBaseType " value = " SqlServer " />
< propertyname = " DataBaseName " value = " MasterDb " />
< propertyname = " IP " value = " 192.168.0.2 " />
< propertyname = " User " value = " sa " />
< propertyname = " Password " value = " testpwd " />
</ object >

< object name = " transactionScopeFactory " type = " DataRabbit.Application.TransactionScopeFactory,DataRabbit.Application " init - method = " Initialize " >
< propertyname = " DataConfiguration " ref = " dataConfiguration " />
</ object >

示例的CompanyUserBL类的实例只需要注入该transactionScopeFactory即可。

转到:DataRabbit 轻量的数据访问框架 -- 序

你可能感兴趣的:(最佳实践)