amiry jd
amiry jd

Reputation: 27615

how to implement generic repository pattern and UoW in NHibernate 3.2

I'm new to NHibernate and I'm trying to impelement Generic Repository Pattern and Unit of Work for using in an ASP.NET MVC 3 application. I googled the title and found new links; but all of them was more complexe to understanding by me. I use StructureMap as my IOC. Can you suggest me some links or blog posts please?

Upvotes: 4

Views: 4071

Answers (2)

shanabus
shanabus

Reputation: 13115

Check out this solution - https://bitbucket.org/cedricy/cygnus/overview

Its a simple implementation of a Repository pattern that we've used in our production MVC 1, 2, and 3 applications.

Of course, we've learned since then that we really appreciate having our queries run directly against ISession. You have more control over them that way. That and Ayende told us not too.

Thanks Cedric!

Upvotes: 1

Jesse
Jesse

Reputation: 8393

Here are a couple of items to read thru:

The implementation I have used in my most recent project looked like:

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();
    }
}

Upvotes: 5

Related Questions