NHibernate small project (2)

xiaoxiao2021-03-06  40

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

{/ ** ////

/// sessionFactory summary description. /// public class SessionFactory {private static ISessionFactory sessions; private static Configuration cfg; private static Dialect dialect; public SessionFactory () {// // TODO: Add constructor logic here //} public static ISession OpenSession () {if (sessions == null) {buildSessionFactory ();} return sessions.OpenSession ();} private static void buildSessionFactory () {exportSchema (new string [] { "users.hbm.xml", "guestbook. HBM.XML "}, True);} private static void exportschema (String [] files, bool exportschema) {cfg = new configuration (); for (int i = 0; i

System;

Using

NhiBernate;

Using

Guestbook.data;

Namespace

Guestbook.dal

{/ ** ////

/// EntityContorl's summary description.

/// 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

{/ ** ////

/// Usersdal's summary description. /// public class usersdal {private EntityControl control; public usersdal () {control = EntityControl.CreateEntityControl ();} public void addUser (users user) {control.addEntity (user);} public void updateUser (users User, int ID) {Control.UpdateEntity (user, user.id);} public void deluser (user) {control.delentity (user);}}}

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

{/ ** ////

/// User's summary description. /// [TestFixTure] public class user {public user () {// // Todo: Add constructor logic //} [TEST] public void addddaluser () {user = new users ( ); Newuser.name = "pb"; newuser.email = "cctv5cn@gmail.com"; newuser.password = "147852"; newuser.flag = 1; newuser.regtime = DateTime.now; usersdal ud = new usersdal Ud.adduser (newuser);}}}

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,

转载请注明原文地址:https://www.9cbs.com/read-68749.html

New Post(0)