Recently I wrote a series of unitofwork and repository ModesArticle, You can view these articles here...
Http://www.n-pei.com/blog/unitofwork-for-nhibernate
Http://www.n-pei.com/blog/unitofwork-for-nhibernate-part2-nic
Http://www.n-pei.com/blog/unitofwork-for-nhibernate-part3-nic
Http://www.n-pei.com/blog/repository_pattern_nhibernate
IOC tools are essential for us to focus more on system design .. So here I will use autofac, Asp.net MVC, and nhib.pdf to give a simple example.
First, create an Asp.net MVC Project (MVC2 ).
Add the previously created unitofwork and repository projects.
Next, add a productcontroller In the MVC project.
Add the nhib.pdf configuration file:
With the configuration file, we can create our install view.
First, create a table in the corresponding database through schema, and then insert several data records in the database .....
View:
Run install:
The above uses the unitofwork mode to insert records.
Next we will use repository to add a product record.
First, the Controller partCode:
View code:
Add a record. Try again,
Set breakpoints:
In addition, you can use unitoofwork sessions to query products or use repository to query prodct;
In the following article, I will combine autofac with repository optimization to make the code more concise and implement IOC.