加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

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

发布时间:2020-12-16 06:47:01 所属栏目:asp.Net 来源:网络整理
导读:我是NHibernate的新手,我正在尝试阻止Generic Repository Pattern和Unit of Work在ASP.NET MVC 3应用程序中使用.我用Google搜索了标题并找到了新的链接;但是所有这些对我来说都更加复杂.我使用StructureMap作为我的IOC.你能建议我一些链接或博客文章吗? 解
我是NHibernate的新手,我正在尝试阻止Generic Repository Pattern和Unit of Work在ASP.NET MVC 3应用程序中使用.我用Google搜索了标题并找到了新的链接;但是所有这些对我来说都更加复杂.我使用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();
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读