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

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

using NHibernate;
using NHibernate.Cfg;
using NHibernate.Dialect;
using NHibernate.Tool.hbm2ddl;

using guestbook.data;
namespace guestbook.Dal
{
    
/// <summary>
    
/// SessionFactory 的摘要说明。
    
/// </summary>

    public class SessionFactory
    
{
        
private static ISessionFactory sessions;
        
private static Configuration cfg;
        
private static Dialect dialect;

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


        
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<files.Length;i++)
            
{
                cfg.AddResource(
"guestbook.data.hbm."+files[i],Assembly.Load("guestbook.data"));
            }


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

            sessions
=cfg.BuildSessionFactory();
        }


    }

}


using System;

using NHibernate;
using guestbook.data;

namespace guestbook.Dal
{
    
/// <summary>
    
/// EntityContorl 的摘要说明。
    
/// </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();
            }

        }

    }

}

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

using NHibernate;
using guestbook.data;

namespace guestbook.Dal
{
    
/// <summary>
    
/// usersdal 的摘要说明。
    
/// </summary>

    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(users user)
        
{
            control.DelEntity(user);
        }



    }

}


在写个测试文件
using System;

using System.Collections;
using NHibernate;
using NHibernate.Cfg;

using NUnit.Framework;

using guestbook.data;
using guestbook.Dal;
namespace guestbook.test.daltest
{
    
/// <summary>
    
/// user 的摘要说明。
    
/// </summary>

    [TestFixture]
    
public class user
    
{
        
public user()
        
{
            
//
            
// TODO: 在此处添加构造函数逻辑
            
//
        }

    
        [Test]
public void AddDalUser()
        
{
            users newUser
=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);
        }

    }

}

天啊 ,失败了,,提示错误,
guestbook.test.daltest.user.AddDalUser : NHibernate.MappingException :
 Resource: guestbook.guestbook.data.hbm.users.hbm.xml not found
这是怎么 回事 ,我的目录结构应该没有问题啊 ,

这也是我提前写这篇文章的原因,大侠们 救命 啊,
posted @ 2004-10-14 17:12 无心之柳.NET 阅读(...) 评论(...) 编辑 收藏