The realization of order function in the online bookstore

Source: Internet
Author: User
Tags net domain eventbus

First, Introduction

The previous topic has realized the function of the shopping cart for the online bookstore, in this topic, will continue to improve the online bookstore case, the topic will be on the online bookstore order function implementation of the introduction, now nonsense to say, let's see how the Order function is realized.

Second, the realization of the order function ideas

In the online shopping friends, for the order function of the process naturally not unfamiliar, here I still first to comb under the order of a process:

    • Users click on my shopping cart, you can tick the corresponding products for settlement
    • On the billing page you can submit an order to create an order
    • The payment is made after the order is created successfully.

General shopping site orders under the process of 3 steps, because in this case there is no docking third-party payment platform, so there is no third step above the process. That is, in the online bookstore case, after the successful order submission is the payment status.

From the above order flow we can know the order function implementation ideas:

    • The user clicks on the Purchase item button in the cart to place an order, and the closing method in the trigger controller is called.
    • Settlement method by calling the settlement method in the OrderService service
    • Since the creation of the order involves 3 entity operations, including the shopping cart entity, the shopping Cart item entity, and the order entity. So there's a need to introduce Domain Services. Because creating an order involves multiple entities, this business logic is inappropriate in each entity, because it belongs to the logic of a single entity. So this is where Domain Services are introduced to implement this operation involving multiple entities.
    • The settlement method in the OrderService service accomplishes the function of order creation by invoking the Createorder method in the domain service.
    • Domain Services can invoke the corresponding entity warehousing to complete the persistence of the entity. One thing to note here: Because Domain Services involve the persistence of multiple entities, it is necessary to introduce a unit of work pattern to uniformly commit the operations of those entities, either successfully or unsuccessfully. This is also the original purpose of the introduction of work units.

The idea above is the realization of the order function. With the above ideas, the realization of the order function is also at a glance. Let's take a look below in the online bookstore case.

Third, the realization of the online bookstore order function

Here we follow the above implementation of the idea from the bottom to achieve the order function.

    1. First we need order warehousing to complete the order entity's persistence. The specific order warehousing interface and implementation are shown in the following code:
Order Warehousing Interface Public    interface iorderrepository:irepository<order>    {    }//Order Warehousing Implementation Class    public class Orderrepository:entityframeworkrepository<order>, iorderrepository    {public        orderrepository ( Irepositorycontext context): Base (context)        {        }    }

2. Implementation of Domain Services. The specific domain service interface and implementation code are as follows:

Domain Service Interface public interface Idomainservice {Order createorder (user user, ShoppingCart shoppingcart); }//Domain service type//operations involving multiple entities can encapsulate these operations in the domain service public class Domainservice:idomainservice {private Rea        DOnly Irepositorycontext _repositorycontext;        Private ReadOnly ishoppingcartitemrepository _shoppingcartitemrepository;                     Private ReadOnly iorderrepository _orderrepository; <summary>///Create an order with 2 operations involved: 1.        Remove items in the shopping cart from the shopping cart; 2. Create an order.        Both operations must be completed or failed at the same time. </summary>//<param name= "user" ></param>//<param name= "ShoppingCart" ></p        aram>//<returns></returns> public Order createorder (user user, ShoppingCart shoppingcart)            {var order = New Order (); var shoppingcartitems = _shoppingcartitemrepository.getall (New expressionspecification& Lt Shoppingcartitem> (S= = S.shoopingcart.id = = shoppingcart.id)); if (Shoppingcartitems = = NULL | |!shoppingcartitems.any ()) throw new InvalidOperationException ("No items in the shopping basket"            ); Order.            OrderItems = new list<orderitem> (); foreach (Var shoppingcartitem in shoppingcartitems) {var orderitem = Shoppingcartitem.convertto                OrderItem ();                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. Fulfillment of order service. The specific order service implementation code looks like this:

public class Orderserviceimp:applicationservice, IOrderService {#region Private fileds private readonl        Y 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 user             Repository, 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 = _userreposit Ory.            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 (n EW expressionspecification<order> (O=>o.id.equals (orderId)), Elp=>elp.            OrderItems); Return Mapper.map<order, OrderDto> (order); }//Get all orders for the specified user 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. Implementation of the checkout operation in the HomeController controller. The concrete implementation code looks like this:

public class homecontroller:controllerbase{//<summary>///Billing///</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 = Pro Xy. GetOrder (new Guid (ID));                return View (model);}}        

This allows us to implement the order function in the online bookstore. The specific view interface is the shopping cart page implemented in the previous topic. Here is a detailed look at the order of the specific implementation effect:

Billing page:

Click the Confirm Purchase button and click Confirm in the pop-up box to complete the order creation:

View all order pages via my order:

Iv. Summary

To this, the online bookstore case of the implementation of the order function is completed, the next topic will continue to improve the case, the next topic in the case of the introduction of background management operations. Business or administrator can enter the background management to confirm the delivery of user orders, as well as add goods, classification and other operations. See the next topic for specific implementations.

All implementations of the source code in this topic download: https://github.com/lizhi5753186/OnlineStore_Second/

Classification:. NET domain Driven Design series Tags: DDD, Order, domain Service

The realization of order function in the online bookstore

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.