The Suteki.shop provides a nhibernate-based ORM capability, but does not complete its design-time vision for the current project schedule, nor does it have complete testing on unit tests. But the idea of the design is still very interesting.
The way in which NHibernate is introduced into the project is the same as advocated in Rhino.commons, which is based on IRepository mode. The definition of IRepository this interface has been mentioned and explained in the previous article, so there is no more explanation.
OK, let's start today's text.
Let's take a look at this class diagram first:
The class nhibernaterepository on the left is based on the IRepository basic implementation, known as CRUD operations.
Here's how to implement the code:
public class nhibernaterepository<t>: Irepository<t>, irepository where T:class
{
Private ReadOnly Isessionmanager SessionManager;
Public nhibernaterepository (Isessionmanager SessionManager)
{
This.sessionmanager = SessionManager;
}
Private ISession Session
{
Get
{
return Sessionmanager.opensession ();
}
}
Public T GetByID (int id)
{
return session.load<t> (ID);
}
Public iqueryable<t> GetAll ()
{
return session.linq<t> ();
}
public void InsertOnSubmit (T entity)
{
Session.save (entity);
}
public void DeleteOnSubmit (T entity)
{
Session.delete (entity);
}
public void SubmitChanges ()
{
Session.flush ();
}
Object Irepository.getbyid (int id)
{
return GetByID (ID);
}
IQueryable Irepository.getall ()
{
return GetAll ();
}
void Irepository.insertonsubmit (Object entity)
{
InsertOnSubmit ((T) entity);
}
void Irepository.deleteonsubmit (Object entity)
{
DeleteOnSubmit ((T) entity);
}
}