当前位置:首页 > ASP.NET&Spring.NET&NHibernate最佳实践(四)——第3章人事子系统(1)

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

点击次数:1020  更新日期:2010-12-30
\n

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


\n

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


\n

namespace Guushuuse.SalaryPrj.HR.DomainModel
{
/**//// <summary>
/// 部门
/// </summary>
public class Dept
{
private int _id;
private string _code;
private string _name;


\n

属性#region 属性


\n

/**//// <summary>
/// ID
/// </summary>
public virtual int ID
{
get { return _id; }
set { _id = value; }
}


\n

/**//// <summary>
/// 部门代码
/// </summary>
public virtual string Code
{
get { return _code; }
set { _code = value; }
}


\n

/**//// <summary>
/// 部门名称
/// </summary>
public virtual string Name
{
get { return _name; }
set { _name = value; }
}


\n

#endregion 属性


\n

构造函数#region 构造函数


\n

/**//// <summary>
///
/// </summary>
public Dept()
{
this._id = -1;
this._code = String.Empty;
this._name = String.Empty;
}


\n

#endregion 构造函数
}
}


\n

员工(Employee.cs)


\n


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


\n

namespace Guushuuse.SalaryPrj.HR.DomainModel
{
/**//// <summary>
/// 员工
/// </summary>
public class Employee
{
private int _id;
private string _code;
private string _name;
private Dept _dept;


\n

属性#region 属性


\n

/**//// <summary>
/// ID
/// </summary>
public virtual int ID
{
get { return _id; }
set { _id = value; }
}


\n

/**//// <summary>
/// 工号
/// </summary>
public virtual string Code
{
get { return _code; }
set { _code = value; }
}


\n

/**//// <summary>
/// 姓名
/// </summary>
public virtual string Name
{
get { return _name; }
set { _name = value; }
}


\n

/**//// <summary>
/// 部门
/// </summary>
public virtual Dept Dept
{
get { return _dept; }
set { _dept = value; }
}


\n

/**//// <summary>
/// 部门ID
/// </summary>
public virtual int DeptID
{
get
{
if (_dept != null)
{
return _dept.ID;
}
else
{
return -1;
}
}
}


\n

/**//// <summary>
/// 部门名称
/// </summary>
public virtual string DeptName
{
get
{
if (_dept != null)
{
return _dept.Name;
}
else
{
return String.Empty;
}
}
}


\n

#endregion 属性


\n

构造函数#region 构造函数


\n

/**//// <summary>
///
/// </summary>
public Employee()
{
this._id = -1;
this._code = String.Empty;
this._name = String.Empty;
}


\n

#endregion 构造函数
}
}


\n

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>


\n

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


\n

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>


\n

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


\n

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


\n

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


\n

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);
}
}


\n

部门数据访问类(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;


\n

namespace Guushuuse.SalaryPrj.HR.Dao
{
/**//// <summary>
/// 部门数据访问类
/// </summary>
public class DeptDao : HibernateDaoSupport, IDeptDao
{
public DeptDao()
{


\n

}


\n

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


\n

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


\n

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


\n

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


\n

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


\n

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


\n

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);
}
}


\n

员工数据访问类(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;


\n

namespace Guushuuse.SalaryPrj.HR.Dao
{
/**//// <summary>
/// 员工数据访问类
/// </summary>
public class EmployeeDao : HibernateDaoSupport, IEmployeeDao
{
public EmployeeDao()
{


\n

}


\n


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


\n

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


\n

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


\n

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


\n

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


\n

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


\n

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

\n