DDD领域模型数据访问之对象(十一)
在工程DDD.Domain中文件夹ModelPermission新建类:BAS_Object
public partial class BAS_Obejct:AggreateRoot { //仓储接口 private IRepository irepository; //构造函数 public BAS_Obejct(IRepository irepository) { this.irepository = irepository; } /// /// 创建对象 /// /// 聚合根的类名 /// /// 描述 /// 对象的标识 public void CreateObject(string code,string name,string description,Guid obj_id) { var bas_object = new BAS_Obejct(); bas_object.Id = base.Id; bas_object.Name = name; bas_object.Description = description; bas_object.Code = code; bas_object.Obj_Id = obj_id; //添加到仓储中 irepository.Create(bas_object); } /// /// 根据对象名返回对象信息 /// /// /// public BAS_Obejct GetObjectByName(string name) { return irepository.GetByCondition(p => p.Name == name, p => true).SingleOrDefault(); } }
BAS_ObjectSet:(领域对象)
public partial class BAS_ObjectSet:AggreateRoot { private IRepository irepository; public BAS_ObjectSet(IRepository irepository) { this.irepository = irepository; } /// /// 创建对象集 /// /// /// /// public void CreateObjectSet(string name,string description,Guid obj_id) { var bas_objectset = new BAS_ObjectSet(); bas_objectset.Id = base.Id; bas_objectset.Name = name; bas_objectset.Description = description; bas_objectset.Obj_Id = obj_id; irepository.Create(bas_objectset); } /// /// 根据对象集名返回对象集合 /// /// /// public BAS_ObjectSet GetObjectSetByName(string name) { return irepository.GetByCondition(p => Name == name, p => true).SingleOrDefault(); } }
BAS_OOSet
public partial class BAS_OOSet:AggreateRoot { private IRepository irepository; public BAS_OOSet(IRepository irepository) { this.irepository = irepository; } public BAS_OOSet() { } /// /// 将多个对象添加到对象集中 /// /// /// public void CreateBAS_OOSet(List objects,BAS_ObjectSet objectset) { foreach(var obj in objects) { var bas_ooset = new BAS_OOSet(); bas_ooset.Id = base.Id; bas_ooset.BAS_ObjectSet = objectset; bas_ooset.BAS_Obejct = obj; irepository.Create(bas_ooset); } } /// /// 根据Object获取OOSET /// /// /// public List GetoosetByObject(BAS_Obejct obj) { return irepository.GetByCondition(p => p.BAS_Obejct.Id == obj.Id, p => true); } /// /// 根据ObjectSet获取OOSET /// /// /// public List GEtoosetByObjectSet(BAS_ObjectSet objectset) { return irepository.GetByCondition(p => BAS_ObjectSet.Id == objectset.Id, p => true); } }
添加对象容器:
public partial class BAS_ObejctContainer:AggreateRoot { private IRepository irepository; public BAS_ObejctContainer(IRepository irepository) { this.irepository = irepository; } /// /// 创建对象容器 /// /// public void CreateObjectContainer(Guid obj_id) { var bas_objectcontainer = new BAS_ObejctContainer(); bas_objectcontainer.Id = base.Id; irepository.Create(bas_objectcontainer); } }
BAS_ObjectService类:
public class BAS_ObjectService { private IRepository irepositoryobject; private IRepository irepositoryobjectcontainer; public BAS_ObjectService(IRepository irepositoryobject, IRepository irepositoryobjectcontainer) { this.irepositoryobject = irepositoryobject; this.irepositoryobjectcontainer = irepositoryobjectcontainer; } /// /// 创建对象 /// /// /// /// public void CreateObject(string name,string description,string code) { var bas_object = new BAS_Obejct(irepositoryobject); var obj_id = Guid.NewGuid(); bas_object.CreateObject(code, name, description, obj_id); var bas_objectcontainer = new BAS_ObejctContainer(irepositoryobjectcontainer); bas_objectcontainer.CreateObjectContainer(obj_id); } /// /// 根据对象Code返回对象 /// /// /// public BAS_Obejct GetObjectByCode(string objectcode) { return irepositoryobject.GetByCondition(p => p.Code == objectcode, p => true) .SingleOrDefault(); } }
BAS_OOSetService
public class BAS_OOSetService { private IRepository irepositoryobject; private IRepository irepositoryobjectset; private IRepository irepositoryooset; BAS_OOSet ooset; public BAS_OOSetService(IRepository irepositoryobject, IRepository irepositoryobjectset, IRepository irepositoryooset) { this.irepositoryobject = irepositoryobject; this.irepositoryobjectset = irepositoryobjectset; this.irepositoryooset = irepositoryooset; ooset = new BAS_OOSet(irepositoryooset); } /// /// 添加多个对象到对象集中 /// /// /// public void CreateBas_OOSet(string[] objectnames,string objectsetname) { var listobject = new List(); for(int i=0;i p.Name == objectnames[i], p => true).SingleOrDefault(); listobject.Add(obj); } var objectset = irepositoryobjectset.GetByCondition(p => p.Name == objectsetname, p => true).SingleOrDefault(); ooset.CreateBAS_OOSet(listobject, objectset); } /// /// 根据对象名获取OOSET /// /// /// public List GetoosetByObjectName(string objectname) { var obj = irepositoryobject.GetByCondition(p => p.Name == objectname, p => true) .SingleOrDefault(); return ooset.GetoosetByObject(obj); } /// /// 根据对象集名获取OOSET /// /// /// public List GetoosetByObjectSetName(string objectsetname) { var objectset = irepositoryobjectset.GetByCondition(p => p.Name == objectsetname, p => true) .SingleOrDefault(); return ooset.GEtoosetByObjectSet(objectset); } /// /// 根据对象Code获取OOSET /// /// /// public List GetoosetByObjectCode(string objectcode) { var obj = irepositoryobject.GetByCondition(p => p.Code == objectcode, p => true) .SingleOrDefault(); return ooset.GetoosetByObject(obj); } }
添加BAS_ObjectSetService类:
public class BAS_ObjectSetService { private IRepository irepositoryobjectset; private IRepository irepositoryobject; private IRepository irepositoryobjectcontainer; private IRepository irepositoryooset; BAS_ObjectSet bas_objectset; BAS_OOSet bas_ooset; BAS_OOSetService bas_oosetservice; public BAS_ObjectSetService(IRepository irepositoryobjectset, IRepository irepositoryobject, IRepository irepositoryobjectcontainer, IRepository irepositoryooset) { this.irepositoryobject = irepositoryobject; this.irepositoryobjectset = irepositoryobjectset; this.irepositoryobjectcontainer = irepositoryobjectcontainer; this.irepositoryooset = irepositoryooset; bas_objectset = new BAS_ObjectSet(irepositoryobjectset); bas_ooset = new BAS_OOSet(irepositoryooset); bas_oosetservice = new BAS_OOSetService(irepositoryobject, irepositoryobjectset, irepositoryooset); } /// /// 创建对象集 /// /// /// public void CreateObjectSet(string name,string description) { var obj_id = Guid.NewGuid(); bas_objectset.CreateObjectSet(name, description, obj_id); var objectcontainer = new BAS_ObejctContainer(irepositoryobjectcontainer); objectcontainer.CreateObjectContainer(obj_id); } /// /// 添加多个对象到对象集 /// /// /// public void AddObjectsToObjectSet(List objectnames,string objectsetname) { var objectset = bas_objectset.GetObjectSetByName(objectsetname); var objects = new List(); foreach(var objectname in objectnames) { var bas_object = new BAS_Obejct(irepositoryobject); var obj = bas_object.GetObjectByName(objectname); objects.Add(obj); } bas_ooset.CreateBAS_OOSet(objects, objectset); } /// /// 根据对象名获取所属对象集 /// /// /// public List GetObjectSetByObjectName(string objectname) { var oosets = bas_oosetservice.GetoosetByObjectName(objectname); var objectsets = new List(); foreach(var ooset in oosets) { var objectset = irepositoryobjectset.GetByCondition(p => p.Id == ooset.BAS_ObjectSet.Id, p => true).SingleOrDefault(); objectsets.Add(objectset); } return objectsets; } /// /// 根据对象CODE获取所属对象集 /// /// /// public List GetObjectSetByObjectCode(string objectcode) { var oosets = bas_oosetservice.GetoosetByObjectCode(objectcode); var objectsets = new List(); foreach (var ooset in oosets) { var objectset = irepositoryobjectset.GetByCondition(p => p.Id == ooset.BAS_ObjectSet.Id, p => true).SingleOrDefault(); objectsets.Add(objectset); } return objectsets; } /// /// 判断某个对象是否在某个对象集中 /// /// /// /// public bool IsObjectSetContainObject(string objectname,string objectsetname) { return GetObjectSetByObjectName(objectname).Contains (bas_objectset.GetObjectSetByName(objectsetname)); } }
对象的应用服务:ObjectAppService
public class ObjectAppService { IRepositoryContext context = ServiecLocator.Instance.GetService(typeof(IRepositoryContext)) as IRepositoryContext; IRepository objectrepository = ServiecLocator.Instance.GetService(typeof(IRepository)) as IRepository; IRepository objectcontainerrepository = ServiecLocator.Instance.GetService(typeof(IRepository)) as IRepository; BAS_ObjectService bas_obejctservice; BAS_Obejct bas_object; public ObjectAppService() { bas_obejctservice = new BAS_ObjectService(objectrepository, objectcontainerrepository); bas_object = new BAS_Obejct(objectrepository); } /// /// 对象创建 /// /// /// /// public void CreateObject(string name,string description,string code) { bas_obejctservice.CreateObject(name, description, code); context.Commit(); } /// /// 根据对象名获取对象 /// /// /// public BAS_Obejct GetObjectByName(string name) { return bas_object.GetObjectByName(name); } }
ObjectSetAppService服务对象:
public class ObjectSetAppService { IRepositoryContext context = ServiecLocator.Instance.GetService(typeof(IRepositoryContext)) as IRepositoryContext; IRepository objectrepository = ServiecLocator.Instance.GetService(typeof(IRepository)) as IRepository; IRepository objectcontainerrepository = ServiecLocator.Instance.GetService(typeof(IRepository)) as IRepository; IRepository objectsetrepository = ServiecLocator.Instance.GetService(typeof(IRepository)) as IRepository; IRepository oosetrepository = ServiecLocator.Instance.GetService(typeof(IRepository))as IRepository; BAS_ObjectSetService objectsetservice; BAS_ObjectSet bas_objectset; public ObjectSetAppService() { objectsetservice = new BAS_ObjectSetService(objectsetrepository, objectrepository, objectcontainerrepository, oosetrepository); bas_objectset = new BAS_ObjectSet(objectsetrepository); } /// /// 对象集创建 /// /// /// public void CreateobjectSet(string name,string description) { objectsetservice.CreateObjectSet(name, description); context.Commit(); } /// /// 根据对象集名返回对象集 /// /// /// public BAS_ObjectSet GetObjectSetByName(string name) { return bas_objectset.GetObjectSetByName(name); } /// /// 将多个对象添加到对象集中 /// /// /// public void AddObjectToObjectSet(string[] objectnames,string objectsetname) { objectsetservice.AddObjectsToObjectSet(objectnames.ToList(), objectsetname); context.Commit(); } /// /// 判断一个对象是否在对象集中 /// /// /// /// public bool IsObjectSetContainObject(string objectname,string objectsetname) { return objectsetservice.IsObjectSetContainObject(objectname, objectsetname); } }
单元测试的代码:
[TestMethod] public void AddObject() { ObjectAppService objectservice = new ObjectAppService(); objectservice.CreateObject("产品对象", "标识产品对象", "Product"); Assert.IsNotNull(objectservice.GetObjectByName("产品对象")); } [TestMethod] public void AddObjectSet() { ObjectSetAppService objectsetservice = new ObjectSetAppService(); objectsetservice.CreateobjectSet("订单对象集", "包含订单业务所有的对象"); Assert.IsNotNull(objectsetservice.GetObjectSetByName("订单对象集")); } [TestMethod] public void AddObjectToObjectSet() { ObjectSetAppService objectsetservice = new ObjectSetAppService(); string[] objects = new string[1]; objects[0] = "产品对象"; objectsetservice.AddObjectToObjectSet(objects, "订单对象集"); Assert.IsTrue(objectsetservice.IsObjectSetContainObject("产品对象", "订单对象集")); }
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
暂时没有评论,来抢沙发吧~