After completing the entity layer, the intermediate layer is written. After seeing which articles have seen, which EntityConrol they use is very easy to use, I will almost take a copy, huh, huh, take my mind,
Add a new project Guestbook.dal
I divide sessionFactory and EntityControl into two files. Its main code is
Using
System;
Using
System.Reflection;
Using
System.data;
Using
System.data.sqlclient;
Using
NhiBernate;
Using
NHibernate.cfg;
Using
NhiBernate.dialev;
Using
NHibernate.tool.hbm2ddl;
Using
Guestbook.data;
Namespace
Guestbook.dal
{/ ** //// System; Using NhiBernate; Using Guestbook.data; Namespace Guestbook.dal {/ ** //// /// summary> public class EntityControl {private static EntityControl entity; public static EntityControl CreateEntityControl () {if (entity == null) {entity = new EntityControl ();} return entity;} public void addEntity (Object entity) {ISession s = sessionFactory.opensession (); itransaction t = s.begintransaction (); try {s.save (entity); t.commit ();} catch (exception e) {t. rollback (); throw E; } Finally {s.close ();}} public void updateentity (object entity, object key) {iSession s = sessionFactory.openSession (); itransaction t = s.begintransaction (); try {s.Update (entity, key) ; T.COMMIT ();} Catch (exception e) {t. rollback (); throw e;} finally {s.close ();}}} public void delentity (object entity) {iSession S = sessionFactory.openSession (); itransaction t = S.BegintransAction (); try {s.delete (entity); T.Commit ();} catch (exception e) {t. rollback (); throw e;} finally {s.close ();}}}} I didn't say it. I haven't mentioned that I have explained it clearly in the article I offer in the beginning of the article. I just reused, where my EntityControl has not been completed, just provide an insertion first. , Update, delete the function, write an application class to try Using System; Using NhiBernate; Using Guestbook.data; Namespace Guestbook.dal {/ ** //// Write a test file Using System; Using System.collections; Using NhiBernate; Using NHibernate.cfg; Using NUNIT.FRAMEWORK; Using Guestbook.data; Using Guestbook.dal; Namespace Guestbook.test.daltest {/ ** //// God, failed, , Prompt mistake, Guestbook.test.daltest.user.addddaluser: NHibernate.Mappingexception: Resource: Guestbook.guestbook.data.hbm.users.hbm.xml not found What is going on, my directory structure should have no problem, This is also the reason I wrote this article in advance, the heroes save your life,