DDD领域模型数据访问之对象(十一)

在工程DDD.Domain中文件夹ModelPermission新建类:BAS_Object

 public partial class BAS_Obejct:AggreateRoot
{
//仓储接口
private IRepository<BAS_Obejct> irepository;
//构造函数
public BAS_Obejct(IRepository<BAS_Obejct> irepository)
{
this.irepository = irepository;
} /// <summary>
/// 创建对象
/// </summary>
/// <param name="code">聚合根的类名</param>
/// <param name="name"></param>
/// <param name="description">描述</param>
/// <param name="obj_id">对象的标识</param>
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);
} /// <summary>
/// 根据对象名返回对象信息
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
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<BAS_ObjectSet> irepository;
public BAS_ObjectSet(IRepository<BAS_ObjectSet> irepository)
{
this.irepository = irepository;
} /// <summary>
/// 创建对象集
/// </summary>
/// <param name="name"></param>
/// <param name="description"></param>
/// <param name="obj_id"></param>
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);
} /// <summary>
/// 根据对象集名返回对象集合
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
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<BAS_OOSet> irepository;
public BAS_OOSet(IRepository<BAS_OOSet> irepository)
{
this.irepository = irepository;
}
public BAS_OOSet() { } /// <summary>
/// 将多个对象添加到对象集中
/// </summary>
/// <param name="objects"></param>
/// <param name="objectset"></param>
public void CreateBAS_OOSet(List<BAS_Obejct> 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);
} }
/// <summary>
/// 根据Object获取OOSET
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public List<BAS_OOSet> GetoosetByObject(BAS_Obejct obj)
{
return
irepository.GetByCondition(p => p.BAS_Obejct.Id == obj.Id, p => true);
}
/// <summary>
/// 根据ObjectSet获取OOSET
/// </summary>
/// <param name="objectset"></param>
/// <returns></returns>
public List<BAS_OOSet> GEtoosetByObjectSet(BAS_ObjectSet objectset)
{
return
irepository.GetByCondition(p => BAS_ObjectSet.Id == objectset.Id, p => true);
}
}

添加对象容器:

 public partial class BAS_ObejctContainer:AggreateRoot
{
private IRepository<BAS_ObejctContainer> irepository;
public BAS_ObejctContainer(IRepository<BAS_ObejctContainer> irepository)
{
this.irepository = irepository;
} /// <summary>
/// 创建对象容器
/// </summary>
/// <param name="obj_id"></param>
public void CreateObjectContainer(Guid obj_id)
{
var bas_objectcontainer = new BAS_ObejctContainer();
bas_objectcontainer.Id = base.Id;
irepository.Create(bas_objectcontainer);
}
}

创建DomainService:BAS_ObjectService类:

public class BAS_ObjectService
{
private IRepository<BAS_Obejct> irepositoryobject;
private IRepository<BAS_ObejctContainer> irepositoryobjectcontainer; public BAS_ObjectService(IRepository<BAS_Obejct> irepositoryobject,
IRepository<BAS_ObejctContainer> irepositoryobjectcontainer)
{
this.irepositoryobject = irepositoryobject;
this.irepositoryobjectcontainer = irepositoryobjectcontainer;
} /// <summary>
/// 创建对象
/// </summary>
/// <param name="name"></param>
/// <param name="description"></param>
/// <param name="code"></param>
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);
} /// <summary>
/// 根据对象Code返回对象
/// </summary>
/// <param name="objectcode"></param>
/// <returns></returns>
public BAS_Obejct GetObjectByCode(string objectcode)
{
return
irepositoryobject.GetByCondition(p => p.Code == objectcode, p => true)
.SingleOrDefault();
}
}

对象和对象之间的领域服务:BAS_OOSetService

 public class BAS_OOSetService
{
private IRepository<BAS_Obejct> irepositoryobject;
private IRepository<BAS_ObjectSet> irepositoryobjectset;
private IRepository<BAS_OOSet> irepositoryooset;
BAS_OOSet ooset;
public BAS_OOSetService(IRepository<BAS_Obejct> irepositoryobject,
IRepository<BAS_ObjectSet> irepositoryobjectset,
IRepository<BAS_OOSet> irepositoryooset)
{
this.irepositoryobject = irepositoryobject;
this.irepositoryobjectset = irepositoryobjectset;
this.irepositoryooset = irepositoryooset;
ooset = new BAS_OOSet(irepositoryooset);
} /// <summary>
/// 添加多个对象到对象集中
/// </summary>
/// <param name="objectnames"></param>
/// <param name="objectsetname"></param>
public void CreateBas_OOSet(string[] objectnames,string objectsetname)
{
var listobject = new List<BAS_Obejct>();
for(int i=0;i<objectnames.Length;i++)
{
var obj=
irepositoryobject.GetByCondition(p => 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); } /// <summary>
/// 根据对象名获取OOSET
/// </summary>
/// <param name="objectname"></param>
/// <returns></returns>
public List<BAS_OOSet> GetoosetByObjectName(string objectname)
{
var obj =
irepositoryobject.GetByCondition(p => p.Name == objectname, p => true)
.SingleOrDefault();
return ooset.GetoosetByObject(obj); }
/// <summary>
/// 根据对象集名获取OOSET
/// </summary>
/// <param name="objectsetname"></param>
/// <returns></returns>
public List<BAS_OOSet> GetoosetByObjectSetName(string objectsetname)
{
var objectset =
irepositoryobjectset.GetByCondition(p => p.Name == objectsetname, p => true)
.SingleOrDefault();
return ooset.GEtoosetByObjectSet(objectset);
}
/// <summary>
/// 根据对象Code获取OOSET
/// </summary>
/// <param name="objectcode"></param>
/// <returns></returns>
public List<BAS_OOSet> 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<BAS_ObjectSet> irepositoryobjectset;
private IRepository<BAS_Obejct> irepositoryobject;
private IRepository<BAS_ObejctContainer> irepositoryobjectcontainer;
private IRepository<BAS_OOSet> irepositoryooset; BAS_ObjectSet bas_objectset;
BAS_OOSet bas_ooset;
BAS_OOSetService bas_oosetservice;
public BAS_ObjectSetService(IRepository<BAS_ObjectSet> irepositoryobjectset,
IRepository<BAS_Obejct> irepositoryobject,
IRepository<BAS_ObejctContainer> irepositoryobjectcontainer,
IRepository<BAS_OOSet> 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);
}
/// <summary>
/// 创建对象集
/// </summary>
/// <param name="name"></param>
/// <param name="description"></param>
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);
} /// <summary>
/// 添加多个对象到对象集
/// </summary>
/// <param name="objectnames"></param>
/// <param name="objectsetname"></param>
public void AddObjectsToObjectSet(List<string> objectnames,string objectsetname)
{
var objectset = bas_objectset.GetObjectSetByName(objectsetname); var objects = new List<BAS_Obejct>(); 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);
} /// <summary>
/// 根据对象名获取所属对象集
/// </summary>
/// <param name="objectname"></param>
/// <returns></returns>
public List<BAS_ObjectSet> GetObjectSetByObjectName(string objectname)
{
var oosets = bas_oosetservice.GetoosetByObjectName(objectname);
var objectsets = new List<BAS_ObjectSet>();
foreach(var ooset in oosets)
{
var objectset =
irepositoryobjectset.GetByCondition(p => p.Id == ooset.BAS_ObjectSet.Id,
p => true).SingleOrDefault();
objectsets.Add(objectset);
} return objectsets;
}
/// <summary>
/// 根据对象CODE获取所属对象集
/// </summary>
/// <param name="objectcode"></param>
/// <returns></returns>
public List<BAS_ObjectSet> GetObjectSetByObjectCode(string objectcode)
{
var oosets = bas_oosetservice.GetoosetByObjectCode(objectcode);
var objectsets = new List<BAS_ObjectSet>();
foreach (var ooset in oosets)
{
var objectset =
irepositoryobjectset.GetByCondition(p => p.Id == ooset.BAS_ObjectSet.Id,
p => true).SingleOrDefault();
objectsets.Add(objectset);
} return objectsets;
} /// <summary>
/// 判断某个对象是否在某个对象集中
/// </summary>
/// <param name="objectname"></param>
/// <param name="objectsetname"></param>
/// <returns></returns>
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<BAS_Obejct> objectrepository = ServiecLocator.Instance.GetService(typeof(IRepository<BAS_Obejct>))
as IRepository<BAS_Obejct>;
IRepository<BAS_ObejctContainer> objectcontainerrepository = ServiecLocator.Instance.GetService(typeof(IRepository<BAS_ObejctContainer>))
as IRepository<BAS_ObejctContainer>; BAS_ObjectService bas_obejctservice;
BAS_Obejct bas_object; public ObjectAppService()
{
bas_obejctservice = new BAS_ObjectService(objectrepository,
objectcontainerrepository);
bas_object = new BAS_Obejct(objectrepository);
} /// <summary>
/// 对象创建
/// </summary>
/// <param name="name"></param>
/// <param name="description"></param>
/// <param name="code"></param>
public void CreateObject(string name,string description,string code)
{
bas_obejctservice.CreateObject(name, description, code);
context.Commit();
} /// <summary>
/// 根据对象名获取对象
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
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<BAS_Obejct> objectrepository = ServiecLocator.Instance.GetService(typeof(IRepository<BAS_Obejct>))
as IRepository<BAS_Obejct>;
IRepository<BAS_ObejctContainer> objectcontainerrepository = ServiecLocator.Instance.GetService(typeof(IRepository<BAS_ObejctContainer>))
as IRepository<BAS_ObejctContainer>;
IRepository<BAS_ObjectSet> objectsetrepository = ServiecLocator.Instance.GetService(typeof(IRepository<BAS_ObjectSet>))
as IRepository<BAS_ObjectSet>;
IRepository<BAS_OOSet> oosetrepository = ServiecLocator.Instance.GetService(typeof(IRepository<BAS_OOSet>))
as IRepository<BAS_OOSet>; BAS_ObjectSetService objectsetservice;
BAS_ObjectSet bas_objectset; public ObjectSetAppService()
{
objectsetservice =
new BAS_ObjectSetService(objectsetrepository, objectrepository, objectcontainerrepository, oosetrepository);
bas_objectset = new BAS_ObjectSet(objectsetrepository);
} /// <summary>
/// 对象集创建
/// </summary>
/// <param name="name"></param>
/// <param name="description"></param>
public void CreateobjectSet(string name,string description)
{
objectsetservice.CreateObjectSet(name, description);
context.Commit();
}
/// <summary>
/// 根据对象集名返回对象集
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public BAS_ObjectSet GetObjectSetByName(string name)
{
return bas_objectset.GetObjectSetByName(name);
}
/// <summary>
/// 将多个对象添加到对象集中
/// </summary>
/// <param name="objectnames"></param>
/// <param name="objectsetname"></param>
public void AddObjectToObjectSet(string[] objectnames,string objectsetname)
{
objectsetservice.AddObjectsToObjectSet(objectnames.ToList(), objectsetname);
context.Commit();
} /// <summary>
/// 判断一个对象是否在对象集中
/// </summary>
/// <param name="objectname"></param>
/// <param name="objectsetname"></param>
/// <returns></returns>
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("产品对象", "订单对象集"));
}
上一篇:解决浏览器跨域限制方案之JSONP


下一篇:解决浏览器跨域限制方案之CORS