我在我的项目(ASP.NET MVC4)中使用DapperWrapper,它使用Ninject作为依赖注入框架。除了TransactionScope之外,所有东西(绑定SqlExecutor和SqlExecutorFactory)似乎都有效。这是我的Ninject配置的一部分:
kernel.Bind<IDbExecutor>().ToMethod(context =>
{
var sqlConnection = new SqlConnection(_connectionString);
sqlConnection.Open();
return new SqlExecutor(sqlConnection);
}).InRequestScope();
kernel.Bind<IDbExecutorFactory>().ToMethod(context =>
{
return new SqlExecutorFactory(_connectionString);
}).InRequestScope();
kernel.Bind<ITransactionScope>().To<TransactionScopeWrapper>().InRequestScope();
我在Model Service类中使用TransactionScope块:
using (ITransactionScope scope = new TransactionScopeWrapper())
{
//MyDA class Method1
//MyDA class Method2
//MyDA class Method3
scope.Complete();
}
我所看到的是,即使稍后会抛出错误,早期DA类方法所做的更改也会被提交。
这里的MyDA课程骨架:
public sealed class MyDA
{
private readonly IDbExecutor dbExecutor;
//Constructor
public MyDA(IDbExecutorFactory dbExecutorFactory)
{
this.dbExecutor = dbExecutorFactory.CreateExecutor();
}
internal void Method1([input])
{
this.dbExecutor.Execute([Some stored procedure]);
}
}
有什么建议吗?