结合领域驱动设计的SOA分布式软件架构,你还在为这些烦恼?( 六 )

();foreach (var orderItem in orderItemList)orderItemDTO.Add(GetOrderItemDTO(orderItem));orderDTO.OrderItemList = orderItemDTO;}}return orderDTO;}public static OrderItemDTO GetOrderItemDTO(OrderItem orderItem){OrderItemDTO orderItemDTO = new OrderItemDTO();orderItemDTO.Count = orderItem.Count.GetInt();orderItemDTO.Goods = orderItem.Goods.GetString();orderItemDTO.OrderID = orderItem.OrderID;orderItemDTO.OrderItemID = orderItem.ID;orderItemDTO.Price = orderItem.Price.GetDouble();return orderItemDTO;}//把DTO转换成多个对象public static void SetOrder(OrderDTO orderDTO, out Person person, out Order order){person = new Person();person.EntityKey=new System.Data.EntityKey("BusinessContext.Person","ID",orderDTO.PersonID);person.Address = orderDTO.Address;person.EMail = orderDTO.EMail;person.ID = orderDTO.PersonID;person.Name = orderDTO.Name;person.Point = orderDTO.Point;person.Telephone = orderDTO.Telephone;order = new Order();order.EntityKey=new System.Data.EntityKey("BusinessContext.Order","ID",orderDTO.OrderID);order.Count = orderDTO.Count;if (orderDTO.Delivery.Year!=0001order.Favorable = orderDTO.Favorable;order.Freightage = orderDTO.Freightage;order.ID = orderDTO.OrderID;order.OrderNumber = orderDTO.OrderNumber;order.PersonID = orderDTO.PersonID;order.Price = orderDTO.Price;order.TotalPrice = orderDTO.TotalPrice;var orderItemDTOList = orderDTO.OrderItemList;if (orderItemDTOList.Count() != 0)foreach (var orderItemDTO in orderItemDTOList)order.OrderItem.Add(GetOrderItem(orderItemDTO));}public static OrderItem GetOrderItem(OrderItemDTO orderItemDTO){OrderItem orderItem = new OrderItem();orderItem.EntityKey = new System.Data.EntityKey("BusinessContext.OrderItem", "ID", orderItemDTO.OrderItemID);orderItem.Count = orderItemDTO.Count;orderItem.Goods = orderItemDTO.Goods;orderItem.ID = orderItemDTO.OrderItemID;orderItem.OrderID = orderItemDTO.OrderID;orderItem.Price = orderItemDTO.Price;return orderItem;}}}//数据传输对象 DTOnamespace Business.TransferObject{[DataContract]public class OrderItemDTO{private int _orderItemID;private int _orderID;private string _goods;private double _price;private int _count;[DataMember]public int OrderItemID{get { return _orderItemID; }set { _orderItemID = value; }}........................}[DataContract]public class OrderDTO{private int _personID;private string _name;private string _address;private string _telephone;private int _point;private string _email;private int _orderID;private string _orderNumber;private int _count;private double _freightage;private double _favorable;private DateTime _delivery;private double _price;private double _totalPrice;private IList _orderItemDTOList;[DataMember]public int PersonID{get{return this._personID;}set{this._personID=value;}}....................}}通过数据转换器 , 可以顺利实现领域模型与DTO之间的转换 , 协调应用层服务的运行 。
结合领域驱动设计的SOA分布式软件架构,你还在为这些烦恼?文章插图
4. 应用层的发布
在开发SOA系统的时候 , 应用层的服务需要使用远程方法对外开放 , 在接收到请求的时候 , 它可以调用领域层服务获取运算结果 , 然后通过数据转换器OperationAssembler把运算结果转换成DTO , 最后返还到表现层 。 在起初 , 我曾尝试对应每个应用层的对象建立一个远程接口 , 但经过多次重构以后 , 我觉得行程对象就是一个简单的对外接口 , 对象之间不存在什么逻辑关系 。 所以更简单的方法是使用外观模式 , 建立少数的几个远程服务类 , 把所有的应用层对象的方法都包含在内 。
结合领域驱动设计的SOA分布式软件架构,你还在为这些烦恼?文章插图
可以留意代码 , OperationService包括了对Person模型和Order模型的所有操作 。 而且每个操作都只是简单地调用应用层服务 (ApplicationService) 获得计算结果 , 然后使用数据转换器 (OperationAssembler)转换数据 , 当中并不存在任何的业务逻辑 。
namespace Business.Service.ApplicationService{[ServiceContract]public interface IOperationService{[OperationContract]int AddOrder(ref OrderDTO orderDTO);[OperationContract]int DeleteOrder(OrderDTO orderDTO);[OperationContract]int UpdateOrder(ref OrderDTO orderDTO);[OperationContract]IList GetOrderByPerson(int personID);[OperationContract]OrderDTO GetOrder(int orderID);[OperationContract]int AddPerson(ref OrderDTO orderDTO);[OperationContract]int UpdatePerson(ref OrderDTO orderDTO);[OperationContract]OrderDTO GetPerson(int personID);[OperationContract]IList GetPersonList();[OperationContract]OrderDTO Payment(int orderID);}public class OperationService:IOperationService{[OperationBehavior(TransactionScopeRequired = true, TransactionAutoComplete = true)]public int AddOrder(ref OrderDTO orderDTO){OrderService orderService = new OrderService();Order order = GetOrder(orderDTO);int n = orderService.AddOrder(order);orderDTO = OperationAssembler.GetOrderDTO(order, null);return n;}[OperationBehavior(TransactionScopeRequired = true, TransactionAutoComplete = true)]public int DeleteOrder(OrderDTO orderDTO){OrderService orderService = new OrderService();return orderService.DeleteOrder(GetOrder(orderDTO));}[OperationBehavior(TransactionScopeRequired = true, TransactionAutoComplete = true)]public int UpdateOrder(ref OrderDTO orderDTO){OrderService orderService = new OrderService();Order order = GetOrder(orderDTO);int n = orderService.UpdateOrder(order);orderDTO = OperationAssembler.GetOrderDTO(order, null);return n;}............................}}