3.4. 人事子系统服务层(Service)
部门服务接口(IDeptService.cs)
using System;
using Guushuuse.SalaryPrj.HR.DomainModel;
using Guushuuse.SalaryPrj.HR.Dao;
using System.Collections;
\n
namespace Guushuuse.SalaryPrj.HR.Service
{
/**//// <summary>
/// 部门服务接口
/// </summary>
public interface IDeptService
{
void CreateDept(Dept dept);
void DeleteDept(Dept dept);
IDeptDao DeptDao { get; set; }
IList GetAllDepts();
Dept GetDept(int deptID);
void UpdateDept(Dept dept);
}
}
\n
部门服务类(DeptService.cs)
using System;
using System.Collections.Generic;
using System.Text;
using Guushuuse.SalaryPrj.HR.Dao;
using Guushuuse.SalaryPrj.HR.DomainModel;
using System.Collections;
using Spring.Transaction.Interceptor;
\n
namespace Guushuuse.SalaryPrj.HR.Service
{
/**//// <summary>
/// 部门服务类
/// </summary>
public class DeptService : IDeptService
{
private IDeptDao _deptDao;
\n
public IDeptDao DeptDao
{
get { return _deptDao; }
set { _deptDao = value; }
}
\n
public DeptService()
{
\n
}
\n
[Transaction(ReadOnly = false)]
public void CreateDept(Dept dept)
{
_deptDao.CreateDept(dept);
}
\n
[Transaction(ReadOnly = false)]
public void UpdateDept(Dept dept)
{
_deptDao.UpdateDept(dept);
}
\n
[Transaction(ReadOnly = false)]
public void DeleteDept(Dept dept)
{
_deptDao.DeleteDept(dept);
}
\n
public IList GetAllDepts()
{
return _deptDao.GetAllDepts();
}
\n
public Dept GetDept(int deptID)
{
return _deptDao.GetDept(deptID);
}
}
}
\n
员工服务接口(IEmployeeService.cs)
using System;
using Guushuuse.SalaryPrj.HR.DomainModel;
using Guushuuse.SalaryPrj.HR.Dao;
using System.Collections;
\n
namespace Guushuuse.SalaryPrj.HR.Service
{
/**//// <summary>
/// 员工服务接口
/// </summary>
public interface IEmployeeService
{
void CreateEmployee(Employee employee);
void DeleteEmployee(Employee employee);
IEmployeeDao EmployeeDao { get; set; }
IList GetAllEmployees();
Employee GetEmployee(int employeeID);
void UpdateEmployee(Employee employee);
}
}
\n
员工服务类(EmployeeService.cs)
using System;
using System.Collections.Generic;
using System.Text;
using Guushuuse.SalaryPrj.HR.Dao;
using Guushuuse.SalaryPrj.HR.DomainModel;
using System.Collections;
using Spring.Transaction.Interceptor;
\n
namespace Guushuuse.SalaryPrj.HR.Service
{
/**//// <summary>
/// 员工服务类
/// </summary>
public class EmployeeService : IEmployeeService
{
private IEmployeeDao _employeeDao;
\n
public IEmployeeDao EmployeeDao
{
get { return _employeeDao; }
set { _employeeDao = value; }
}
\n
public EmployeeService()
{
\n
}
\n
[Transaction(ReadOnly = false)]
public void CreateEmployee(Employee employee)
{
_employeeDao.CreateEmployee(employee);
}
\n
[Transaction(ReadOnly = false)]
public void UpdateEmployee(Employee employee)
{
_employeeDao.UpdateEmployee(employee);
}
\n
[Transaction(ReadOnly = false)]
public void DeleteEmployee(Employee employee)
{
_employeeDao.DeleteEmployee(employee);
}
\n
public IList GetAllEmployees()
{
return _employeeDao.GetAllEmployees();
}
\n
public Employee GetEmployee(int employeeID)
{
return _employeeDao.GetEmployee(employeeID);
}
}
}
\n
服务定位类(ServiceLocator.cs)
using System;
using System.Collections.Generic;
using System.Text;
using Spring.Context;
using Spring.Context.Support;
\n
namespace Guushuuse.SalaryPrj.HR.Service
{
/**//// <summary>
/// 服务定位类
/// </summary>
public class ServiceLocator
{
private static IApplicationContext _ctx;
\n
static ServiceLocator()
{
_ctx = ContextRegistry.GetContext();
}
\n
public static IDeptService DeptService
{
get
{
IDeptService deptService = _ctx["deptService"] as IDeptService;
\n
return deptService;
}
}
\n
public static IEmployeeService EmployeeService
{
get
{
IEmployeeService employeeService = _ctx["employeeService"] as IEmployeeService;
\n
return employeeService;
}
}
}
}
\n
修改Config/Guushuuse.SalaryPrj.HR.config文件,新增object
<object id=”deptService” type=”Guushuuse.SalaryPrj.HR.Service.DeptService, Guushuuse.SalaryPrj.HR”>
<property name=”DeptDao” ref=”deptDao” />
</object>
\n
<object id=”employeeService” type=”Guushuuse.SalaryPrj.HR.Service.EmployeeService, Guushuuse.SalaryPrj.HR”>
<property name=”EmployeeDao” ref=”employeeDao” />
</object>
\n
3.5. 人事子系统帮助类(Helper)
帮助类(HRHelper.cs)
using System;
using System.Collections.Generic;
using System.Text;
using Guushuuse.SalaryPrj.HR.DomainModel;
using Guushuuse.SalaryPrj.HR.Service;
using System.Collections;
\n
namespace Guushuuse.SalaryPrj.HR.Helper
{
/**//// <summary>
/// 帮助类
/// </summary>
public class HRHelper
{
/**//// <summary>
/// 新增部门
/// </summary>
/// <param name=”code”></param>
/// <param name=”name”></param>
public static void CreateDept(string code, string name)
{
try
{
Dept dept = new Dept();
dept.Code = code;
dept.Name = name;
\n
ServiceLocator.DeptService.CreateDept(dept);
}
catch (Exception ex)
{
throw ex;
}
}
\n
/**//// <summary>
/// 更新部门
/// </summary>
/// <param name=”id”></param>
/// <param name=”code”></param>
/// <param name=”name”></param>
public static void UpdateDept(int id, string code, string name)
{
try
{
Dept dept = ServiceLocator.DeptService.GetDept(id);
dept.Code = code;
dept.Name = name;
\n
ServiceLocator.DeptService.CreateDept(dept);
}
catch (Exception ex)
{
throw ex;
}
}
\n
/**//// <summary>
/// 删除部门
/// </summary>
/// <param name=”id”></param>
/// <param name=”code”></param>
/// <param name=”name”></param>
public static void DeleteDept(int id, string code, string name)
{
try
{
Dept dept = ServiceLocator.DeptService.GetDept(id);
ServiceLocator.DeptService.DeleteDept(dept);
}
catch (Exception ex)
{
throw ex;
}
}
\n
/**//// <summary>
/// 取出所有部门
/// </summary>
/// <returns></returns>
public static IList GetAllDepts()
{
IList depts;
\n
try
{
depts = ServiceLocator.DeptService.GetAllDepts();
}
catch (Exception ex)
{
throw ex;
}
\n
return depts;
}
\n
/**//// <summary>
/// 新增员工
/// </summary>
/// <param name=”code”></param>
/// <param name=”name”></param>
/// <param name=”deptID”></param>
public static void CreateEmployee(string code, string name, int deptID)
{
try
{
Employee employee = new Employee();
employee.Code = code;
employee.Name = name;
employee.Dept = ServiceLocator.DeptService.GetDept(deptID);
\n
ServiceLocator.EmployeeService.CreateEmployee(employee);
}
catch (Exception ex)
{
throw ex;
}
}
\n
/**//// <summary>
/// 更新员工
/// </summary>
/// <param name=”id”></param>
/// <param name=”code”></param>
/// <param name=”name”></param>
/// <param name=”deptID”></param>
public static void UpdateEmployee(int id, string code, string name, int deptID)
{
try
{
Employee employee = ServiceLocator.EmployeeService.GetEmployee(id);
employee.Code = code;
employee.Name = name;
employee.Dept = ServiceLocator.DeptService.GetDept(deptID);
\n
ServiceLocator.EmployeeService.CreateEmployee(employee);
}
catch (Exception ex)
{
throw ex;
}
}
\n
/**//// <summary>
/// 删除员工
/// </summary>
/// <param name=”id”></param>
/// <param name=”code”></param>
/// <param name=”name”></param>
public static void DeleteEmployee(int id, string code, string name)
{
try
{
Employee employee = ServiceLocator.EmployeeService.GetEmployee(id);
\n
ServiceLocator.EmployeeService.DeleteEmployee(employee);
}
catch (Exception ex)
{
throw ex;
}
}
\n
/**//// <summary>
/// 取出所有员工
/// </summary>
/// <returns></returns>
public static IList GetAllEmployees()
{
IList employees;
\n
try
{
employees = ServiceLocator.EmployeeService.GetAllEmployees();
}
catch (Exception ex)
{
throw ex;
}
\n
return employees;
}
}
}