我像Dependecy Resolver一样使用Ado.net和IoC-Container Ninject实现存储库模式。但是,现在我有两次豁免权可以实施这种依赖性。 1种方式-我得到了很好的结果,2种方式-错误交易。像这样: System.Data.SqlClient.SqlTransaction.ZombieCheck() System.Data.SqlClient.SqlTransaction.Commit()
public class BaseController : Controller
{
public IUnitOfWork UnitOfWork { get; set; }
public BaseController()
{
IKernel ninjectKernel = new StandardKernel();
ninjectKernel.Bind<IUnitOfWork>().To<AdoNetUnitOfWork>().InRequestScope().WithConstructorArgument("context", new DbContext
(new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString)));
UnitOfWork = ninjectKernel.Get<IUnitOfWork>();
}
} // This is first way.
public class BaseController : Controller
{
[Inject]
public IUnitOfWork UnitOfWork { get; set; }
//In Global.asax
NinjectModule registrations = new NinjectRegistration();
var kernel = new StandardKernel(registrations);
DependencyResolver.SetResolver(new NinjectDependencyResolver(kernel));
public class NinjectRegistration : NinjectModule
{
public override void Load()
{
Bind<IUnitOfWork>().To<AdoNetUnitOfWork>().InRequestScope()
.WithConstructorArgument("context", new DbContext
(new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString)));
//Unbind<ModelValidatorProvider>();
//Rebind<ModelValidatorProvider>().To<AttributeValidatorProvider>();
}
}
// This second way