Guushuuse .NET

一直专注于企业应用开发。 5~9——原创随笔

 

ASP.NET&Spring.NET&NHibernate最佳实践(四)——第3章人事子系统(1)

人事子系统分层结构为:领域模型层(DomainModel)——数据访问层(Dao)——服务层(Sevice)——表示层(Web),在Web页面中采用了ObjectDataSource作为GridView的数据源,并为此增加了一个帮助类。
在数据访问层中充分体现了Spring.NET和NHibernate的无缝集成,只要继承HibernateDaoSupport就能很便捷的使用NHibernate,而不需要很深入了解NHibernate。

3.1. 人事子系统领域模型层(DomainModel)
部门(Dept.cs)
using System;
using System.Collections.Generic;
using System.Text;

namespace Guushuuse.SalaryPrj.HR.DomainModel
{
    
/// <summary>
    
/// 部门
    
/// </summary>

    public class Dept
    
{
        
private int _id;
        
private string _code;
        
private string _name;

        
属性 属性

        
构造函数 构造函数
    }

}

员工(Employee.cs)

using System;
using System.Collections.Generic;
using System.Text;

namespace Guushuuse.SalaryPrj.HR.DomainModel
{
    
/// <summary>
    
/// 员工
    
/// </summary>

    public class Employee
    
{
        
private int _id;
        
private string _code;
        
private string _name;
        
private Dept _dept;

        
属性 属性

        
构造函数 构造函数
    }

}


3.2. 人事子系统映射文件(HBM)
Dept.hbm.xml
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  
<class name="Guushuuse.SalaryPrj.HR.DomainModel.Dept, Guushuuse.SalaryPrj.HR" table="t_depts">
    
<id name="ID" column="dept_id" type="Int32"  unsaved-value="-1">
      
<generator class="identity" />
    
</id>

    
<property name="Code" column="dept_code" type="String" length="255" not-null="true" />
    
<property name="Name" column="dept_name" type="String" length="255" not-null="true" />  
  
</class>
</hibernate-mapping>

Employee.hbm.xml
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  
<class name="Guushuuse.SalaryPrj.HR.DomainModel.Employee, Guushuuse.SalaryPrj.HR" table="t_employees">
    
<id name="ID" column="employee_id" type="Int32"  unsaved-value="-1">
      
<generator class="identity" />
    
</id>

    
<property name="Code" column="employee_code" type="String" length="255" not-null="true" />
    
<property name="Name" column="employee_name" type="String" length="255" not-null="true" />

    
<many-to-one name="Dept" column="dept_id" class="Guushuuse.SalaryPrj.HR.DomainModel.Dept, Guushuuse.SalaryPrj.HR" not-null="true" />
  
</class>
</hibernate-mapping>

3.3. 人事子系统数据访问层(Dao)
部门数据访问接口(IDeptDao.cs)
using System;
using Guushuuse.SalaryPrj.HR.DomainModel;
using System.Collections;

namespace Guushuuse.SalaryPrj.HR.Dao
{
    
/// <summary>
    
/// 部门数据访问接口
    
/// </summary>

    public interface IDeptDao
    
{
        
void CreateDept(Dept dept);
        
void DeleteDept(Dept dept);
        IList GetAllDepts();
        Dept GetDept(
int deptID);
        
void UpdateDept(Dept dept);
    }

}


部门数据访问类(DeptDao.cs)
using System;
using System.Collections.Generic;
using System.Text;
using Spring.Data.NHibernate.Support;
using Spring.Transaction.Interceptor;
using Guushuuse.SalaryPrj.HR.DomainModel;
using System.Collections;

namespace Guushuuse.SalaryPrj.HR.Dao
{
    
/// <summary>
    
/// 部门数据访问类
    
/// </summary>

    public class DeptDao : HibernateDaoSupport, IDeptDao
    
{
        
public DeptDao()
        
{

        }


        [Transaction(ReadOnly 
= false)]
        
public void CreateDept(Dept dept)
        
{
            HibernateTemplate.Save(dept);
        }


        [Transaction(ReadOnly 
= false)]
        
public void UpdateDept(Dept dept)
        
{
            HibernateTemplate.Update(dept);
        }


        [Transaction(ReadOnly 
= false)]
        
public void DeleteDept(Dept dept)
        
{
            HibernateTemplate.Delete(dept);
        }


        
public IList GetAllDepts()
        
{
            
return HibernateTemplate.LoadAll(typeof(Dept));
        }


        
public Dept GetDept(int deptID)
        
{
            
return (Dept)HibernateTemplate.Get(typeof(Dept), deptID);
        }

    }

}


员工数据访问接口(IEmployeeDao.cs)
using System;
using Guushuuse.SalaryPrj.HR.DomainModel;
using System.Collections;

namespace Guushuuse.SalaryPrj.HR.Dao
{
    
/// <summary>
    
/// 员工数据访问接口
    
/// </summary>

    public interface IEmployeeDao
    
{
        
void CreateEmployee(Employee employee);
        
void DeleteEmployee(Employee employee);
        IList GetAllEmployees();
        Employee GetEmployee(
int employeeID);
        
void UpdateEmployee(Employee employee);
    }

}


员工数据访问类(EmployeeDao.cs)
using System;
using System.Collections.Generic;
using System.Text;
using Spring.Data.NHibernate.Support;
using Spring.Transaction.Interceptor;
using Guushuuse.SalaryPrj.HR.DomainModel;
using System.Collections;

namespace Guushuuse.SalaryPrj.HR.Dao
{
    
/// <summary>
    
/// 员工数据访问类
    
/// </summary>

    public class EmployeeDao : HibernateDaoSupport, IEmployeeDao
    
{
        
public EmployeeDao()
        
{

        }



        [Transaction(ReadOnly 
= false)]
        
public void CreateEmployee(Employee employee)
        
{
            HibernateTemplate.Save(employee);
        }


        [Transaction(ReadOnly 
= false)]
        
public void UpdateEmployee(Employee employee)
        
{
            HibernateTemplate.Update(employee);
        }


        [Transaction(ReadOnly 
= false)]
        
public void DeleteEmployee(Employee employee)
        
{
            HibernateTemplate.Delete(employee);
        }


        
public IList GetAllEmployees()
        
{
            
return HibernateTemplate.LoadAll(typeof(Employee));
        }


        
public Employee GetEmployee(int employeeID)
        
{
            
return (Employee)HibernateTemplate.Get(typeof(Employee), employeeID);
        }

    }

}


修改Config/Guushuuse.SalaryPrj.HR.config文件,新增object
<object id="deptDao" type="Guushuuse.SalaryPrj.HR.Dao.DeptDao, Guushuuse.SalaryPrj.HR">
    
<property name="HibernateTemplate" ref="hibernateTemplate" />
  
</object>

  
<object id="employeeDao" type="Guushuuse.SalaryPrj.HR.Dao.EmployeeDao, Guushuuse.SalaryPrj.HR">
    
<property name="HibernateTemplate" ref="hibernateTemplate" />
  
</object>

ASP.NET&Spring.NET&NHibernate最佳实践(五)——第3章人事子系统(2) 

ASP.NET&Spring.NET&NHibernate最佳实践(六)——第3章人事子系统(3) 

ASP.NET&Spring.NET&NHibernate最佳实践(七)——第3章人事子系统(4)人事子系统小结

posted on 2008-05-10 17:34 guushuuse 阅读(2523) 评论(8)  编辑 收藏 网摘 所属分类: 51.ASP.NET&Spring.NET&NHibernate最佳实践

评论

#1楼 2008-05-10 23:12 求知无傲      

mark   回复  引用  查看    

#2楼 2008-05-11 19:18 laudy      

lz,为什么不在部门DAO和员工DAO之上再建立一个公共的泛型DAO类作为基类呢?

这样在部门DAO和员工DAO类中一句代码都不需要了(当有特殊的操作时就采用重载来处理)
  回复  引用  查看    

#3楼 2008-05-11 19:27 laudy      

也不知道我说明白了没有,我的意思是:

public interface IGenericDao<T, ID>
{
...
}

public class GenericDao<T, ID> : HibernateDaoSupport, IGenericDao<T, ID>
{
...
}

public interface IDeptDao
{
}

public class DeptDao : GenericDao<Dept,Int32>,IDeptDao
{
}

员工类同理

这样在部门DAO和员工DAO类中就不需要写代码了,全部都在GenericDao中实现了,当然是在没有特殊操作的情况下,如果有特殊操作,就可以在部门DAO或者员工DAO中进行重载(不过我建议在Service层进行重载)


个人拙见,如有不当之处,还请见谅,最好能指教一番,多谢!!!

如不嫌弃,请加我MSN:laudy_lisa@hotmail.com,方便以后多交流!!

  回复  引用  查看    

#4楼[楼主] 2008-05-11 20:14 guushuuse      

To laudy:
谢谢指教

你的方式不错,但我很难用文字来表达你这种方式。所以代码写得多一些,尽量把我的意思表达出来。看来我的代码还需不断重构。
  回复  引用  查看    

#5楼 2008-05-12 01:48 镜涛      

@laudy
恩,通用的操作利用泛型解决了针对不同实体创建单独类造成的代码重复。继承一个泛型基类,对于个别方法单独重载。
  回复  引用  查看    

#6楼 2008-05-12 16:15 Solog      

希望你提及 及联操作的内容。
最近也在学习这些,支持LZ
  回复  引用  查看    

#7楼 2008-05-12 16:17 Solog      

public interface IBaseDAO<EntityT, idT>
{
IList<EntityT> LoadAll();
EntityT LoadByID(idT id);
IList<EntityT> Load(string hsqlQuery, object[] values);
void Save(EntityT fine);
void SaveOrUpdate(EntityT fine);
void Delete(EntityT fine);
NHibernate.ISessionFactory SessionFactory { set; }
}
BaseDAO实现IBaseDAO
我是让其他实际DAO继承抽象BaseDAO,如此来实现的
  回复  引用  查看    

#8楼 2008-05-16 13:53 laudy      

关联表的操作在Entity类中已经设置好了啊,在主表中添加IList<Entity>属性,配置文件设置好,直接调用范型类方法就可以进行操作了啊

个人观点:表之间的关联操作,重点在于数据库中表的设计及配置文件的配置和实体类的定义,当然这是应用层面的,复杂的操作要想理解透彻,还是看看NHibernate源代码吧:),我也没用过NHibernate,只是对Spring.net的Ioc及AOP思想比较感兴趣
  回复  引用  查看    




发表评论

昵称: [登录] [注册]

主页:

邮箱:(仅博主可见)

评论内容:

  登录  注册

[使用Ctrl+Enter键快速提交评论]

0 1191417




相关文章:

相关链接:

导航

统计

公告

无锡地图

与我联系

搜索

 

常用链接

留言簿

我的标签

随笔分类

随笔档案

01技术网站

07技术网站

11.NET

15.NET开源

16.NET开源

18.NET博客

81其他

82其他

99友情链接

积分与排名

最新评论

阅读排行榜

评论排行榜