Read Catalogue
- Objective
- Modeling
- Realize
- Conclusion
First, preface
The previous several have realized a basic purchase + price calculation process, this time let the price plump some, add a member price concept. Membership price in the current mainstream e-commerce, is a less common model, the problem is:
1. Increase the complexity of the operation, how the member price and promotion combination, such as should be used before the discount or after the use of folding.
2. If it is pre-folded then it is necessary to consider the amount of the full minus type promotion to meet the point threshold instead of a relatively improved.
3. If you enjoy multiple benefits after the discount, you need to take into account the cost control.
In our practiced hand demo, we temporarily decided to let the member price be used after the discount, and only if not meet the full reduction of the promotion of the case only valid.
Second, modeling
so start modeling first, this time the member price is relatively simple, is generally a discount rate of the problem. Just create several relationships to meet your needs, as follows:
Member-to-rank relationship (value object): I think the downgrade of the hierarchy should be handled in the "user context", so the price context here is only a redundancy of the data, and the "user context" is a final consistent relationship. Of course, you can not do this redundancy, from the remote service to obtain, which can be weighed according to the actual situation. I think the user level change is a non-HF data, so redundancy here can reduce the number of RPCs.
Rank-to-discount relationship (Value object): This data should not change as soon as it is determined, and will be used for external publicity, without a doubt established as a value object. As shown in 1:
"Figure 1"
third, the use of
First confirm the 2 value object data source defined above, tentatively take the membership and Hierarchy Relationship (userrolerelation) from the user context, because we have not started to introduce the concept of final consistency ; The relationship between rank and discount (rolediscountrelation) has a local context. So for the first time here, in the price context, we need access to external resources, and we need to create an anti-corrosion layer to handle this RPC interaction. In this case, together with the purchase context, put the embalming layer into the virtual folder for each context, as shown in 2:
"Figure 2"
The following code defines the interfaces for these 2 data fetches:
Public Interface Iuserservice { userrolerelation getuserrolerelation (string userId); }
Public Interface Irolediscountrelationrepository// : irepository<rolediscountrelation> { Rolediscountrelation Get (string Roleid); }
You can see the code for a line of comments in Irolediscountrelationrepository because there is a need to persist a value object to the repository independently, and only support the persistence of the aggregate root in our previous design. So here's a temporary manual definition of the code written in this article, will be in the next article specifically about how to deal with this situation.
then because the calculation of the membership price needs to be calculated according to the user, it is necessary to add the UserID parameter in the cartrequest, let the purchase context pass the data to ensure the business needs here.
Public class cartrequest { publicstringgetset;} Public string UserId Get Set ; } Public Get Set ; } }
The calculation of the member's price is the function of rank and discount (value object) , which can be used to create a method in this value object, and the implementation of this is the calculation of the discounted amount for the incoming price and then returns. The following code:
Public decimal Calculatediscountedprice (decimal price ) { return price * Convert.todecimal ( This . discountrate); }
Then we started to combine it with the previous promotional business. Remember the data structure returned by our previous calculatesalepriceservice.calculate (Cartrequest cart) method (Portal: http://www.cnblogs.com/Zachary-Fan/ p/ddd_7.html):
return New Calculatedcartdto { boughtproducts.where (ent = fullgroupdtos.selectmany (e = e.calculatedcartitems). All (E = E.productid ! = ent. ProductId)) = ent. Todto ()). ToArray (), = Fullgroupdtos.toarray (), = cart. Cartid };
All we have to do is calculate the value of the data assigned to Calculatedcartitems for the membership price, because these are the items that are not participating in the full-cut promotion. But here in order for Boughtproduct to support our business operations and assume that the interface needs to show how much the membership price and promotional price are respectively discounted amount, so in the Boughtproduct value of the object added a Reducepricebymemberprice, Used to store the amount waived by the member's price. Then add the corresponding method to set the member's discount amount in Boughtproduct, as follows:
PublicBoughtproduct Changereducepricebymemberprice (decimalReducepricebymemberprice) { if(Reducepricebymemberprice <0) Throw NewArgumentException ("Reducepricebymemberprice cannot be less than 0"); varSelectedmultiprodcutspromotionid = This. Inmultiproductpromotionrule = =NULL?NULL: ((promotionrule) This. Inmultiproductpromotionrule). Promotoinid; return NewBoughtproduct ( This. ProductId, This. Quantity, This. UnitPrice, This. Reduceprice, Reducepricebymemberprice, This. _promotionrules, Selectedmultiprodcutspromotionid); }
Finally the Calculateservice is adjusted to 3 this way:
"Figure 3"
Iv. Conclusion
May be a bit boring halfway, but I think my topic is the use of DDD from 0 to implement an e-commerce website process, DDD Business is the core, so the details of business and the use of DDD concept must not be lost.
The source address of this article: Https://github.com/ZacharyFan/DDDDemo/tree/Demo8.
Zachary_fan
Source: http://www.cnblogs.com/Zachary-Fan/p/DDD_8.html
The conclusion of the preface's Modeling implementation
How to design an e-commerce website Step by step with DDD (eight)--integration of membership price