💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
# [.NET领域驱动设计实战系列]专题六:DDD实践案例:网上书店订单功能的实现 ## 一、引言 上一专题已经为网上书店实现了购物车的功能了,在这一专题中,将继续对网上书店案例进行完善,本专题将对网上书店订单功能的实现进行介绍,现在废话不多说了,让我们来一起看看订单功能是如何实现的吧。 ## 二、订单功能的实现思路 在网上购过物的朋友,对于订单功能的流程自然不陌生,这里我还是先来梳理下下订单的一个流程: * 用户点击我的购物车,可以勾选对应的商品进行结算 * 在结算页面可以提交订单来创建一个订单 * 创建订单成功之后就是进行付款了。 一般购物网站下订单的流程分上面3步,由于在本案例中并没有对接第三方的支付平台,所以这里就没有上面第三步的过程了。即在网上书店案例中订单提交成功之后就是已付款状态。 从上面下订单流程我们就可以知道订单功能的实现思路: * 用户点击购物车中购买商品按钮来进行下订单,此时触发控制器中的结算方法进行调用 * 结算方法通过调用OrderService服务中的结算方法 * 由于订单的创建涉及了3个实体操作,包括购物车实体,购物车项实体和订单实体。所以这里需要引入领域服务。因为创建订单这个操作涉及了多个实体,则这个业务逻辑放在每个实体中都不合适,因为并属于单独一个实体的逻辑。所以这里引入领域服务来实现这种涉及多个实体的操作。 * 则OrderService服务中的结算方法通过调用领域服务中的CreateOrder方法来完成订单创建的功能。 * 领域服务中可以调用对应的实体仓储来完成实体的持久化。这里需要注意的一点:因为领域服务涉及多个实体的持久化,则需要引入工作单元模式将这些实体的操作进行统一提交,要不都成功,要不都不成功。这也就是引入工作单元初衷。 上面的思路就是订单功能的实现思路。有了上面的思路之后,实现订单功能也一目了然了。下面让我们一起在网上书店案例中实现下看看。 ## 三、网上书店订单功能的实现 这里我们按照上面的实现思路由下至上地去实现订单功能。 1. 首先我们需要订单仓储来完成订单实体的持久化。具体订单仓储接口和实现如下代码所示: ``` // 订单仓储接口 public interface IOrderRepository : IRepository<Order> { } // 订单仓储的实现类 public class OrderRepository : EntityFrameworkRepository<Order>, IOrderRepository { public OrderRepository(IRepositoryContext context) : base(context) { } } ``` 2\. 领域服务的实现。具体的领域服务接口和实现代码如下所示: ``` // 领域服务接口 public interface IDomainService { Order CreateOrder(User user, ShoppingCart shoppingCart); } // 领域服务类型 // 牵涉到多个实体的操作可以把这些操作封装到领域服务里 public class DomainService : IDomainService { private readonly IRepositoryContext _repositoryContext; private readonly IShoppingCartItemRepository _shoppingCartItemRepository; private readonly IOrderRepository _orderRepository; /// <summary> /// 创建订单,涉及到的操作有2个:1\. 把购物车中的项中购物车移除; 2.创建一个订单。 /// 这两个操作必须同时完成或失败。 /// </summary> /// <param name="user"></param> /// <param name="shoppingCart"></param> /// <returns></returns> public Order CreateOrder(User user, ShoppingCart shoppingCart) { var order = new Order(); var shoppingCartItems = _shoppingCartItemRepository.GetAll( new ExpressionSpecification<ShoppingCartItem>(s => s.ShoopingCart.Id == shoppingCart.Id)); if (shoppingCartItems == null || !shoppingCartItems.Any()) throw new InvalidOperationException("购物篮中没有任何物品"); order.OrderItems = new List<OrderItem>(); foreach (var shoppingCartItem in shoppingCartItems) { var orderItem = shoppingCartItem.ConvertToOrderItem(); orderItem.Order = order; order.OrderItems.Add(orderItem); _shoppingCartItemRepository.Remove(shoppingCartItem); } order.User = user; order.Status = OrderStatus.Paid; _orderRepository.Add(order); _repositoryContext.Commit(); return order; } } ``` 3\. 订单服务的实现。具体订单服务实现代码如下所示: ``` public class OrderServiceImp : ApplicationService, IOrderService { #region Private Fileds private readonly IShoppingCartRepository _shoppingCartRepository; private readonly IShoppingCartItemRepository _shoppingCartItemRepository; private readonly IUserRepository _userRepository; private readonly IOrderRepository _orderRepository; private readonly IProductRepository _productRepository; private readonly IDomainService _domainService; private readonly IEventBus _eventBus; #endregion #region Ctor public OrderServiceImp(IRepositoryContext context, IUserRepository userRepository, IShoppingCartRepository shoppingCartRepository, IProductRepository productRepository, IShoppingCartItemRepository shoppingCartItemRepository, IDomainService domainService, IOrderRepository orderRepository, IEventBus eventBus) : base(context) { _userRepository = userRepository; _shoppingCartRepository = shoppingCartRepository; _productRepository = productRepository; _shoppingCartItemRepository = shoppingCartItemRepository; _domainService = domainService; _orderRepository = orderRepository; _eventBus = eventBus; } #endregion public OrderDto Checkout(Guid customerId) { var user = _userRepository.GetByKey(customerId); var shoppingCart = _shoppingCartRepository.GetByExpression(s => s.User.Id == user.Id); var order = _domainService.CreateOrder(user, shoppingCart); return Mapper.Map<Order, OrderDto>(order); } public OrderDto GetOrder(Guid orderId) { var order = _orderRepository.GetBySpecification(new ExpressionSpecification<Order>(o=>o.Id.Equals(orderId)), elp=>elp.OrderItems); return Mapper.Map<Order, OrderDto>(order); } // 获得指定用户的所有订单 public IList<OrderDto> GetOrdersForUser(Guid userId) { var user = _userRepository.GetByKey(userId); var orders = _orderRepository.GetAll(new ExpressionSpecification<Order>(o => o.User.Id == userId), sp => sp.CreatedDate, SortOrder.Descending, elp=>elp.OrderItems); var orderDtos = new List<OrderDto>(); orders .ToList() .ForEach(o=>orderDtos.Add(Mapper.Map<Order, OrderDto>(o))); return orderDtos; } ``` 4\. HomeController控制器中Checkout操作的实现。具体实现代码如下所示: ``` public class HomeController : ControllerBase { /// <summary> /// 结算操作 /// </summary> /// <returns></returns> [Authorize] public ActionResult Checkout() { using (var proxy = new OrderServiceClient()) { var model = proxy.Checkout(this.UserId); return View(model); } } [Authorize] public ActionResult Orders() { using (var proxy = new OrderServiceClient()) { var model = proxy.GetOrdersForUser(this.UserId); return View(model); } } [Authorize] public ActionResult Order(string id) { using (var proxy = new OrderServiceClient()) { var model = proxy.GetOrder(new Guid(id)); return View(model); } } } ``` 这样我们就在网上书店中实现了订单功能了。具体的视图界面也就是上一专题中实现的购物车页面。下面具体看看订单的具体实现效果: 结算页面: ![](http://images0.cnblogs.com/blog2015/383187/201506/082252139104115.png) 点击确认购买按钮,在弹出框中点击确认来完成订单的创建: ![](http://images0.cnblogs.com/blog2015/383187/201506/082254206443667.png) 通过我的订单来查看所有订单页面: ![](http://images0.cnblogs.com/blog2015/383187/201506/082259235041793.png) ## 四、总结 到此,网上书店案例的订单功能的实现就完成了,在接下来的专题将继续对该案例进行完善,在下一专题中将为该案例引入后台管理操作。商家或管理员可以进入后台管理来对用户订单进行确认发货,以及添加商品,分类等操作。具体实现请见下一专题。 本专题中所有实现源码下载:https://github.com/lizhi5753186/OnlineStore_Second/