NHibernate小项目解说
NHibernate小项目解说
完成了实体层 就该写中间层了,看了哪几篇文章后,对他们用的哪个EntityConrol感到非常好用,我也就几乎是照抄了一个,呵呵,拿来主意 吗,
添加一个 新建工程 guestbook.Dal
我把Sessionfactory 和 EntityControl分成了两个文件其主要代码为
usingSystem;
usingSystem.Reflection;
usingSystem.Data;
usingSystem.Data.SqlClient;

usingNHibernate;
usingNHibernate.Cfg;
usingNHibernate.Dialect;
usingNHibernate.Tool.hbm2ddl;

usingguestbook.data;
namespaceguestbook.Dal

{

///<summary>
///SessionFactory的摘要说明。
///</summary>
publicclassSessionFactory

{
privatestaticISessionFactorysessions;
privatestaticConfigurationcfg;
privatestaticDialectdialect;

publicSessionFactory()

{
//
//TODO:在此处添加构造函数逻辑
//
}

publicstaticISessionOpenSession()

{
if(sessions==null)

{
BuildSessionFactory();
}
returnsessions.OpenSession();
}

privatestaticvoidBuildSessionFactory()

{

ExportSchema(newstring[]{
"users.hbm.xml",
"guestbook.hbm.xml"
},true);
}

privatestaticvoidExportSchema(string[]files,boolexportschema)

{
cfg=newConfiguration();
for(inti=0;i<files.Length;i++)

{
cfg.AddResource("guestbook.data.hbm."+files[i],Assembly.Load("guestbook.data"));
}

dialect=Dialect.GetDialect();
if(exportschema)newSchemaExport(cfg).Create(true,true);

sessions=cfg.BuildSessionFactory();
}

}
}
usingSystem;

usingNHibernate;
usingguestbook.data;

namespaceguestbook.Dal

{

///<summary>
///EntityContorl的摘要说明。
///</summary>
publicclassEntityControl

{
privatestaticEntityControlentity;

publicstaticEntityControlCreateEntityControl()

{
if(entity==null)

{
entity=newEntityControl();
}
returnentity;
}
publicvoidaddEntity(Objectentity)

{
ISessions=SessionFactory.OpenSession();
ITransactiont=s.BeginTransaction();
try

{
s.Save(entity);
t.Commit();
}
catch(Exceptione)

{
t.Rollback();
throwe;
}
finally

{
s.Close();
}
}

publicvoidupdateEntity(Objectentity,Objectkey)

{
ISessions=SessionFactory.OpenSession();
ITransactiont=s.BeginTransaction();
try

{
s.Update(entity,key);
t.Commit();
}
catch(Exceptione)

{
t.Rollback();
throwe;
}
finally

{
s.Close();
}
}

publicvoidDelEntity(objectentity)

{
ISessions=SessionFactory.OpenSession();
ITransactiont=s.BeginTransaction();

try

{
s.Delete(entity);
t.Commit();
}
catch(Exceptione)

{
t.Rollback();
throwe;
}
finally

{
s.Close();
}
}
}
}
其中代码的意思 我就不说了,我在文章开头提供的两位仁兄的文章里已经说明的很清楚了,我只是借签过吗 ,其中我的EntityControl还没有完成,只是先提供了一插入、更新、删除的功能,
写一个应用类试试
usingSystem;

usingNHibernate;
usingguestbook.data;

namespaceguestbook.Dal

{

///<summary>
///usersdal的摘要说明。
///</summary>
publicclassusersdal

{
privateEntityControlcontrol;
publicusersdal()

{
control=EntityControl.CreateEntityControl();
}

publicvoidaddUser(usersuser)

{
control.addEntity(user);
}

publicvoidupdateUser(usersuser,intId)

{
control.updateEntity(user,user.id);
}

publicvoidDelUser(usersuser)

{
control.DelEntity(user);
}


添加一个 新建工程 guestbook.Dal
我把Sessionfactory 和 EntityControl分成了两个文件其主要代码为















































































































































































写一个应用类试试








































