Guushuuse .NET

领域驱动设计==哲学

导航

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

3.4. 人事子系统服务层(Service)
部门服务接口(IDeptService.cs)
using System;
using Guushuuse.SalaryPrj.HR.DomainModel;
using Guushuuse.SalaryPrj.HR.Dao;
using System.Collections;

namespace Guushuuse.SalaryPrj.HR.Service
{
    
/// <summary>
    
/// 部门服务接口
    
/// </summary>

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

}


部门服务类(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;

namespace Guushuuse.SalaryPrj.HR.Service
{
    
/// <summary>
    
/// 部门服务类
    
/// </summary>

    public class DeptService : IDeptService
    
{        
        
private IDeptDao _deptDao;

        
public IDeptDao DeptDao
        
{
            
get return _deptDao; }
            
set { _deptDao = value; }
        }


        
public DeptService()
        
{

        }


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


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


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


        
public IList GetAllDepts()
        
{
            
return _deptDao.GetAllDepts();
        }


        
public Dept GetDept(int deptID)
        
{
            
return _deptDao.GetDept(deptID);
        }

    }

}


员工服务接口(IEmployeeService.cs)
using System;
using Guushuuse.SalaryPrj.HR.DomainModel;
using Guushuuse.SalaryPrj.HR.Dao;
using System.Collections;

namespace Guushuuse.SalaryPrj.HR.Service
{
    
/// <summary>
    
/// 员工服务接口
    
/// </summary>

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

}


员工服务类(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;

namespace Guushuuse.SalaryPrj.HR.Service
{
    
/// <summary>
    
/// 员工服务类
    
/// </summary>

    public class EmployeeService : IEmployeeService
    
{        
        
private IEmployeeDao _employeeDao;

        
public IEmployeeDao EmployeeDao
        
{
            
get return _employeeDao; }
            
set { _employeeDao = value; }
        }


        
public EmployeeService()
        
{

        }


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


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


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


        
public IList GetAllEmployees()
        
{
            
return _employeeDao.GetAllEmployees();
        }


        
public Employee GetEmployee(int employeeID)
        
{
            
return _employeeDao.GetEmployee(employeeID);
        }

    }

}


服务定位类(ServiceLocator.cs)
using System;
using System.Collections.Generic;
using System.Text;
using Spring.Context;
using Spring.Context.Support;

namespace Guushuuse.SalaryPrj.HR.Service
{
    
/// <summary>
    
/// 服务定位类
    
/// </summary>

    public class ServiceLocator
    
{
        
private static IApplicationContext _ctx;

        
static ServiceLocator()
        
{
            _ctx 
= ContextRegistry.GetContext();
        }


        
public static IDeptService DeptService
        
{
            
get
            
{
                IDeptService deptService 
= _ctx["deptService"as IDeptService;

                
return deptService;
            }

        }


        
public static IEmployeeService EmployeeService
        
{
            
get
            
{
                IEmployeeService employeeService 
= _ctx["employeeService"as IEmployeeService;

                
return employeeService;
            }

        }

    }

}


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

  
<object id="employeeService" type="Guushuuse.SalaryPrj.HR.Service.EmployeeService, Guushuuse.SalaryPrj.HR">
    
<property name="EmployeeDao" ref="employeeDao" />
  
</object>

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;

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;

                ServiceLocator.DeptService.CreateDept(dept);
            }

            
catch (Exception ex)
            
{
                
throw ex;
            }

        }


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

                ServiceLocator.DeptService.CreateDept(dept);
            }

            
catch (Exception ex)
            
{
                
throw ex;
            }

        }


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

        }


        
/// <summary>
        
/// 取出所有部门
        
/// </summary>
        
/// <returns></returns>

        public static IList GetAllDepts()
        
{
            IList depts;

            
try
            
{
                depts 
= ServiceLocator.DeptService.GetAllDepts();
            }

            
catch (Exception ex)
            
{
                
throw ex;
            }


            
return depts;
       }


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

                ServiceLocator.EmployeeService.CreateEmployee(employee);
            }

            
catch (Exception ex)
            
{
                
throw ex;
            }

        }


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

                ServiceLocator.EmployeeService.CreateEmployee(employee);
            }

            
catch (Exception ex)
            
{
                
throw ex;
            }

        }


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

                ServiceLocator.EmployeeService.DeleteEmployee(employee);
            }

            
catch (Exception ex)
            
{
                
throw ex;
            }

        }


        
/// <summary>
        
/// 取出所有员工
        
/// </summary>
        
/// <returns></returns>

        public static IList GetAllEmployees()
        
{
            IList employees;

            
try
            
{
                employees 
= ServiceLocator.EmployeeService.GetAllEmployees();
            }

            
catch (Exception ex)
            
{
                
throw ex;
            }


            
return employees;
        }

    }

}


posted on 2008-05-10 18:57  guushuuse  阅读(1477)  评论(2编辑  收藏  举报