asp.net MVC example Project "Suteki.shop" Analysis of NHibernate

Source: Internet
Author: User

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&gt, 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);
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.