About Repository pattern and Nhibernate

I am working on a new project for an intranet application. I am using NHibernate and DDD. I created a simple Repository structure...It user Windsor IOC container for instance repository (of course you can implement your with LINQToSQL or a Fake repository for testing). This is interface and implementation:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Jobbing.DAL.Repository
{

//Repository Interface
public interface IRepository<T>
{
void Save(T obj);
void Update(T obj);
void Delete(T obj);
T Load<T>(object id);
T GetReference<T>(object id);
void DeleteAll(IList<T> objs);
void UpdateAll(IList<T> objs);
void InsertAll(IList<T> objs);
IList<T> GetAll<T>();
IList<T> GetAllOrdered<T>(string propertyName,bool Ascending);
IList<T> Find<T>(IList<string> criteria);
void Detach(T item);
IList<T> GetAll<T>(int pageIndex, int pageSize);
void Commit();
void Rollback();
void BeginTransaction();
}
}

//Implementation of NHibernate Repository

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using NHibernate;
using NHibernate.Expression;
using NHibernate.Engine.Query;
using NHibernate.Mapping;

namespace Jobbing.DAL.Repository
{
public class NHibernateRepository<T> : IRepository<T>
{
private ISession session;

public NHibernateRepository()
{
//session = NHibernateHelper.GetCurrentSession();
session = NHibernateSessionManager.Instance.GetSession();
session.BeginTransaction();
}

#region IRepository<T> Members

public void Save(T obj)
{
session.Save(obj);
}

public void Update(T obj)
{
session.Update(obj);
}

public void Delete(T obj)
{
session.Delete(obj);
}

public T Load<T>(object id)
{
return session.Load<T>(id);
}

public T GetReference<T>(object id)
{
return session.Get<T>(id);
}

public void DeleteAll(IList<T> objs)
{
for (Int32 I = 0; I < objs.Count; ++I)
{
Delete(objsIdea);
}
}

public void UpdateAll(IList<T> objs)
{
for (Int32 I = 0; I < objs.Count; ++I)
{
Update(objsIdea);
}
}

public void InsertAll(IList<T> objs)
{
for (Int32 I = 0; I < objs.Count; ++I)
{
Save(objsIdea);
}
}

public void Detach(T item)
{
session.Evict(item);
}

internal void Flush()
{
session.Flush();
}

public IList<T> GetAll<T>(int pageIndex, int pageSize)
{
ICriteria criteria = session.CreateCriteria(typeof(T));
criteria.SetFirstResult(pageIndex * pageSize);
if (pageSize > 0)
{
criteria.SetMaxResults(pageSize);
}
return criteria.List<T>();
}

public IList<T> GetAll<T>()
{
return GetAll<T>(0, 0);
}

public IList<T> Find<T>(IList<string> strs)
{
System.Collections.Generic.IList<ICriterion> objs = new System.Collections.Generic.List<ICriterion>();
foreach(string s in strs){
ICriterion cr1 = Expression.Sql(s);
objs.Add(cr1);
}
ICriteria criteria = session.CreateCriteria(typeof(T));
foreach (ICriterion rest in objs)
session.CreateCriteria(typeof(T)).Add(rest);

criteria.SetFirstResult(0);
return criteria.List<T>();
}

//public IList<T> GetByCriteria<T>(IList<ICriterion> objs, int pageIndex, int pageSize)
//{

// ICriteria criteria = session.CreateCriteria(typeof(T));
// foreach (ICriterion rest in objs)
// session.CreateCriteria(typeof(T)).Add(rest);

// criteria.SetFirstResult(pageIndex * pageSize);
// if (pageSize > 0)
// {
// criteria.SetMaxResults(pageSize);
// }
// return criteria.List<T>();
//}

//public IList<T> GetByCriteria<T>(IList<ICriterion> objs)
//{

// return this.GetByCriteria<T>(objs, 0, 0);
//}

public void Commit()
{
if (session.Transaction.IsActive)
{
session.Transaction.Commit();
}
}

public void Rollback()
{
if (session.Transaction.IsActive)
{
session.Transaction.Rollback();
session.Clear();
}
}

public void BeginTransaction()
{
Rollback();
session.BeginTransaction();
}

#endregion

#region IRepository<T> Members


public IList<T> GetAllOrdered<T>(string propertyName, bool Ascending)
{
Order cr1 = new Order(propertyName, Ascending);
IList<T> objsResult = session.CreateCriteria(typeof(T)).AddOrder(cr1).List<T>();
return objsResult;

}

#endregion
}
}

On CodeProject you can find more info about helper for NHibernate and module for Open-Session-In-View.
Bye
Antonio
Published 25 October 2008 12:51 PM by antrad
Filed under:

Comments

# Dann said on 12 December, 2009 03:11 PM

Thanks for that. It was useful to see how the GetAll worked.

BTW you spelt NHibernate wrong in the article (NHinernate)

This site

Search

Go

This Blog

Syndication

Sponsors

  • MaximumASP