结构图构造函数注入工作单元始终为空

发布于 2024-11-07 10:46:18 字数 1972 浏览 0 评论 0原文

我试图将我的工作单元传递到我的通用基础存储库中,但是当我尝试调用某些方法时,工作单元不会传递到基础存储库中。

场景:我将下面的 userRepository 注入到我的 UserController 中一切正常,当它调用 userRepository.Save(user) 时,由于工作单元为空而失败。我不确定为什么?

我正在使用 nhibernate 和 Structuremap。我认为我已经正确连接了所有内容,但这里有一些需要仔细检查的代码:

这是基本存储库:

public class BaseRepository<T> : IRepository<T> where T : IAggregateRoot
{
  private readonly IUnitOfWork _unitOfWork;

  public BaseRepository(IUnitOfWork unitOfWork)
  {
    _unitOfWork = unitOfWork; 
  } 

  public BaseRepository()
  {}

  public void Save(T Entity)
  {
    _unitOfWork.Session.Save(Entity);
  }
}

特定存储库:

  public class UserRepository : BaseRepository<User>, IUserRepository
  {
  }

这是我的 nhibernate 结构图配置:

public NhibernateRegistry()
{
  For<IUnitOfWork>().HybridHttpOrThreadLocalScoped().Use<UnitOfWork>();

  For(typeof(IRepository<>)).Use(typeof(BaseRepository<>));

  // Nhibernate Session 
  For<ISession>().HybridHttpOrThreadLocalScoped().Use(context => context.GetInstance<ISessionFactory>().OpenSession());

  // Nhibernate SessionFactory
  For<ISessionFactory>().Singleton().Use(NhibernateHelper.CreateSessionFactory());
}`

这是我的 nhibernate http 模块:

public class NHibernateModule : IHttpModule
{
  private IUnitOfWork _unitOfWork;

  public void Init(HttpApplication context)
  {
    context.BeginRequest += ContextBeginRequest;
    context.EndRequest += ContextEndRequest;
  }

  private void ContextBeginRequest(object sender, EventArgs e)
  {
    _unitOfWork = ObjectFactory.GetInstance<IUnitOfWork>();
  }

  private void ContextEndRequest(object sender, EventArgs e)
  {
    try { _unitOfWork.Commit();  }
    catch { _unitOfWork.Rollback();  }
    finally { Dispose(); }
  }

  public void Dispose()
  {
    if (_unitOfWork != null)
      _unitOfWork.Dispose();
  }

}

I am trying to pass my unitofwork into my generic base repository, but when i try and call some of the methods the unitofwork isnt being passed into the base repository.

The scenario: I inject the userRepository below into my UserController all fine, its when it calls the userRepository.Save(user) it fails due to the unitofwork being null. Im not sure why though?

Im using nhibernate and structuremap. I think ive wired everything up correctly but here is some code to double check:

Here is the base repository:

public class BaseRepository<T> : IRepository<T> where T : IAggregateRoot
{
  private readonly IUnitOfWork _unitOfWork;

  public BaseRepository(IUnitOfWork unitOfWork)
  {
    _unitOfWork = unitOfWork; 
  } 

  public BaseRepository()
  {}

  public void Save(T Entity)
  {
    _unitOfWork.Session.Save(Entity);
  }
}

A specific repository:

  public class UserRepository : BaseRepository<User>, IUserRepository
  {
  }

This is my nhibernate structuremap configuration:

public NhibernateRegistry()
{
  For<IUnitOfWork>().HybridHttpOrThreadLocalScoped().Use<UnitOfWork>();

  For(typeof(IRepository<>)).Use(typeof(BaseRepository<>));

  // Nhibernate Session 
  For<ISession>().HybridHttpOrThreadLocalScoped().Use(context => context.GetInstance<ISessionFactory>().OpenSession());

  // Nhibernate SessionFactory
  For<ISessionFactory>().Singleton().Use(NhibernateHelper.CreateSessionFactory());
}`

Here is my nhibernate http module:

public class NHibernateModule : IHttpModule
{
  private IUnitOfWork _unitOfWork;

  public void Init(HttpApplication context)
  {
    context.BeginRequest += ContextBeginRequest;
    context.EndRequest += ContextEndRequest;
  }

  private void ContextBeginRequest(object sender, EventArgs e)
  {
    _unitOfWork = ObjectFactory.GetInstance<IUnitOfWork>();
  }

  private void ContextEndRequest(object sender, EventArgs e)
  {
    try { _unitOfWork.Commit();  }
    catch { _unitOfWork.Rollback();  }
    finally { Dispose(); }
  }

  public void Dispose()
  {
    if (_unitOfWork != null)
      _unitOfWork.Dispose();
  }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦在深巷 2024-11-14 10:46:18

UserRepository 需要一个构造函数来接收 IUnitOfWork 并将其传递给 BaseRepository 构造函数。目前,UserRepository 使用的是 BaseRepository 的无参数构造函数,因此没有注入 IUnitOfWork。摆脱无参数构造函数,并确保所有派生类型将 IUnitOfWork 传递给基类。

UserRepository needs a constructor that takes in the IUnitOfWork and passes it to the BaseRepository constructor. Currently, UserRepository is using the parameterless constructor of BaseRepository, so no IUnitOfWork is injected. Get rid of the parameterless constructor, and make sure all derived types pass the IUnitOfWork to the base.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文