Many people use spring in the blog Park. net and nhib.pdf, so I also want to learn about them. This is what I wrote about nhib.pdf and spring. net is a simple example. It only implements a simple function of adding information. I do not know whether the combination is unreasonable. I hope you will criticize it.
The general idea is:
1. Compile the object class person and the person ing file person. HBM. xml
2. Use nhib.pdf to generate the t_person table
3. Compile the ipersondao interface and use the persondao class to implement this interface.
4. Use spring. Net to add information
5. The test tool is a test tool that comes with resharper.
The framework of this example is as follows:
Steps:
1. Create a solution springnet_lesson1 and add the Lib folder, which contains
These DLL.
2. Add a domain class library project and compile the person class and ing file,CodeAs follows:
Person. CS
Using system; using system. collections. generic; using system. LINQ; using system. text; namespace domain {public class person {Public Virtual guid ID {Get; set;} Public Virtual string name {Get; Set ;}}}
Person. HBM. xml
<? XML version = "1.0" encoding = "UTF-8"?> <Hibernate-mapping xmlns = "urn: nhibernate-mapping-2.2 "assembly =" Domain "namespace =" Domain "> <class name =" person "table =" t_person "lazy =" true "> <ID name =" ID "type = "guid" column = "personid"> <generator class = "assigned"/> </ID> <property name = "name" type = "string"> <column name =" name "length =" 50 "/> </property> </class>
Set the attributes of the person. HBM. xml file to embedded resources,
3. Add the class library project Dao and compile the ipersondao interface and the persondao class:
Ipersondao. CS
Using system; using system. Collections. Generic; using system. LINQ; using system. Text; using domain; namespace Dao {public interface ipersondao {object save (person );}}
Persondao. CS
Using system; using system. collections. generic; using system. LINQ; using system. text; using domain; using Nhibernate. CFG; namespace Dao {public class persondao: ipersondao {private isessionfactory sessionfactory; Public persondao () {var CFG = new configuration (). configure ("config/hibernate. cfg. XML "); sessionfactory = cfg. buildsessionfactory ();} public object save (person) {using (isession session = sessionfactory. opensession () {var id = session. save (person); Session. flush (); Return ID ;}}}}
4. Add the class library test for testing.
There is a config folder under the class library, which contains the hibernate. cfg. xml file, which contains some information about the configuration database.
<? XML version = "1.0" encoding = "UTF-8"?> <Hibernate-configuration xmlns = "urn: nhibernate-configuration-2.2"> <session-factory name = "Domain"> <property name = "connection. driver_class "> nhib.pdf. driver. sqlclientdriver </property> <property name = "connection. connection_string "> Server = (local); database = nhibernatedemo; uid = sa; Pwd = 123456; </property> <property name =" adonet. batch_size "> 10 </property> <property name =" show_ SQL "> true </property> <property name =" dialect "> nhib.pdf. dialect. mssql2008dialect </property> <property name = "use_outer_join"> true </property> <property name = "command_timeout"> 60 </property> <property name = "hbm2ddl. auto "> Update </property> <property name =" query. substitutions "> true 1, false 0, yes 'y', no 'n' </property> <property name =" proxyfactory. factory_class "> nhib.pdf. bytecode. linfu. proxyfactoryfactory, nhib.pdf. bytecode. linfu </property> <mapping Assembly = "Domain"/> </session-factory>
Add the app. config file, which contains the object to be injected. here only the persondao is injected:
<? XML version = "1.0" encoding = "UTF-8"?> <Configuration> <configsections> <sectiongroup name = "Spring"> <section name = "context" type = "Spring. context. support. contexthandler, spring. core "/> <section name =" objects "type =" Spring. context. support. defaultsectionhandler, spring. core "/> </sectiongroup> </configsections> <spring> <context> <resource uri =" config: // spring/objects "/> </context> <objects xmlns =" http://www.springframework.net "> <description> a simple example of inversion of Control </description> <Object ID =" persondao" type = "Dao. persondao, Dao "/> </objects> </spring> </configuration>
Write the personinit. CS class to generate the table structure.
Using system; using system. collections. generic; using system. LINQ; using system. text; using nhib.pdf. CFG; using nunit. framework; namespace test {[testfixture] public class personinit {// <summary> // In order to generate table t_person in database nhibernatedemo. /// </Summary> [test] public void Init () {var CFG = new configuration (). configure ("config/hibernate. cfg. XML "); Using (isessionfactory sessionfactory = cfg. buildsessionfactory ()){}}}}
The generated table structure t_person is:
Write the domaintest. CS class to add data.
Using system; using system. collections. generic; using system. LINQ; using system. text; using Dao; using domain; using nhibunit; using nunit. framework; using spring. context; using spring. context. support; namespace test {[testfixture] class domaintest {private ipersondao persondao; // [setup] // public void Init () // {// persondao = daofactory. dataaccess. createpersondao (); //} [test] public void save () {var P Erson = new person {id = guid. newguid (), name = ""}; iapplicationcontext context = contextregistry. getcontext (); persondao = context. getObject ("persondao") as ipersondao; If (persondao! = NULL) {var id = persondao. Save (person); Assert. notnull (ID );}}}}
Run this test and you can see that the data has been added.