您的位置:首页 > 编程语言 > Java开发

ExtJS4.1+MVC3+Spring.NET1.3+EF5 整合六:业务逻辑层

2013-04-13 23:30 260 查看
在写这篇文章的时,主要是参考了一个中小型企业网站大部分共有的需求和功能,一般来说,企业网站都会有后台、前台。后台的功能是给管理员使用的,根据不同的权限分配,管理员可以发布、修改信息;前台给普通客户访问,也就是我们正常看到的页面,展示企业的形象、新闻等。

与业务系统不同的是,业务系统可以根据不同的用户群开发单独的系统,但小型项目不需要。为了在一个项目中开发针对两个用户群的“网站”,我使用ASP.NET MVC中的Area(区域)来解决这个问题。即创建一个名称为"Manage"的区域做为后台管理模块,默认的模块(默认为Default)做为前台用户访问。相应的,在Service层中增加两个命名空间:Manage、Default,分别对应于Web层的两个Area。先看下Service层的结构:





1 IService 接口

1.1 ISQLPaging接口

不论是在C/S还是B/S中,数据分页问题都是最常见的,在上篇的数据访问层中,能够看到IDao有如下方法定义:

IList<T> Query(string sql, int pageIndex, int pageSize, out int recordCount);

从参数就能看出:分页的SQL语句,主要是考虑Where条件、Group分组、Order排序等一系列问题,所以就是使用者自己处理,这个要根据实际情况,这么做比较自由,但最终又把SQL语句交给使用者处理;第二个是分页索引,第1页是0,第2页是1 ,依次类推;第三个参数是分页大小,即一页共显示多少条数据;最后一个是输出参数,总共有多少数据,以便在用户层中计算有多少页。

在Dao层是通过这4个参数来处理分页的,但在Service层中就不建议这么做 了,最好定义一个接口,以实现这个功能,所以有了ISQLPaging接口:

namespace MESE.IService
{
public interface ISQLPaging
{
int PageIndex { get; }
int PageSize { get; }
int RecordCount { get; set; }
string QuerySQL { get; }
}
}


1.2 IRoleService 接口

根据业务需求定义接口内容:

namespace MESE.IService.Manage
{
public interface IRoleService
{
IList<Role> Query(ISQLPaging paging);
IList<Role> QueryAll();
bool Exists(string code);
Role Read(string code);
bool Add(Role entity);
bool Update(Role entity);
bool Delete(Role entity);
bool Delete(string code);
int DeleteByKeys(IList<string> keys);
}
}


1.3 IAdminService接口

与IRoleService接口差不多:

namespace MESE.IService.Manage
{
public interface IAdminService
{
IList<Admin> Query(ISQLPaging paging);
IList<Admin> QueryAll();
bool Exists(string code);
Admin Read(string code);
bool Add(Admin entity);
bool Update(Admin entity);
bool Delete(Admin entity);
bool Delete(string code);
int DeleteByKeys(IList<string> keys);
}
}


1.4 ICategoryService接口

namespace MESE.IService.Manage
{
public interface ICategoryService
{
IList<Category> Query(ISQLPaging paging);
IList<Category> QueryByParent(string parent);
IList<Category> QueryAll();
bool Exists(string code);
Category Read(string code);
bool Add(Category entity);
bool Update(Category entity);
bool Delete(Category entity);
bool Delete(string code);
int DeleteByKeys(IList<string> keys);
}
}


2 Service实现类

2.1 RoleService实现类

namespace MESE.Service.Manage
{
public class RoleService : IRoleService
{
public IRoleDao RoleDao { get; set; }

public IList<Role> Query(ISQLPaging paging)
{
int count;
var list = this.RoleDao.Query(paging.QuerySQL, paging.PageIndex, paging.PageSize, out count);
paging.RecordCount = count;
return list;
}
public IList<Role> QueryAll()
{
return this.RoleDao.QueryAll();
}
public bool Exists(string code)
{
return this.RoleDao.Exists(code);
}
public Role Read(string code)
{
return this.RoleDao.Read(code);
}
public bool Add(Role entity)
{
return this.RoleDao.Add(entity);
}
public bool Update(Role entity)
{
return this.RoleDao.Update(entity);
}
public bool Delete(Role entity)
{
return this.RoleDao.Delete(entity);
}
public bool Delete(string code)
{
return this.RoleDao.Delete(code);
}
public int DeleteByKeys(IList<string> keys)
{
return this.RoleDao.DeleteByKeys(keys);
}
}
}


2.2 AdminService实现类

namespace MESE.Service.Manage
{
public class AdminService : IAdminService
{
public IAdminDao AdminDao { get; set; }

public IList<Admin> Query(ISQLPaging paging)
{
int count;
var list = this.AdminDao.Query(paging.QuerySQL, paging.PageIndex, paging.PageSize, out count);
paging.RecordCount = count;
return list;
}
public IList<Admin> QueryAll()
{
return this.AdminDao.QueryAll();
}
public bool Exists(string code)
{
return this.AdminDao.Exists(code);
}
public Admin Read(string code)
{
return this.AdminDao.Read(code);
}
public bool Add(Admin entity)
{
return this.AdminDao.Add(entity);
}
public bool Update(Admin entity)
{
return this.AdminDao.Update(entity);
}
public bool Delete(Admin entity)
{
return this.AdminDao.Delete(entity);
}
public bool Delete(string code)
{
return this.AdminDao.Delete(code);
}
public int DeleteByKeys(IList<string> keys)
{
return this.AdminDao.DeleteByKeys(keys);
}
}
}


2.3 CategoryService实现类

namespace MESE.Service.Manage
{
public class CategoryService : ICategoryService
{
public ICategoryDao CategoryDao { get; set; }

public IList<Category> Query(ISQLPaging paging)
{
int count;
var list = this.CategoryDao.Query(paging.QuerySQL, paging.PageIndex, paging.PageSize, out count);
paging.RecordCount = count;
return list;
}
public IList<Category> QueryByParent(string parent)
{
return this.CategoryDao.QueryByParent(parent);
}
public IList<Category> QueryAll()
{
return this.CategoryDao.QueryAll();
}
public bool Exists(string code)
{
return this.CategoryDao.Exists(code);
}
public Category Read(string code)
{
return this.CategoryDao.Read(code);
}
public bool Add(Category entity)
{
return this.CategoryDao.Add(entity);
}
public bool Update(Category entity)
{
return this.CategoryDao.Update(entity);
}
public bool Delete(Category entity)
{
return this.CategoryDao.Delete(entity);
}
public bool Delete(string code)
{
return this.CategoryDao.Delete(code);
}
public int DeleteByKeys(IList<string> keys)
{
return this.CategoryDao.DeleteByKeys(keys);
}
}
}


可能有人会问,为什么IAdminDao、IRoleDao、ICategoryDao没有创建?为什么没有异常、日志的处理?

IDao层接口是通过Spring注入的,异常日志的处理是通过Spring的Aop来处理的。

实际上,还应该有个“系统日志”记录,我这里没有写,就是对关键信息的变更写入系统数据库,以便后期查询,比如:添加管理员、角色,权限分配等。

下一篇编写DTO,及AutoMapper类库的使用,请关注。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: