Analysis of "Suteki. Shop" in Asp.net MVC sample project-nhib.pdf

Source: Internet
Author: User

In Suteki. Shop, the built-in ORM function based on the NHibernate method is provided, but the current project progress is not complete yet.
The Department completed its design concept, and there was no complete test in the unit test. However, the design idea is still very interesting.
 
In terms of introducing NHibernate into the project implementation method, it is similar to the method advocated in Rhino. Commons.
It is based on the IRepository mode. The definition of the IRepository interface has been mentioned in the previous article and has been explained accordingly.
So I will not explain it here.

Now, let's start the text of today.

First, let's take a look at this class diagram:




The class NHibernateRepository on the left is based on the basic implementation of IRepository, that is, the so-called CRUD operation.
The specific implementation code is as follows:

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


In the constructor of this class, the ISessionManager instance is received as its session manager. In castle, "NHibernateIntegration" is provided"
NHibernate is integrated to encapsulate sessions and transactions. Therefore, you can use the ISessionManager
The data object to perform the so-called CRUD operation. Because nhib.pdf. Linq. dll is used here to replace Linq to SQL
The code is similar to that of Linq.

Next let's take a look at nhibernateentityperesolver (Suteki. Common \ Services \ nhibernateentityperesolver. cs ),
The function is to map the specified object class in the specified Model to the Entity in nhib.pdf. The following is the specific implementation:

Public class NHibernateEntityTypeResolver: IEntityTypeResolver
{
Public Type GetTypeOf (IEntity entity)
{
Return NHibernateProxyHelper. GuessClass (entity );
}

Public Type GetRealTypeOf (Type type)
{
Var nhibernateProxyInterface = type. GetInterface ("INHibernateProxy ");
If (nhibernateProxyInterface = null)
{
Return type;
}

Return type. BaseType;
}
}

With these two classes, you can use them in the project. However, at present, Suteki. Shop has not implemented the NHibernate mode ORM, so
No unit tests have been conducted. However, the author provides an example in his BLOG to test and run NHibernateRepository.
.

The following example shows the specific test code. First download and decompress the ZIP file for this example, and modify the config file.
"Keith. windsorhnib.pdf. Tests. dll. Windsor. config", find the data connection string and replace it with our local database.
Link.

Let's take a look at the test code below:

[TestFixture]
Public class RepositoryTests: WindsorNHibernateContainerTest
{
Private IRepository <Customer> customerRepository;

[SetUp]
Public void SetUp ()
{
CustomerRepository = GetComponet <IRepository <Customer> ();
}

[Test]
Public void Customer_RepositoryShouldNotBeNull ()
{
Var customers = customerRepository. GetAll ();

Foreach (var customer in mers)
{
Console. WriteLine ("Customer: {0}", customer. ContactName );

Foreach (var order in customer. Orders)
{
Console. WriteLine ("\ tOrder: {0}", order. OrderID );
}
}
Assert. That (customers, Is. Not. Null );
}

In this example, the connected database is Northwind, and this unit test is to obtain all "customer information" in the database ".

Note: customerRepository is of the IRepository <Customer> type, and the final binding type is defined above.
(Implemented by IOC binding ).

We believe that Suteki. Shop will improve the code in the future and provide users with at least two data access solutions:
One is Linq To SQL, and the other is nhib.pdf.

Well, today's content is here.

Author: daizhj, Dai zhenjun, LaoD

 

URL: http://daizhj.cnblogs.com/

 

Related Article

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.