ASP.NET&Spring.NET&NHibernate最佳实践(四)——第3章人事子系统(1)_[Asp.Net教程]
人事子系统分层结构为:领域模型层(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
{
/**////
/// 部门
///
public class Dept
{
private int _id;
private string _code;
private string _name;
属性#region 属性
/**////
/// ID
///
public virtual int ID
{
get { return _id; }
set { _id = value; }
}
/**////
/// 部门代码
///
public virtual string Code
{
get { return _code; }
set { _code = value; }
}
/**////
/// 部门名称
///
public virtual string Name
{
get { return _name; }
set { _name = value; }
}
#endregion 属性
构造函数#region 构造函数
/**////
///
///
public Dept()
{
this._id = -1;
this._code = String.Empty;
this._name = String.Empty;
}
#endregion 构造函数
}
}
员工(Employee.cs)
using System;
using System.Collections.Generic;
using System.Text;
namespace Guushuuse.SalaryPrj.HR.DomainModel
{
/**////
/// 员工
///
public class Employee
{
private int _id;
private string _code;
private string _name;
private Dept _dept;
属性#region 属性
/**////
/// ID
///
public virtual int ID
{
get { return _id; }
set { _id = value; }
}
/**////
/// 工号
///
public virtual string Code
{
get { return _code; }
set { _code = value; }
}
/**////
/// 姓名
///
public virtual string Name
{
get { return _name; }
set { _name = value; }
}
/**////
/// 部门
///
public virtual Dept Dept
{
get { return _dept; }
set { _dept = value; }
}
/**////
/// 部门ID
///
public virtual int DeptID
{
get
{
if (_dept != null)
{
return _dept.ID;
}
else
{
return -1;
}
}
}
/**////
/// 部门名称
///
public virtual string DeptName
{
get
{
if (_dept != null)
{
return _dept.Name;
}
else
{
return String.Empty;
}
}
}
#endregion 属性
构造函数#region 构造函数
/**////
///
///
public Employee()
{
this._id = -1;
this._code = String.Empty;
this._name = String.Empty;
}
#endregion 构造函数
}
}
3.2. 人事子系统映射文件(HBM)
Dept.hbm.xml
Employee.hbm.xml
3.3. 人事子系统数据访问层(Dao)
部门数据访问接口(IDeptDao.cs)
using System;
using Guushuuse.SalaryPrj.HR.DomainModel;
using System.Collections;
namespace Guushuuse.SalaryPrj.HR.Dao
{
/**////
/// 部门数据访问接口
///
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
{
/**////
/// 部门数据访问类
///
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
{
/**////
/// 员工数据访问接口
///
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
{
/**////
/// 员工数据访问类
///
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