NHibernate 做个小项目来试一下吧 二

完成了实体层 就该写中间层了,看了哪几篇文章后,对他们用的哪个EntityConrol感到非常好用,我也就几乎是照抄了一个,呵呵,拿来主意 吗, red_smile.gif
添加一个 新建工程  guestbook.Dal
我把Sessionfactory 和 EntityControl分成了两个文件其主要代码为
None.gif using  System;
None.gif
using  System.Reflection;
None.gif
using  System.Data;
None.gif
using  System.Data.SqlClient;
None.gif
None.gif
using  NHibernate;
None.gif
using  NHibernate.Cfg;
None.gif
using  NHibernate.Dialect;
None.gif
using  NHibernate.Tool.hbm2ddl;
None.gif
None.gif
using  guestbook.data;
None.gif
namespace  guestbook.Dal
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// SessionFactory 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class SessionFactory
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private static ISessionFactory sessions;
InBlock.gif        
private static Configuration cfg;
InBlock.gif        
private static Dialect dialect;
InBlock.gif
InBlock.gif        
public SessionFactory()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// TODO: 在此处添加构造函数逻辑
InBlock.gif            
//
ExpandedSubBlockEnd.gif
        }

InBlock.gif
InBlock.gif        
public static ISession OpenSession()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if(sessions==null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                BuildSessionFactory();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return sessions.OpenSession();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private static void BuildSessionFactory()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            ExportSchema(
new string[]dot.gif{
InBlock.gif                                         
"users.hbm.xml",
InBlock.gif                                         
"guestbook.hbm.xml"
ExpandedSubBlockEnd.gif                                     }
,true);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private static void ExportSchema(string[] files,bool exportschema)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            cfg
=new Configuration();
InBlock.gif            
for(int i=0;i<files.Length;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                cfg.AddResource(
"guestbook.data.hbm."+files[i],Assembly.Load("guestbook.data"));
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            dialect
=Dialect.GetDialect();
InBlock.gif            
if(exportschema) new SchemaExport(cfg).Create(true,true);
InBlock.gif
InBlock.gif            sessions
=cfg.BuildSessionFactory();
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

None.gif using  System;
None.gif
None.gif
using  NHibernate;
None.gif
using  guestbook.data;
None.gif
None.gif
namespace  guestbook.Dal
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// EntityContorl 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class EntityControl
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private static EntityControl entity;
InBlock.gif
InBlock.gif        
public static EntityControl CreateEntityControl()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if(entity==null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                entity
=new EntityControl();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return entity;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public void addEntity(Object entity)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            ISession s
=SessionFactory.OpenSession();
InBlock.gif            ITransaction t
=s.BeginTransaction();
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                s.Save(entity);
InBlock.gif                t.Commit();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch(Exception e)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                t.Rollback();
InBlock.gif                
throw e;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                s.Close();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif            
InBlock.gif        
public void updateEntity(Object entity,Object key)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            ISession s
=SessionFactory.OpenSession();
InBlock.gif            ITransaction t
=s.BeginTransaction();
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                s.Update(entity,key);
InBlock.gif                t.Commit();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch(Exception e)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                t.Rollback();
InBlock.gif                
throw e;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                s.Close();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
public void DelEntity(object entity)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            ISession s
=SessionFactory.OpenSession();
InBlock.gif            ITransaction t
=s.BeginTransaction();
InBlock.gif            
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                s.Delete(entity);
InBlock.gif                t.Commit();
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch(Exception e)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                t.Rollback();
InBlock.gif                
throw e;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
finally
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                s.Close();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
其中代码的意思 我就不说了,我在文章开头提供的两位仁兄的文章里已经说明的很清楚了,我只是借签过吗 ,其中我的EntityControl还没有完成,只是先提供了一插入、更新、删除的功能,
写一个应用类试试
None.gif using  System;
None.gif
None.gif
using  NHibernate;
None.gif
using  guestbook.data;
None.gif
None.gif
namespace  guestbook.Dal
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// usersdal 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class usersdal
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private EntityControl control;
InBlock.gif        
public usersdal()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            control
=EntityControl.CreateEntityControl();
ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
public void addUser(users user)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            control.addEntity(user);
ExpandedSubBlockEnd.gif        }

InBlock.gif            
InBlock.gif        
public void updateUser(users user,int Id)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            control.updateEntity(user,user.id);
ExpandedSubBlockEnd.gif        }

InBlock.gif        
InBlock.gif        
public void DelUser(users user)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            control.DelEntity(user);
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

在写个测试文件
None.gif using  System;
None.gif
None.gif
using  System.Collections;
None.gif
using  NHibernate;
None.gif
using  NHibernate.Cfg;
None.gif
None.gif
using  NUnit.Framework;
None.gif
None.gif
using  guestbook.data;
None.gif
using  guestbook.Dal;
None.gif
namespace  guestbook.test.daltest
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// user 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    [TestFixture]
InBlock.gif    
public class user
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public user()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// TODO: 在此处添加构造函数逻辑
InBlock.gif            
//
ExpandedSubBlockEnd.gif
        }

InBlock.gif    
InBlock.gif        [Test]
public void AddDalUser()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            users newUser
=new users();
InBlock.gif            newUser.Name
="Pb";
InBlock.gif            newUser.email
="cctv5cn@gmail.com";
InBlock.gif            newUser.password
="147852";
InBlock.gif            newUser.flag
=1;
InBlock.gif            newUser.regtime
=DateTime.Now;
InBlock.gif
InBlock.gif            usersdal ud
=new usersdal();
InBlock.gif            ud.addUser(newUser);
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

天啊 ,失败了, angry_smile.gif,提示错误,
None.gif guestbook.test.daltest.user.AddDalUser : NHibernate.MappingException :
None.gif Resource: guestbook.guestbook.data.hbm.users.hbm.xml not found
这是怎么 回事 ,我的目录结构应该没有问题啊 ,
Capture_3.jpg
这也是我提前写这篇文章的原因,大侠们 救命 啊,
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值