将我的 UnitOfWork 注入到我的存储库构造函数中

发布于 2024-10-19 01:04:43 字数 1732 浏览 2 评论 0原文

我很新,但正在寻求学习 nhibernate 和 DI 与结构图,所以我正在制作一个应用程序来测试一切。

我在将工作单元注入基础存储库时遇到一些问题。我对每个请求创建一个工作单元,然后在最后提交或回滚。但是,当尝试注入我的基础存储库时,当我在我的方法中使用它时,它总是为空。更具体地说,我收到此错误:“对象引用未设置为对象的实例”。有什么想法我做错了吗?如果这是我应该走的正确路线?

非常感谢。

基本存储库:

public abstract class Repository<T> : IRepository<T>
{
  public readonly IUnitOfWork _unitOfWork;

  public Repository(IUnitOfWork UnitOfWork)
  {
    UnitOfWork = _unitOfWork;
  }

  public Repository() {}

  public void Save(T obj)
  {
    _unitOfWork.CurrentSession.Save(obj);
  }
}

在我的 application_start 中设置的 StructureMap 注册表:

 public class NHibernateRegistry : Registry
 {
   public NHibernateRegistry()
   {
     For<IUnitOfWork>().HybridHttpOrThreadLocalScoped().Use<UnitOfWork>();

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

     For<ISessionFactory>().Singleton().Use(NHibernateSessionFactory.GetSessionFactory());
   }
 }

更新:

基本存储库由特定存储库(即 ArticleRepository)继承,然后我将其注入到 application_start 上的 aspx 页面中。然后我的 aspx 页面继承了一个进行构建的基础页面。我像这样访问存储库:

public IArticleRepository ArticleRepo { get; set; }

public void SaveThing()
{
    ArticleRepo.Save(object);
}

这在 application_start 上被调用:

  public class Bootstrapper
  {
    public static void BootStrap()
    {
      ObjectFactory.Configure(x =>
      {
        x.AddRegistry<NHibernateRegistry>();
        x.AddRegistry<WebRegistry>();
      });
    }
  }

在 webregistry 中:

For<IArticleRepository>().Use<ArticleRepository>();

Im very new but on a quest to learn nhibernate and DI with structuremap so am making an app to test everything out.

I am having some problems injecting my unitofwork into my base repository. I create a unitofwork on every request then either commit or rollback at the end. But when tryin to inject into my base repository its always null when i go to use it in my methods. More specifically i get this error: "Object reference not set to an instance of an object". Any ideas what im doing wrong and if this is the correct route i should be going?

Many thanks in advance.

Base Repository:

public abstract class Repository<T> : IRepository<T>
{
  public readonly IUnitOfWork _unitOfWork;

  public Repository(IUnitOfWork UnitOfWork)
  {
    UnitOfWork = _unitOfWork;
  }

  public Repository() {}

  public void Save(T obj)
  {
    _unitOfWork.CurrentSession.Save(obj);
  }
}

StructureMap registry thats set in my application_start:

 public class NHibernateRegistry : Registry
 {
   public NHibernateRegistry()
   {
     For<IUnitOfWork>().HybridHttpOrThreadLocalScoped().Use<UnitOfWork>();

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

     For<ISessionFactory>().Singleton().Use(NHibernateSessionFactory.GetSessionFactory());
   }
 }

Update:

The baserepository is inherited by specific repositorys ie ArticleRepository then im injecting that into my aspx pages on application_start. Then my aspx page inherits a basepage where the buildUp takes place. And i access the repository like this:

public IArticleRepository ArticleRepo { get; set; }

public void SaveThing()
{
    ArticleRepo.Save(object);
}

This gets called on application_start:

  public class Bootstrapper
  {
    public static void BootStrap()
    {
      ObjectFactory.Configure(x =>
      {
        x.AddRegistry<NHibernateRegistry>();
        x.AddRegistry<WebRegistry>();
      });
    }
  }

And in the webregistry:

For<IArticleRepository>().Use<ArticleRepository>();

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

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

发布评论

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

评论(2

雨夜星沙 2024-10-26 01:04:43

您的 ArticleRepository 很可能没有采用 IUnitOfWork 作为参数的构造函数,请确保您具有以下

public class ArticleRepository : Repository<Article>
{
    public ArticleRepository(IUnitOfWork unitOfWork) : base(unitOfWork)
    {
    }
...
}

记住结构映射使用类上最贪婪的构造函数它实例化的。由于您的抽象存储库类永远不会被实例化,因此它不会使用其构造函数。如果您的类需要以这种方式注入依赖项,我还建议不要使用任何默认构造函数。这样出错的机会就更少了。

There is a good chance your ArticleRepository doesn't have a constructor that takes an IUnitOfWork as a parameter make sure you have the following

public class ArticleRepository : Repository<Article>
{
    public ArticleRepository(IUnitOfWork unitOfWork) : base(unitOfWork)
    {
    }
...
}

Remember Strcuture map uses the greediest constructor on the class that it instantiates. Since your abstract repository class will never be instantiated it will not use its constructors. I would also suggest not having any default constructors if your classes need to have their dependencies injected that way. Less chance for errors that way.

花间憩 2024-10-26 01:04:43
  public readonly IUnitOfWork _unitOfWork;

  public Repository(IUnitOfWork UnitOfWork)
  {
    UnitOfWork = _unitOfWork;
  }

我认为你的意思是写

  public readonly IUnitOfWork _unitOfWork;

  public Repository(IUnitOfWork UnitOfWork)
  {
    _unitOfWork = UnitOfWork;
  }

你正在将局部变量分配给参数而不是参数给局部变量。

编辑:您还应该用小写的 U 'unitOfWork' 而不是 'UnitOfWork' 编写参数

  public readonly IUnitOfWork _unitOfWork;

  public Repository(IUnitOfWork UnitOfWork)
  {
    UnitOfWork = _unitOfWork;
  }

I think you mean to write

  public readonly IUnitOfWork _unitOfWork;

  public Repository(IUnitOfWork UnitOfWork)
  {
    _unitOfWork = UnitOfWork;
  }

You were assigning the local variable to the parameter instead of the parameter to the local variable.

Edit: Also you should write your parameter with a lowercase U 'unitOfWork' instead of 'UnitOfWork'

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