DDD领域模型数据访问权限之用户权限(十)

BAS_PRService岗位和角色服务:

  public class BAS_PRService
    {
        //岗位
        private IRepository<BAS_Post> irepositorypost;
        //角色
        private IRepository<BAS_Role> irepositoryrole;
        //岗位和角色
        private IRepository<BAS_PR> irepositorypr;

        BAS_PR bas_pr;

        public BAS_PRService(IRepository<BAS_Post> irepositorypost,
         IRepository<BAS_Role> irepositoryrole,
         IRepository<BAS_PR> irepositorypr)
        {
            this.irepositorypost = irepositorypost;
            this.irepositoryrole = irepositoryrole;
            this.irepositorypr = irepositorypr;

            bas_pr = new BAS_PR(irepositorypr);
        }

        /// <summary>
        /// 将多个岗位添加到角色中
        /// </summary>
        /// <param name="postnames"></param>
        /// <param name="rolename"></param>
        public void CreateBAS_PR(string[] postnames,string rolename)
        {
            var listpost = new List<BAS_Post>();
            for(int i=0;i<postnames.Length;i++)
            {
                var post =
                    irepositorypost.GetByCondition(p => p.Name == postnames[i], p => true)
                    .SingleOrDefault();
                listpost.Add(post);
            }

            var role = irepositoryrole.GetByCondition(p => p.Name == rolename, p => true).SingleOrDefault();
            bas_pr.CreateBAS_PR(listpost, role);
        }

        /// <summary>
        /// 根据岗位名获取PR信息
        /// </summary>
        /// <param name="postname"></param>
        /// <returns></returns>
        public List<BAS_PR> GetPRSByPostName(string postname)
        {
            var post = irepositorypost.GetByCondition(p => p.Name == postname, p => true).
                SingleOrDefault();
            return bas_pr.GetPRSByPost(post);
        }
        /// <summary>
        /// 根据角色名获取PR信息
        /// </summary>
        /// <param name="rolename"></param>
        /// <returns></returns>
        public List<BAS_PR> GetPRSByRoleName(string rolename)
        {
            var role = irepositoryrole.GetByCondition(p => p.Name == rolename, p => true)
                .SingleOrDefault();
            return bas_pr.GetPRSByRole(role);
        }
    }

 在基础机构层DDD.Infrastructure添加SessionHelper的帮助类:

public class SessionHelper
    {
        /// <summary>
        /// 添加 Session
        /// </summary>
        /// <param name="strsessionname"></param>
        /// <param name="strvalue"></param>
        public static void AddSession(string strsessionname,string strvalue)
        {
            HttpContext.Current.Session[strsessionname] = strvalue;
            HttpContext.Current.Session.Timeout = 600;
        }
        /// <summary>
        /// 添加多个Session值
        /// </summary>
        /// <param name="strsessioinname"></param>
        /// <param name="strvalues"></param>
        public static void AddSession(string strsessioinname,string[] strvalues)
        {
            HttpContext.Current.Session[strsessioinname] = strvalues;
            HttpContext.Current.Session.Timeout = 600;
        }
           /// <summary>
           /// 得到Session的值
           /// </summary>
           /// <param name="strsessionname"></param>
           /// <returns></returns>
        public static  string[] Gets(string strsessionname)
        {
            if (HttpContext.Current.Session[strsessionname] == null)
                return null;
            return (string[])HttpContext.Current.Session[strsessionname];
        }
    }

 在工程DDD.Domain.DomainService中新建:BAS_RoleService服务类:

 

   public class BAS_RoleService
    {
        //角色
        private IRepository<BAS_Role> irepositoryrole;
        //用户角色
        private IRepository<BAS_User> irepositoryuser;
        //部门
        private IRepository<BAS_Department> irepositorydepartment;
        //岗位
        private IRepository<BAS_Post> irepositorypost;
        //标识容器
        private IRepository<BAS_IdentityContianer> irepositoryidentitycontainer;
        //角色和用户
        private IRepository<BAS_UR> irepositoryur;
        //角色和部门
        private IRepository<BAS_DR> irepositorydr;
        //角色和岗位
        private IRepository<BAS_PR> irepositorypr;

        BAS_Role bas_role;

        //构造函数
        public BAS_RoleService(IRepository<BAS_Role> irepositoryrole,
         IRepository<BAS_User> irepositoryuser,
         IRepository<BAS_Department> irepositorydepartment,
         IRepository<BAS_Post> irepositorypost,
         IRepository<BAS_IdentityContianer> irepositoryidentitycontainer,
         IRepository<BAS_UR> irepositoryur,
         IRepository<BAS_DR> irepositorydr,
         IRepository<BAS_PR> irepositorypr)
        {
            this.irepositoryrole = irepositoryrole;
            this.irepositoryidentitycontainer = irepositoryidentitycontainer;
            this.irepositoryuser = irepositoryuser;
            this.irepositorydepartment = irepositorydepartment;
            this.irepositorypost = irepositorypost;
            this.irepositoryur = irepositoryur;
            this.irepositorydr = irepositorydr;
            this.irepositorypr = irepositorypr;

            bas_role = new BAS_Role(irepositoryrole);
        }

        /// <summary>
        /// 创建角色
        /// </summary>
        /// <param name="name"></param>
        /// <param name="description"></param>
        public void CreateRole(string name,string description)
        {
            var con_id = Guid.NewGuid();
            bas_role.CreateRole(name, description, con_id);

            var bas_identitycontainer = new BAS_IdentityContianer(irepositoryidentitycontainer);
            bas_identitycontainer.CreateIdentityContainer(con_id);
        }
     
        /// <summary>
        /// 将多个用户添加到角色
        /// </summary>
        /// <param name="usernos"></param>
        /// <param name="rolename"></param>
        public void AddUserToRole(List<string> usernos,string rolename)
        {
            var role = bas_role.GetRoleByName(rolename);
            var users = new List<BAS_User>();
            foreach(var userno in usernos)
            {
                var user = new BAS_User(irepositoryuser).GetUserByNo(userno);
                users.Add(user);
            }
            new BAS_UR(irepositoryur).CreateBAS_UR(users, role);
        }

      
        /// <summary>
        /// 将多个部门添加到角色
        /// </summary>
        /// <param name="departmentnames"></param>
        /// <param name="rolename"></param>
        public void AddDepartmentToRole(List<string> departmentnames,string rolename)
        {
            var role = bas_role.GetRoleByName(rolename);
            var departments = new List<BAS_Department>();
            foreach(var departmentname in departmentnames)
            {
                var department = new BAS_Department(irepositorydepartment).GetDepartmentByName(departmentname);
                departments.Add(department);
            }

            new BAS_DR(irepositorydr).CreateBAS_DR(departments, role);
        }

        /// <summary>
        /// 将多个岗位添加到角色
        /// </summary>
        /// <param name="postnames"></param>
        /// <param name="rolename"></param>
        public void AddPostToRole(List<string> postnames,string rolename)
        {
            var role = bas_role.GetRoleByName(rolename);
            var posts = new List<BAS_Post>();
            foreach(var postname in postnames)
            {
                var post = new BAS_Post(irepositorypost).GetPostByName(postname);
                posts.Add(post);
            }

            new BAS_PR(irepositorypr).CreateBAS_PR(posts, role);
        }

        /// <summary>
        /// 根据用户NO获取用户所属角色
        /// </summary>
        /// <param name="no"></param>
        /// <returns></returns>
        public List<BAS_Role> GetRoleByUserNo(string no)
        {
            var urs =
                new BAS_URService(irepositoryuser, irepositoryrole, irepositoryur)
                .GetURSByUserNo(no);
            var roles = new List<BAS_Role>();
            foreach(var ur in urs)
            {
                var role =
                    irepositoryrole.GetByCondition(p => p.Id == ur.BAS_Role.Id, p => true).SingleOrDefault();
                roles.Add(role);
            }
            return roles;
        }
        /// <summary>
        /// 用户是否在一个角色中
        /// </summary>
        /// <param name="no"></param>
        /// <param name="rolename"></param>
        /// <returns></returns>
        public bool IsRoleContainUser(string no,string rolename)
        {
            return GetRoleByUserNo(no).Contains(bas_role.GetRoleByName(rolename));
        }

        

        /// <summary>
        /// 根据部门名获取所属的角色
        /// </summary>
        /// <param name="departmentname"></param>
        /// <returns></returns>
        public List<BAS_Role > GetRoleByDepartmentName(string departmentname)
        {
            var drs =
                new BAS_DRService(irepositorydepartment, irepositoryrole, irepositorydr)
                .GetDRSByDepartmentName(departmentname);
            var roles = new List<BAS_Role>();
            foreach(var dr in drs)
            {
                var role =
                    irepositoryrole.GetByCondition(p => p.Id == dr.BAS_Role.Id, p => true)
                    .SingleOrDefault();
                roles.Add(role);
            }

            return roles;
        }
        /// <summary>
        /// 某一个部门是否在一个角色中
        /// </summary>
        /// <param name="departmentname"></param>
        /// <param name="rolename"></param>
        /// <returns></returns>
        public bool IsRoleContainDepartment(string departmentname,string rolename)
        {
            return GetRoleByDepartmentName(departmentname).Contains(bas_role.GetRoleByName(rolename));
        }
       
        /// <summary>
        /// 根据岗位名获取所属角色信息
        /// </summary>
        /// <param name="postname"></param>
        /// <returns></returns>
        public List<BAS_Role> GetRoleByPostName(string postname)
        {
            var prs =
                new BAS_PRService(irepositorypost, irepositoryrole, irepositorypr)
                .GetPRSByPostName(postname);
            var roles = new List<BAS_Role>();
            foreach(var pr in prs)
            {
                var role =
                    irepositoryrole.GetByCondition(p => p.Id == pr.BAS_Role.Id, p => true)
                    .SingleOrDefault();
                roles.Add(role);
            }
            return roles;
        }
        /// <summary>
        /// 判断岗位是否在角色中
        /// </summary>
        /// <param name="postname"></param>
        /// <param name="rolename"></param>
        /// <returns></returns>
        public bool IsRoleContainPost(string postname,string rolename)
        {
            return GetRoleByPostName(postname).Contains(bas_role.GetRoleByName(rolename));
        }
    }

 

   创建DomainService服务类:

  public class BAS_UserService
    {
        //用户仓储
        private IRepository<BAS_User> irepositoryuser;
        //标识容器
        private IRepository<BAS_IdentityContianer> irepositoryidentitycontainer;
        //角色
        private IRepository<BAS_Role> irepositoryrole;
        //部门信息
        private IRepository<BAS_Department> irepositorydepartment;
        //岗位信息
        private IRepository<BAS_Post> irepositorypost;
        //用户角色
        private IRepository<BAS_UR> irepositoryur;
        //用户对应的岗位
        private IRepository<BAS_UDPSet> irepositoryudp;
        //部门对应的角色
        private IRepository<BAS_DR> irepositorydr;
        //岗位对应的角色
        private IRepository<BAS_PR> irepositorypr;

        BAS_User bas_user;

        public BAS_UserService(IRepository<BAS_User> irepositoryuser,
        IRepository<BAS_IdentityContianer> irepositoryidentitycontainer,
        IRepository<BAS_Role> irepositoryrole,
        IRepository<BAS_Department> irepositorydepartment,
        IRepository<BAS_Post> irepositorypost,
        IRepository<BAS_UR> irepositoryur,
        IRepository<BAS_UDPSet> irepositoryudp,
        IRepository<BAS_DR> irepositorydr,
        IRepository<BAS_PR> irepositorypr)
        {
            this.irepositoryuser = irepositoryuser;
            this.irepositoryrole = irepositoryrole;
            this.irepositorydepartment = irepositorydepartment;
            this.irepositorypost = irepositorypost;
            this.irepositoryidentitycontainer = irepositoryidentitycontainer;
            this.irepositoryur = irepositoryur;
            this.irepositoryudp = irepositoryudp;
            this.irepositorydr = irepositorydr;
            this.irepositorypr = irepositorypr;

            bas_user = new BAS_User(irepositoryuser);
        }

        /// <summary>
        /// 创建用户
        /// </summary>
        /// <param name="no"></param>
        /// <param name="name"></param>
        /// <param name="mobile"></param>
        /// <param name="password"></param>
        public void CreateUser(string no,string name,string mobile,string password)
        {
            var con_id = Guid.NewGuid();
            bas_user.CreateUser(no, name, mobile, con_id,password);

            var bas_identitycontainer = new BAS_IdentityContianer(irepositoryidentitycontainer);
            bas_identitycontainer.CreateIdentityContainer(con_id);

        }
        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="no"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public bool UserLogin(string no,string password)
        {
            //用户登录验证
            var loginresult = bas_user.LoginVerify(no, password);
           
            if(loginresult)
            {
                SessionHelper.AddSession("UserNo", no);
                var conids = GetUserAllCon_Id(no);
                var sconids = new string[conids.Count];
                for(int i=0;i<conids.Count;i++)
                {
                    sconids[i] = conids[i].ToString();
                }
                SessionHelper.AddSession("UserConId", sconids);
            }
            return loginresult;
        }
        /// <summary>
        /// 得到用户的所有ID
        /// </summary>
        /// <param name="no"></param>
        /// <returns></returns>
        private List<Guid> GetUserAllCon_Id(string no)
        {
            var userallcon_ids = new List<Guid>();
            //获取用户自身的Con_id
            userallcon_ids.Add(bas_user.GetUserByNo(no).Con_Id);
            
            // 获取用户所属角色的Con_id
            var roleservice = new BAS_RoleService(irepositoryrole, irepositoryuser,
                irepositorydepartment, irepositorypost, irepositoryidentitycontainer, irepositoryur,
                irepositorydr, irepositorypr);
            //获取用户所属的角色
            var userroles = roleservice.GetRoleByUserNo(no);
            userallcon_ids.AddRange(userroles.Select(p => p.Con_Id));
            //获取用户所属的部门
            var departmentservice = new BAS_DepartmentService(irepositorydepartment,
                irepositoryidentitycontainer, irepositoryudp, irepositoryuser);
            var userdepartments = departmentservice.GetDepartmentByUserNo(no);
            userallcon_ids.AddRange(userdepartments.Select(p => p.Con_Id));
            //获取部门所属的角色
            foreach(var userdepartment in userdepartments)
            {
                var roles = roleservice.GetRoleByDepartmentName(userdepartment.Name);
                userallcon_ids.AddRange(roles.Select(p => p.Con_Id));
            }
            //获取用户所属的岗位
            var postservice = new BAS_PostService(irepositorypost, irepositoryidentitycontainer,
                irepositoryuser, irepositoryudp);
            var userposts = postservice.GetPostsByUserNO(no);
            userallcon_ids.AddRange(userposts.Select(p => p.Con_Id));

            foreach (var post in userposts)
            {
                var roles = roleservice.GetRoleByPostName(post.Name);
                userallcon_ids.AddRange(roles.Select(p => p.Con_Id));
            }

            return userallcon_ids;
        }
    }

 添加应用层的服务:DDD.Application工程  (DepartmentAppService)

  public class DepartmentAppService
    {
        IRepositoryContext context = ServiecLocator.Instance.GetService(typeof(IRepositoryContext))
          as IRepositoryContext;
        //部门仓储
        IRepository<BAS_Department> departmentrepository = ServiecLocator.Instance.GetService(typeof(IRepository<BAS_Department>))
            as IRepository<BAS_Department>;
        //标识容器仓储
        IRepository<BAS_IdentityContianer> identitycontainerrepository = ServiecLocator.Instance.GetService(typeof(IRepository<BAS_IdentityContianer>))
    as IRepository<BAS_IdentityContianer>;
        //用户岗位部门仓储
        IRepository<BAS_UDPSet> udprepository = ServiecLocator.Instance.GetService(typeof(IRepository<BAS_UDPSet>))
as IRepository<BAS_UDPSet>;
        //用户仓储
        IRepository<BAS_User> userrepository = ServiecLocator.Instance.GetService(typeof(IRepository<BAS_User>))
as IRepository<BAS_User>;

        //调用领域服务
        BAS_DepartmentService departmentservice;
        BAS_Department bas_department;

        //构造函数
        public DepartmentAppService()
        {
            departmentservice = new BAS_DepartmentService(departmentrepository,
                identitycontainerrepository, udprepository, userrepository);
            bas_department = new BAS_Department(departmentrepository);
        }

        /// <summary>
        /// 部门创建
        /// </summary>
        /// <param name="name"></param>
        /// <param name="description"></param>
        public void CreateDepartment(string name,string description)
        {
            departmentservice.CreateDepartment(name, description);
            context.Commit();
        }

        /// <summary>
        /// 根据部门名获取部门信息
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public BAS_Department GetDepartmentByName(string name)
        {
            return bas_department.GetDepartmentByName(name);
        }

    }

 PostAppService应用层服务:

 public class PostAppService
    {
        IRepositoryContext context = ServiecLocator.Instance.GetService(typeof(IRepositoryContext))
         as IRepositoryContext;
        IRepository<BAS_Post> postrepository = ServiecLocator.Instance.GetService(typeof(IRepository<BAS_Post>))
            as IRepository<BAS_Post>;
        IRepository<BAS_IdentityContianer> identitycontainerrepository = ServiecLocator.Instance.GetService(typeof(IRepository<BAS_IdentityContianer>))
    as IRepository<BAS_IdentityContianer>;
        IRepository<BAS_User> userrepository = ServiecLocator.Instance.GetService(typeof(IRepository<BAS_User>))
as IRepository<BAS_User>;
        IRepository<BAS_UDPSet> udprepository = ServiecLocator.Instance.GetService(typeof(IRepository<BAS_UDPSet>))
as IRepository<BAS_UDPSet>;

        BAS_PostService postservice;
        BAS_Post bas_post;

        public PostAppService()
        {
            postservice = new BAS_PostService(postrepository, identitycontainerrepository, userrepository,
                udprepository);
            bas_post = new BAS_Post(postrepository);
        }

        /// <summary>
        /// 岗位创建
        /// </summary>
        /// <param name="name"></param>
        /// <param name="description"></param>
        public void CreatePost(string name,string description)
        {
            postservice.CreatePost(name, description);
            context.Commit();
        }
        /// <summary>
        /// 根据岗位名返回岗位对象
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public BAS_Post GetPostByName(string name)
        {
            return bas_post.GetPostByName(name);
        }
    }

 添加RoleAppService应用层的服务:

public class RoleAppService
    {
        IRepositoryContext context = ServiecLocator.Instance.GetService(typeof(IRepositoryContext))
          as IRepositoryContext;
        IRepository<BAS_Department> departmentrepository = ServiecLocator.Instance.GetService(typeof(IRepository<BAS_Department>))
            as IRepository<BAS_Department>;
        IRepository<BAS_IdentityContianer> identitycontainerrepository = ServiecLocator.Instance.GetService(typeof(IRepository<BAS_IdentityContianer>))
    as IRepository<BAS_IdentityContianer>;
        IRepository<BAS_Role> rolerepository = ServiecLocator.Instance.GetService(typeof(IRepository<BAS_Role>))
as IRepository<BAS_Role>;
        IRepository<BAS_User> userrepository = ServiecLocator.Instance.GetService(typeof(IRepository<BAS_User>))
as IRepository<BAS_User>;
        IRepository<BAS_Post> postrepository = ServiecLocator.Instance.GetService(typeof(IRepository<BAS_Post>))
as IRepository<BAS_Post>;
        IRepository<BAS_UR> urrepository = ServiecLocator.Instance.GetService(typeof(IRepository<BAS_UR>))
as IRepository<BAS_UR>;
        IRepository<BAS_DR> drrepository = ServiecLocator.Instance.GetService(typeof(IRepository<BAS_DR>))
as IRepository<BAS_DR>;
        IRepository<BAS_PR> prrepository = ServiecLocator.Instance.GetService(typeof(IRepository<BAS_PR>))
as IRepository<BAS_PR>;

        BAS_RoleService roleserivice;
        BAS_Role bas_role;
        public RoleAppService()
        {
            roleserivice =
                new BAS_RoleService(rolerepository, userrepository, departmentrepository,
                postrepository, identitycontainerrepository, urrepository,
                drrepository, prrepository);
            bas_role = new BAS_Role(rolerepository);
        }

        /// <summary>
        /// 角色创建
        /// </summary>
        /// <param name="name"></param>
        /// <param name="description"></param>
        public void CreateRole(string name,string description)
        {
            roleserivice.CreateRole(name, description);
            context.Commit();
        }
        /// <summary>
        /// 根据角色名返回角色
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public BAS_Role GetRoleByName(string name)
        {
            return bas_role.GetRoleByName(name);
        }

        /// <summary>
        /// 添加用户到角色
        /// </summary>
        /// <param name="usernos"></param>
        /// <param name="rolename"></param>
        public void AddUserToRole(string [] usernos,string rolename)
        {
            roleserivice.AddUserToRole(usernos.ToList(), rolename);
            context.Commit();
        }

        /// <summary>
        /// 添加部门到角色
        /// </summary>
        /// <param name="departmentnames"></param>
        /// <param name="rolename"></param>
        public void AddDepartmentToRole(string [] departmentnames,string rolename)
        {
            roleserivice.AddDepartmentToRole(departmentnames.ToList(), rolename);
            context.Commit();
        }

        /// <summary>
        /// 添加岗位到角色
        /// </summary>
        /// <param name="postnames"></param>
        /// <param name="rolename"></param>
        public void AddPostToRole(string [] postnames,string rolename)
        {
            roleserivice.AddPostToRole(postnames.ToList(), rolename);
            context.Commit();
        }

        /// <summary>
        /// 角色是否包含某个用户
        /// </summary>
        /// <param name="userno"></param>
        /// <param name="rolename"></param>
        /// <returns></returns>
        public bool IsRoleContainUser(string userno,string rolename)
        {
            return roleserivice.IsRoleContainUser(userno, rolename);
        }
        /// <summary>
        /// 角色是否包含某个部门
        /// </summary>
        /// <param name="departmentname"></param>
        /// <param name="rolename"></param>
        /// <returns></returns>
        public bool IsRoleContainDepartment(string departmentname,string rolename)
        {
            return roleserivice.IsRoleContainDepartment(departmentname, rolename);
        }
        /// <summary>
        /// 角色是否包含某个岗位
        /// </summary>
        /// <param name="postname"></param>
        /// <param name="rolename"></param>
        /// <returns></returns>
        public bool IsRoleContainPost(string postname,string rolename)
        {
            return roleserivice.IsRoleContainPost(postname, rolename);
        }
    }

 UserAppService应用层的服务:

public class UserAppService
    {
        IRepositoryContext context = ServiecLocator.Instance.GetService(typeof(IRepositoryContext))
          as IRepositoryContext;
        IRepository<BAS_Department> departmentrepository = ServiecLocator.Instance.GetService(typeof(IRepository<BAS_Department>))
            as IRepository<BAS_Department>;
        IRepository<BAS_IdentityContianer> identitycontainerrepository = ServiecLocator.Instance.GetService(typeof(IRepository<BAS_IdentityContianer>))
    as IRepository<BAS_IdentityContianer>;
        IRepository<BAS_Role> rolerepository = ServiecLocator.Instance.GetService(typeof(IRepository<BAS_Role>))
as IRepository<BAS_Role>;
        IRepository<BAS_User> userrepository = ServiecLocator.Instance.GetService(typeof(IRepository<BAS_User>))
as IRepository<BAS_User>;
        IRepository<BAS_Post> postrepository = ServiecLocator.Instance.GetService(typeof(IRepository<BAS_Post>))
as IRepository<BAS_Post>;
        IRepository<BAS_UR> urrepository = ServiecLocator.Instance.GetService(typeof(IRepository<BAS_UR>))
as IRepository<BAS_UR>;
        IRepository<BAS_DR> drrepository = ServiecLocator.Instance.GetService(typeof(IRepository<BAS_DR>))
as IRepository<BAS_DR>;
        IRepository<BAS_PR> prrepository = ServiecLocator.Instance.GetService(typeof(IRepository<BAS_PR>))
as IRepository<BAS_PR>;
        IRepository<BAS_UDPSet> udprepository = ServiecLocator.Instance.GetService(typeof(IRepository<BAS_UDPSet>))
as IRepository<BAS_UDPSet>;

        BAS_UserService bas_userservice;
        BAS_User bas_user;
        BAS_UDPService udpservice;

        public UserAppService()
        {
            bas_userservice =
                new BAS_UserService(userrepository, identitycontainerrepository, rolerepository,
                departmentrepository, postrepository, urrepository, udprepository, drrepository, prrepository);
            bas_user = new BAS_User(userrepository);
            udpservice = new BAS_UDPService(udprepository, userrepository, departmentrepository,
                postrepository);
        }

        /// <summary>
        /// 用户创建
        /// </summary>
        /// <param name="no"></param>
        /// <param name="name"></param>
        /// <param name="mobile"></param>
        /// <param name="password"></param>
        public void CreateUser(string no,string name,string mobile,string password)
        {
            bas_userservice.CreateUser(no, name, mobile, password);
            context.Commit();
        }
        /// <summary>
        /// 根据用户NO获取用户对象
        /// </summary>
        /// <param name="no"></param>
        /// <returns></returns>
        public BAS_User GetUserByNo(string no)
        {
            return bas_user.GetUserByNo(no);
        }
        /// <summary>
        /// 将用户添加到部门与岗位
        /// </summary>
        /// <param name="userno"></param>
        /// <param name="departmentname"></param>
        /// <param name="postname"></param>
        /// <param name="ismain"></param>
        public void CreateDepartmentPostToUser(string userno,string departmentname,string postname
            ,bool ismain)
        {
            udpservice.CreateDepartmentPostToUser(userno, departmentname, postname, ismain);
            context.Commit();
        }
        /// <summary>
        /// 根据部门,用户与岗位信息获取三者连接信息
        /// </summary>
        /// <param name="departmentname"></param>
        /// <param name="userno"></param>
        /// <param name="postname"></param>
        /// <returns></returns>
        public List<BAS_UDPSet> GetUDPByDepartmentUserPost(string departmentname,string userno,
            string postname)
        {
            return udpservice.GetUdpByDepartment(departmentname)
                .Concat(udpservice.GetUdpByPost(postname)).Concat
                (udpservice.GetUdpByUser(userno)).ToList();
        }
    }

 如何在T4模板中生成同一个上下文:

添加一下代码:
public partial class SalesOrdersModelContainer : DbContext

 添加WebApp连接数据库的配置:

 <connectionStrings>
    <add name="SalesOrdersModelContainer" 
connectionString=
"metadata=res://*/SalesOrdersModel.csdl|res://*/SalesOrdersModel.ssdl|
res://*/SalesOrdersModel.msl|res://*/PermissionModel.csdl|res://*/PermissionModel.ssdl|res://*/PermissionModel.msl;
provider=System.Data.SqlClient;
provider connection string="data source=localhost;initial catalog=SalesOrderDB;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework""
providerName="System.Data.EntityClient" /> </connectionStrings>

 单元测试代码:

namespace DDD.UnitTest
{
    [TestClass]
    public class PermissionTest
    {
        [TestMethod]
        public void CreateUser()
        {
            UserAppService userappservice =
                new UserAppService();
            userappservice.CreateUser("10", "孙", "1359333", "pass");
            Assert.IsNotNull(userappservice.GetUserByNo("10"));
        }

        [TestMethod]
        public void CreateDepartment()
        {
            DepartmentAppService departmentservice =
                new DepartmentAppService();
            departmentservice.CreateDepartment("财务中心", "进行财务核算与管理");
            Assert.IsNotNull(departmentservice.GetDepartmentByName("财务中心"));
        }

        [TestMethod]
        public void CreatePost()
        {
            PostAppService postservice = new PostAppService();
            postservice.CreatePost("高级会计师", "高级会计师");
            Assert.IsNotNull(postservice.GetPostByName("高级会计师"));
        }

        [TestMethod]
        public void CreateDepartmentPostToUser()
        {
            UserAppService userservice = new UserAppService();
            userservice.CreateDepartmentPostToUser("10", "财务中心", "高级会计师", true);
            Assert.IsNotNull(userservice.GetUDPByDepartmentUserPost("财务中心", "10", "高级会计师"));
        }

        [TestMethod]
        public void CreateRole()
        {
            RoleAppService roleservice =
                new RoleAppService();
            roleservice.CreateRole("角色1", "具有某些权限的角色");
            Assert.IsNotNull(roleservice.GetRoleByName("角色1"));
        }

        [TestMethod]
        public void AddUserToRole()
        {
            RoleAppService roleservice = new RoleAppService();
            string[] usernos = new string[1];
            usernos[0] = "10";
            roleservice.AddUserToRole(usernos, "角色1");
            Assert.IsTrue(roleservice.IsRoleContainUser("10", "角色1"));
        }

        [TestMethod]
        public void AddDepartmentToRole()
        {
            RoleAppService roleservice = new RoleAppService();
            string[] departmentnames = new string[1];
            departmentnames[0] = "财务中心";
            roleservice.AddDepartmentToRole(departmentnames, "角色1");
            Assert.IsTrue(roleservice.IsRoleContainDepartment("财务中心", "角色1"));
        }

        [TestMethod]
        public void AddPostToRole()
        {
            RoleAppService roleservice = new RoleAppService();
            string[] postnames = new string[1];
            postnames[0] = "高级会计师";
            roleservice.AddPostToRole(postnames, "角色1");
            Assert.IsTrue(roleservice.IsRoleContainPost("高级会计师", "角色1"));
        }
    }
}

 

转载于:https://www.cnblogs.com/sunliyuan/p/7868626.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值