Automatic Transaction Management Facility 使用
需要引用的程序集:
- Castle.Facilities.AutomaticTransactionManagement
- Castle.Services.Transactions
- Castle.DynamicProxy
- Castle.Core
1、配置
WindsorContainer container = new WindsorContainer(store);
container.AddFacility( "auto.transaction", new TransactionFacility() );
container.AddFacility( "auto.transaction", new TransactionFacility() );
或
<facilities>
<facility id="transaction" type="Castle.Facilities.AutomaticTransactionManagement.TransactionFacility, Castle.Facilities.AutomaticTransactionManagement" />
</facilities>
<facility id="transaction" type="Castle.Facilities.AutomaticTransactionManagement.TransactionFacility, Castle.Facilities.AutomaticTransactionManagement" />
</facilities>
2、在代码中使用自动的事务管理
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using FES.Persistence;
using FES.Domain.Common;
using FES.Service.Interface;
using Castle.Facilities.IBatisNetIntegration;
using Castle.Services.Transaction;
using Castle.Facilities.AutomaticTransactionManagement;
namespace FES.Service.Implement
{
[Transactional]
[UsesAutomaticSessionCreation]
public class CommonService : ICommonService
{
private CommonSqlMapDao _common;
public CommonService(CommonSqlMapDao common)
{
_common = common;
}
[Transaction(TransactionMode.Requires)]
public bool LogIn(string empcode, string password, ref Employees emp)
{
emp = _common.GetEmployeeDetailByPassword(empcode, password);
if (emp == null)
{
return false;
}
else
{
return true;
}
}
[Transaction(TransactionMode.Requires)]
public void InsertEmpPages(EmpPages pages)
{
_common.DeleteEmpPages(pages.Eid);
_common.AddEmpPages(pages);
}
[Transaction(TransactionMode.Requires)]
public void InsertEmpPages(ArrayList al,int eid)
{
_common.DeleteEmpPages(eid);
for (int i = 0; i < al.Count; i++)
{
EmpPages pages = (EmpPages)al[i];
_common.AddEmpPages(pages);
}
}
}
}
using System.Collections;
using System.Collections.Generic;
using System.Text;
using FES.Persistence;
using FES.Domain.Common;
using FES.Service.Interface;
using Castle.Facilities.IBatisNetIntegration;
using Castle.Services.Transaction;
using Castle.Facilities.AutomaticTransactionManagement;
namespace FES.Service.Implement
{
[Transactional]
[UsesAutomaticSessionCreation]
public class CommonService : ICommonService
{
private CommonSqlMapDao _common;
public CommonService(CommonSqlMapDao common)
{
_common = common;
}
[Transaction(TransactionMode.Requires)]
public bool LogIn(string empcode, string password, ref Employees emp)
{
emp = _common.GetEmployeeDetailByPassword(empcode, password);
if (emp == null)
{
return false;
}
else
{
return true;
}
}
[Transaction(TransactionMode.Requires)]
public void InsertEmpPages(EmpPages pages)
{
_common.DeleteEmpPages(pages.Eid);
_common.AddEmpPages(pages);
}
[Transaction(TransactionMode.Requires)]
public void InsertEmpPages(ArrayList al,int eid)
{
_common.DeleteEmpPages(eid);
for (int i = 0; i < al.Count; i++)
{
EmpPages pages = (EmpPages)al[i];
_common.AddEmpPages(pages);
}
}
}
}
注意:如果你在容器注册使用自动事务管理的组件不是以一个接口来注册的,那么在需要自动事务管理的方法必须声明为:virtual。
还可以在容器注册组件的配置文件中来使用自动的事务管理:
<configuration>
<components>
<component
id="common"
service="FES.Service.Interface.ICommonService, FES.Service"
type="FES.Service.Implement.CommonService, FES.Service" isTransactional="true">
<transaction>
<method name="LogIn" />
<method name="InsertEmpPages" />
</transaction>
</component>
</components>
</configuration>
<components>
<component
id="common"
service="FES.Service.Interface.ICommonService, FES.Service"
type="FES.Service.Implement.CommonService, FES.Service" isTransactional="true">
<transaction>
<method name="LogIn" />
<method name="InsertEmpPages" />
</transaction>
</component>
</components>
</configuration>
posted on 2007-01-24 11:31 Daniel Pang 阅读(2826) 评论(1) 编辑 收藏 举报