如何在NHibernate 3.2中实现通用存储库模式和UoW

age*_*t47 4 nhibernate unit-of-work repository-pattern c#-4.0 asp.net-mvc-3

我是新手NHibernate,我正在尝试阻止Generic Repository PatternUnit of WorkASP.NET MVC 3应用程序中使用.我用Google搜索了标题并找到了新的链接; 但是所有这些对我来说都更加复杂.我使用StructureMap作为我的IOC.你能建议我一些链接或博客文章吗?

Jes*_*sse 5

这里有几个要阅读的项目:

我在最近的项目中使用的实现看起来像:

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();
    }
}
Run Code Online (Sandbox Code Playgroud)