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

网友投稿 254 2022-11-30

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

实现权限的领域对象:BAS_Permission

public partial class BAS_Permission:AggreateRoot { private IRepository irepository; public BAS_Permission(IRepository irepository) { this.irepository = irepository; } ///

/// 创建权限 /// /// /// [Name,UnitPrice] /// 0 /// ["Filed":"UntiPrice","Operator":"Equals","Value":"5000"] /// /// public void CreatePermission(string name,string codeproperty,OperationType operationtype, string value,string description,Guid per_id) { var bas_permission = new BAS_Permission(); bas_permission.Id = base.Id; bas_permission.Name = name; bas_permission.Description = description; bas_permission.CodeProperty = codeproperty; bas_permission.CodeValue = value; bas_permission.Per_Id = per_id; irepository.Create(bas_permission); } /// /// 根据权限名获取权限信息 /// /// /// public BAS_Permission GetPermissionByName(string name) { return irepository.GetByCondition(p => p.Name == name, p => true) .SingleOrDefault(); } }

权限集合的领域对象:BAS_PermissionSet

public partial class BAS_PermissionSet:AggreateRoot { private IRepository irepository; public BAS_PermissionSet(IRepository irepository) { this.irepository = irepository; } ///

/// 创建权限集对象 /// /// /// /// public void CreatePermissionSet(string name,string description,Guid per_id) { var bas_permissionset = new BAS_PermissionSet(); bas_permissionset.Id = base.Id; bas_permissionset.Name = name; bas_permissionset.Description = description; bas_permissionset.Per_Id = per_id; irepository.Create(bas_permissionset); } /// /// 根据权限集名获取权限集信息 /// /// /// public BAS_PermissionSet GetPermissionSetByName(string name) { return irepository.GetByCondition(p => p.Name == name, p => true).SingleOrDefault(); } }

权限和权限集合对应的关系:

public partial class BAS_PPSet:AggreateRoot { private IRepository irepository; public BAS_PPSet(IRepository irepository) { this.irepository = irepository; } public BAS_PPSet() { } ///

/// 添加多个权限到权限集中 /// /// /// public void CreateBAS_PPS(List permissions,BAS_PermissionSet permissionset) { foreach(var permission in permissions) { var bas_ppset = new BAS_PPSet(); bas_ppset.Id = base.Id; bas_ppset.BAS_Permission = permission; bas_ppset.BAS_PermissionSet = permissionset; irepository.Create(bas_ppset); } } /// /// 根据权限查找PPSET /// /// /// public List GetppsetByPermission(BAS_Permission permission) { return irepository.GetByCondition(p => p.BAS_Permission.Id == permission.Id, p => true); } /// /// 根据权限集查找PPSET /// /// /// public List GetppsetByPermissionSet(BAS_PermissionSet permissionset) { return irepository.GetByCondition(p => p.BAS_PermissionSet.Id == permissionset.Id, p => true); } }

权限容器服务:

public partial class BAS_PermissionConatiner:AggreateRoot { private IRepository irepository; public BAS_PermissionConatiner(IRepository irepository) { this.irepository = irepository; } public void CreatePermissionContainer(Guid per_id) { var bas_permissioncontainer = new BAS_PermissionConatiner(); bas_permissioncontainer.Id = per_id; irepository.Create(bas_permissioncontainer); } }

权限服务:BAS_PermissionService

public class BAS_PermissionService { private IRepository irepositorypermission; private IRepository irepositorypermissioncontainer; public BAS_PermissionService(IRepository irepositorypermission, IRepository irepositorypermissioncontainer) { this.irepositorypermission = irepositorypermission; this.irepositorypermissioncontainer = irepositorypermissioncontainer; } ///

/// 创建权限 /// /// /// /// /// /// public void CreatePermission(string name,string codeproperty,OperationType operationtype,string value,string description) { var bas_permission = new BAS_Permission(irepositorypermission); var per_id = Guid.NewGuid(); bas_permission.CreatePermission(name, codeproperty, operationtype, value, description, per_id); var bas_permissioncontainer = new BAS_PermissionConatiner(irepositorypermissioncontainer); bas_permissioncontainer.CreatePermissionContainer(per_id); } }

BAS_PPSetService

public class BAS_PPSetService { private IRepository irepositorypermission; private IRepository irepositorypermissionset; private IRepository irepositoryppset; BAS_PPSet bas_ppset; public BAS_PPSetService(IRepository irepositorypermission, IRepository irepositorypermissionset, IRepository irepositoryppset) { this.irepositorypermission = irepositorypermission; this.irepositorypermissionset = irepositorypermissionset; this.irepositoryppset = irepositoryppset; bas_ppset = new BAS_PPSet(irepositoryppset); } ///

/// 创建多个权限到权限集中 /// /// /// public void CreateBAS_PPS(string[] permissionnames,string permissionsetname) { var listpermission = new List(); for(int i=0;i p.Name == permissionnames[i], p => true).SingleOrDefault(); listpermission.Add(permission); } var permissionset = irepositorypermissionset.GetByCondition(p => p.Name == permissionsetname, p => true).SingleOrDefault(); bas_ppset.CreateBAS_PPS(listpermission, permissionset); } /// /// 根据权限名获取PPSET /// /// /// public List GEtppsetbyPermissionName(string permissionname) { var permission = irepositorypermission.GetByCondition(p => p.Name == permissionname, p => true) .SingleOrDefault(); return bas_ppset.GetppsetByPermission(permission); } /// /// 根据权限集名获取PPSET /// /// /// public List GEtppsetbyPermissionSetName(string permissionsetname) { var permissionset = irepositorypermissionset.GetByCondition(p => p.Name == permissionsetname, p => true) .SingleOrDefault(); return bas_ppset.GetppsetByPermissionSet(permissionset); } }

BAS_PermissionSetService:

public class BAS_PermissionSetService { private IRepository irepositorypermissionset; private IRepository irepositorypermission; private IRepository irepositoryppset; private IRepository irepositorypermissioncontainer; BAS_PermissionSet bas_permissionset; BAS_PPSet bas_ppset; public BAS_PermissionSetService(IRepository irepositorypermissionset, IRepository irepositorypermission, IRepository irepositoryppset, IRepository irepositorypermissioncontainer) { this.irepositorypermission = irepositorypermission; this.irepositorypermissioncontainer = irepositorypermissioncontainer; this.irepositorypermissionset = irepositorypermissionset; this.irepositoryppset = irepositoryppset; bas_permissionset = new BAS_PermissionSet(irepositorypermissionset); bas_ppset = new BAS_PPSet(irepositoryppset); } ///

/// 创建权限集 /// /// /// public void CreatePermissionSet(string name,string description) { var per_id = Guid.NewGuid(); bas_permissionset.CreatePermissionSet(name, description, per_id); var permissioncontainer = new BAS_PermissionConatiner(irepositorypermissioncontainer); permissioncontainer.CreatePermissionContainer(per_id); } /// /// 将多个权限添加到权限集中 /// /// /// public void AddPermissionToPermissionSet(List permissionnames, string permissionsetname) { var permissionset = bas_permissionset.GetPermissionSetByName(permissionsetname); var permissions =new List(); foreach(var permissionname in permissionnames) { var bas_permission = new BAS_Permission(irepositorypermission); var permission = bas_permission.GetPermissionByName(permissionname); permissions.Add(permission); } bas_ppset.CreateBAS_PPS(permissions, permissionset); } /// /// 根据权限名获取权限集的信息 /// /// /// public List GetPermissionSetByPermissionName(string permissionname) { var ppsets = new BAS_PPSetService(irepositorypermission, irepositorypermissionset, irepositoryppset) .GEtppsetbyPermissionName(permissionname); var listpermissionset = new List(); foreach(var ppset in ppsets) { var permissionset = irepositorypermissionset.GetByCondition (p => p.Id == ppset.BAS_PermissionSet.Id, p => true).SingleOrDefault(); listpermissionset.Add(permissionset); } return listpermissionset; } /// /// 判断某个权限是否在某个权限集中 /// /// /// /// public bool IsPermissionSetContainPermission(string permissionname, string permissionsetname) { return GetPermissionSetByPermissionName(permissionname). Contains(bas_permissionset.GetPermissionSetByName(permissionsetname)); } }

在DDD.Application 应用服务:PermissionAppService

public class PermissionAppService { IRepositoryContext context = ServiecLocator.Instance.GetService(typeof(IRepositoryContext)) as IRepositoryContext; IRepository permissionrepository = ServiecLocator.Instance.GetService(typeof(IRepository)) as IRepository; IRepository permissioncontainerrepository = ServiecLocator.Instance.GetService(typeof(IRepository)) as IRepository; BAS_PermissionService bas_permissionservice; BAS_Permission bas_permission; public PermissionAppService() { bas_permissionservice = new BAS_PermissionService (permissionrepository, permissioncontainerrepository); bas_permission = new BAS_Permission(permissionrepository); } ///

/// 权限的创建 /// /// /// /// /// /// public void CreatePermission(string name,string description,string codeproperty, string codevalue,OperationType operationtype) { bas_permissionservice.CreatePermission(name, codeproperty, operationtype, codevalue, description); context.Commit(); } /// /// 根据权限名获取权限信息 /// /// /// public BAS_Permission GetPermissionByName(string name) { return bas_permission.GetPermissionByName(name); } public void CreatePermission(string v1, string v2, string v3, string v4, object operationType) { throw new NotImplementedException(); } }

PermissionSetAppService

public class PermissionSetAppService { IRepositoryContext context = ServiecLocator.Instance.GetService(typeof(IRepositoryContext)) as IRepositoryContext; IRepository permissionrepository = ServiecLocator.Instance.GetService(typeof(IRepository)) as IRepository; IRepository permissioncontainerrepository = ServiecLocator.Instance.GetService(typeof(IRepository)) as IRepository; IRepository permissionsetrepository = ServiecLocator.Instance.GetService(typeof(IRepository)) as IRepository; IRepository ppsetrepository = ServiecLocator.Instance.GetService(typeof(IRepository))as IRepository; BAS_PermissionSetService bas_permissionsetservice; BAS_PermissionSet bas_permissionset; public PermissionSetAppService() { bas_permissionsetservice = new BAS_PermissionSetService(permissionsetrepository, permissionrepository, ppsetrepository, permissioncontainerrepository); bas_permissionset = new BAS_PermissionSet(permissionsetrepository); } ///

/// 创建权限集 /// /// /// public void CreatePermissionSet(string name,string description) { bas_permissionsetservice.CreatePermissionSet(name, description); context.Commit(); } /// /// 根据权限集名获取权限集信息 /// /// /// public BAS_PermissionSet GetPermissionSetByName(string name) { return bas_permissionset.GetPermissionSetByName(name); } /// /// 添加多个权限到权限集 /// /// /// public void AddPermissionToPermissionSet(string[] permissionnames,string permissionsetname) { bas_permissionsetservice.AddPermissionToPermissionSet(permissionnames .ToList(), permissionsetname); context.Commit(); } /// /// 判断某个权限是否在某个权限集中 /// /// /// /// public bool IsPermissionSetContainpermission(string permissionname, string permissionsetname) { return bas_permissionsetservice.IsPermissionSetContainPermission(permissionname, permissionsetname); } }

测试代码:

[TestMethod] public void AddPermission() { PermissionAppService permissionservice = new PermissionAppService(); permissionservice.CreatePermission("产品信息权限", "代表一个产品权限信息", "[ProductName,UnitPrice]", "[{\"Field\":\"UnitPrice\",\"Operator\":\"Equal\",\"Value\":\"10\",\"Relation\":\"And\"}]", Domain.OperationType.Read); Assert.IsNotNull(permissionservice.GetPermissionByName("产品信息权限")); } [TestMethod] public void AddPermissionSet() { PermissionSetAppService permissionsetservice = new PermissionSetAppService(); permissionsetservice.CreatePermissionSet("普通用户权限集", "代表普通用户的一个权限集"); Assert.IsNotNull(permissionsetservice.GetPermissionSetByName("普通用户权限集")); } [TestMethod] public void AddPermissionToPermissionSet() { PermissionSetAppService permissionsetservice = new PermissionSetAppService(); string[] permissionnames = new string[1]; permissionnames[0] = "产品信息权限"; permissionsetservice.AddPermissionToPermissionSet(permissionnames, "普通用户权限集"); Assert.IsTrue(permissionsetservice.IsPermissionSetContainpermission("产品信息权限", "普通用户权限集")); }

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:JAVA实战项目实现客户选购系统详细流程
下一篇:Java组合模式
相关文章

 发表评论

暂时没有评论,来抢沙发吧~