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

22 篇文章 0 订阅
16 篇文章 0 订阅

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

与业务系统不同的是,业务系统可以根据不同的用户群开发单独的系统,但小型项目不需要。为了在一个项目中开发针对两个用户群的“网站”,我使用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接口:

    public interface ISQLPaging
    {
        int PageIndex { get; }
        int PageSize { get; }
        int RecordCount { get; set; }
        string QuerySQL { get; }
    }

1.2 IRoleService 接口

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

    public interface IRoleService
    {
        IList<Role> Query(ISQLPaging paging);
        IList<Role> QueryAll();
        bool Exists(string code);
        Role Read(string code);
        bool Add(RoleDTO dto);
        bool Update(RoleDTO dto);
        bool Delete(string code);
        int DeleteByKeys(IList<string> keys);
    }

1.3 IAdminService接口

与IRoleService接口差不多:

    public interface IAdminService
    {
        IList<Admin> Query(ISQLPaging paging);
        IList<Admin> QueryAll();
        bool Exists(string code);
        Admin Read(string code);
        bool Add(AdminDTO dto);
        bool Update(AdminDTO dto);
        bool Delete(string code);
        int DeleteByKeys(IList<string> keys);
    }

1.4 ICategoryService接口

    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(CategoryDTO dto);
        bool Update(CategoryDTO dto);
        bool Delete(string code);
        int DeleteByKeys(IList<string> keys);
    }


1.5 IAuthorizeService 接口

    public interface IAuthorizeService
    {
        bool Update(AuthorizeDTO dto);
    }

1.6 IUploadService 接口

    public interface IUploadService
    {
        IList<Upload> Query(ISQLPaging paging);
        IList<Upload> QueryAll();
        bool Exists(string code);
        Upload Read(string code);
        bool Add(UploadDTO dto);
        bool Update(UploadDTO dto);
        bool Delete(string code);
        int DeleteByKeys(IList<string> keys);
    }


2 Service实现类

2.1 RoleService实现类

    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(RoleDTO dto)
        {
            Mapper.CreateMap<RoleDTO, Role>();
            return this.RoleDao.Add(Mapper.Map<RoleDTO, Role>(dto));
        }
        public bool Update(RoleDTO dto)
        {
            Mapper.CreateMap<RoleDTO, Role>();
            return this.RoleDao.Update(Mapper.Map<RoleDTO, Role>(dto));
        }
        public bool Delete(string code)
        {
            return this.RoleDao.Delete(code);
        }
        public int DeleteByKeys(IList<string> keys)
        {
            return this.RoleDao.DeleteByKeys(keys);
        }
    }

2.2 AdminService实现类

    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(AdminDTO dto)
        {
            Mapper.CreateMap<AdminDTO, Admin>();
            return this.AdminDao.Add(Mapper.Map<AdminDTO, Admin>(dto));
        }
        public bool Update(AdminDTO dto)
        {
            Mapper.CreateMap<AdminDTO, Admin>();
            return this.AdminDao.Update(Mapper.Map<AdminDTO, Admin>(dto));
        }
        public bool Delete(string code)
        {
            return this.AdminDao.Delete(code);
        }
        public int DeleteByKeys(IList<string> keys)
        {
            return this.AdminDao.DeleteByKeys(keys);
        }
    }

2.3 CategoryService实现类

    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(CategoryDTO dto)
        {
            Mapper.CreateMap<CategoryDTO, Category>();
            return this.CategoryDao.Add(Mapper.Map<CategoryDTO, Category>(dto));
        }
        public bool Update(CategoryDTO dto)
        {
            Mapper.CreateMap<CategoryDTO, Category>();
            return this.CategoryDao.Update(Mapper.Map<CategoryDTO, Category>(dto));
        }
        public bool Delete(string code)
        {
            return this.CategoryDao.Delete(code);
        }
        public int DeleteByKeys(IList<string> keys)
        {
            return this.CategoryDao.DeleteByKeys(keys);
        }
    }


2.4 AuthorizeService 实现类

    public class AuthorizeService : IAuthorizeService
    {
        public IRoleDao RoleDao { get; set; }
        public ICategoryDao CategoryDao { get; set; }

        public bool Update(AuthorizeDTO dto)
        {
            this.RoleDao.EnableTrack = true;
            this.CategoryDao.EnableTrack = true;
            Role role = this.RoleDao.Read(dto.RoleCode);
            role.Categories.Clear();
            if (dto.CategoryCodes != null && dto.CategoryCodes.Count > 0)
            {
                foreach (var code in dto.CategoryCodes)
                {
                    role.Categories.Add(this.CategoryDao.Read(code));
                }
            }
            return this.RoleDao.SaveChanges() > 0;
        }
    }

2.5 UploadService 实现类

    public class UploadService : IUploadService
    {
        public IUploadDao UploadDao { get; set; }

        public IList<Upload> Query(ISQLPaging paging)
        {
            int count;
            var list = this.UploadDao.Query(paging.QuerySQL, paging.PageIndex, paging.PageSize, out count);
            paging.RecordCount = count;
            return list;
        }
        public IList<Upload> QueryAll()
        {
            return this.UploadDao.QueryAll();
        }
        public bool Exists(string code)
        {
            return this.UploadDao.Exists(code);
        }
        public Upload Read(string code)
        {
            return this.UploadDao.Read(code);
        }
        public bool Add(UploadDTO dto)
        {
            Mapper.CreateMap<UploadDTO, Upload>();
            return this.UploadDao.Add(Mapper.Map<UploadDTO, Upload>(dto));
        }
        public bool Update(UploadDTO dto)
        {
            Mapper.CreateMap<UploadDTO, Upload>();
            return this.UploadDao.Update(Mapper.Map<UploadDTO, Upload>(dto));
        }
        public bool Delete(string code)
        {
            return this.UploadDao.Delete(code);
        }
        public int DeleteByKeys(IList<string> keys)
        {
            return this.UploadDao.DeleteByKeys(keys);
        }
    }

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

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

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


下一篇编写DTO,及AutoMapper类库的使用,请关注。


  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值