In previous articles in this series, we focused on product and reseller context-specific implementations, and you’ve gotten a sense of DDD methodology and architecture. But in both bounded contexts, the business logic is simple and does not use much value object content. From the beginning of this article, we will talk about the content of order bound context implementation, where the business logic is relatively complex, and there are a large number of value objects introduced for logical processing.
Demand order context mainly generate the corresponding order items, that are related to each order items order products and purchase quantity and generate the total order items, order items total PV, at the same time, the total order items and order items total PV will accumulate to the total order and order total PV, at the same time, according to the total order to deduct the current distributor of electronic currency, can also according to the purchase of PV products, Add the PV value of the current dealer.
1. Domain model of order-bound context:
From the domain model above, you can see that the order is the aggregation root and the order details are the aggregation entities. The order aggregation root always has three value objects: total price, total PV and harvest address. The order detail entity has three value objects: total price, total PV and product information.
2. Detailed Total value Objects:
public partial class OrderItemTotalPrice { public decimal SubTotalPrice { get; set; }}Copy the code
3. Detailed total PV value objects:
public partial class OrderItemTotalPV { public decimal SubTotalPV { get; set; }}Copy the code
4. Product information value object:
public partial class ProductSKUs { public string ProductSPUName { get; set; } public decimal ProductPrice { get; set; } public decimal ProductPV { get; set; } public Guid ProductSKUId { get; set; }}Copy the code
5. Order Details Entity:
public partial class OrderItem : IEntity { public string Code { get; set; } [Key] public Guid Id { get ; set ; } public OrderItemTotalPrice OrderItemTotalPrice { get; set; } public OrderItemTotalPV OrderItemTotalPV { get; set; } public ProductSKUs ProductSKUs { get; set; } public int Count { get; set; }}Copy the code
The order detail entity introduces OrderItemTotalPrice, OrderItemTotalPV, ProductSKUs and has its own Code and Count attributes.
6. Total Order Price
public partial class OrderTotalPrice { public decimal TotalPrice { get; set; }}Copy the code
7. Total PV value object of the order:
public partial class OrderTotalPV { public decimal TotalPV { get; set; }}Copy the code
8. Order receiving address value object:
Public partial class OrderStreet {// province public string Privince {get; set; } // public string City {get; set; } // public string Zero {get; set; } // public string Street {get; set; }}Copy the code
9. Order aggregation root:
public partial class Orders : IAggregationRoot { public string Code { get; set ; } [Key] public Guid Id { get ; set; } public OrderStreet OrderStreet { get; set; } public OrderTotalPV OrderTotalPV { get; set; } public OrderTotalPrice OrderTotalPrice { get; set; } public DateTime OrderDateTime { get; set; } public Guid OrderDealerId { get; set; } public List<OrderItem> OrderItems { get; set; } public string Telephone { get; set; }}Copy the code
The order aggregation root introduces three value objects OrderStreet, OrderTotalPV and OrderTotalPrice. Code, DateTime, Telephone, OrderItems, etc.
Mysql > create database table;
As explained in the previous article, we can generate the corresponding database table according to the POCO model described above. Note that OrderItems can be automatically recognized as the associated table of Orders. The other several value objects we need to consider whether to generate a separate table or exist as a column of the table as a related entity or aggregate root. We exist these value objects as columns in the associated aggregate root or entity table. EF Core does not automatically handle how these value objects are stored in the database, we need to specify manually:
public class OrderEFCoreContext:DbContext,IOrderContext { public DbSet<Orders> Order { get; set; } public DbSet<OrderItem> OrderItem { get; set; {} protected override void OnConfiguring (DbContextOptionsBuilder optionBuilder) optionBuilder. UseSqlServer (" database connection string "); } protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<Orders>().OwnsOne(p => p.OrderStreet); modelBuilder.Entity<Orders>().OwnsOne(p => p.OrderTotalPrice); modelBuilder.Entity<Orders>().OwnsOne(p => p.OrderTotalPV); modelBuilder.Entity<OrderItem>().OwnsOne(p => p.OrderItemTotalPrice); modelBuilder.Entity<OrderItem>().OwnsOne(p => p.OrderItemTotalPV); modelBuilder.Entity<OrderItem>().OwnsOne(p => p.ProductSKUs); }Copy the code
As you can see from the code above, when OnModelCreating, you can specify six value objects to be included in the corresponding aggregate root and entity-related table.
DDD combat advanced video please pay attention to wechat public number: MSSHCJ