gpt4 book ai didi

asp.net-mvc-3 - 如何在 NHibernate 3.2 中实现通用存储库模式和 UoW

转载 作者:行者123 更新时间:2023-12-05 00:35:20 26 4
gpt4 key购买 nike

我是新来的 NHibernate我正在尝试为在 ASP.NET MVC 3 应用程序中使用引入通用存储库模式和工作单元。我用谷歌搜索标题并找到了新链接;但对我来说,所有这些都更复杂。我使用 StructureMap 作为我的 IOC。你能给我推荐一些链接或博客文章吗?

最佳答案

这里有几个项目可以通读:

  • Advantage of creating a generic repository vs. specific repository for each object?
  • How to make a Generic Repository?

  • 我在最近的项目中使用的实现如下所示:
    public interface IRepository<T>
    {
    IEnumerable<T> GetAll();
    T GetByID(int id);
    T GetByID(Guid key);
    void Save(T entity);
    void Delete(T entity);
    }

    public class Repository<T> : IRepository<T>
    {
    protected readonly ISession Session;

    public Repository(ISession session)
    {
    Session = session;
    }

    public IEnumerable<T> GetAll()
    {
    return Session.Query<T>();
    }

    public T GetByID(int id)
    {
    return Session.Get<T>(id);
    }

    public T GetByID(Guid key)
    {
    return Session.Get<T>(key);
    }

    public void Save(T entity)
    {
    Session.Save(entity);
    Session.Flush();
    }

    public void Delete(T entity)
    {
    Session.Delete(entity);
    Session.Flush();
    }
    }

    关于asp.net-mvc-3 - 如何在 NHibernate 3.2 中实现通用存储库模式和 UoW,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9556871/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com