Search code examples
entity-frameworknhibernaterepositoryrepository-patternspecification-pattern

EF repository implementation to NHib


Below is an implementation for a generic repository desgned for use with Entity Framework. Ignoring its 'goodness' or 'badness' for this exercise...

What would the code look like for this in NHibernate?

Cheers,
Berryl

public class Repository<T> : IRepository<T> 
where T : class
{
protected ObjectContext Context;
protected ObjectSet<T> QueryBase;

public Repository(ObjectContext context)
{
    Context = context;
    QueryBase = context.CreateObjectSet<T>();
}

public IEnumerable<T> Matches(ICriteria<T> criteria)
{
    var query = criteria.BuildQueryFrom(QueryBase);
    return query.ToList();
}

public void Save(T entity)
{
    QueryBase.AddObject(entity);
}

NOTE

This is NOT a question about how to design a repository or whether to use one or not. Just a straight forward translation from EF to NHib...


Solution

  • the Nhib version of a repository would be an ISession object.
    see this famous post from ayende.