The last article mainly described the requirements and POCO objects of the dealer context. This article mainly describes the implementation of the warehouse and domain logic of the bounded context.
The corresponding implementation of bounded context and EF Core data access context reference product context will not be repeated here.
Because there are two aggregates in the reseller context, one is the reseller aggregate and the other is the login aggregate, we need to implement two repository interfaces:
1. Definition of dealer warehousing interface:
public interface IDealerRepository { void CreateDealer<T>(T dealer) where T : class , IAggregationRoot; // Get the hierarchy of the superior dealer (currently registered dealer) int GetParentDealerLayer(Guid dealerid); //The superior dealer (the registered dealer ) ) of the number of children plus one void AddParentSubCount(Guid? parentdealerid); // Subtract the electronic currency of the parent dealer (used to register and place an order, deduct the electronic currency of the dealer) void SubParentEleMoney(Guid parentdealerid, decimal subelemoney); // Add dealer's PV when placing an order void AddDealerPV(Guid dealerid, decimal orderpv); }
2. Login repository interface definition:
public interface ILoginRepository { void CreateLogin<T>(T login) where T : class, IAggregationRoot; Guid UserLogin(string tel, string password); }
3. The corresponding warehousing implementation is implemented by itself in the warehousing implementation project, and the database access and operation are mainly completed through EF Core.
4. The domain logic implementation of the contact object in the dealer aggregation:
public partial class Contact { public Contact CreateContact(Guid dealerid,string name,string tel,string province,string city, string zero,string street,int isdefault) { this.Id = Guid.NewGuid(); this.DealerId = dealerid; this.ContactName = name; this.ContactTel = tel; this.Province = province; this.City = city; this.Zero = zero; this.Street = street; switch (isdefault) { case 1:this.IsDefault = IsDefaultContact.默认; break; case 2:this.IsDefault = IsDefaultContact.非默认; break; } return this; } }
5. Domain logic implementation of dealer hierarchy objects in dealer aggregation:
public partial class DealerTree { private readonly IDealerRepository idealerrepository; public DealerTree (IDealerRepository idealerrepository) { this.idealerrepository = idealerrepository; } public DealerTree CreateDealerTree(Guid? parentdealerid,Guid dealerid) { this.Id = Guid.NewGuid(); this.DealerId = dealerid; this.ParentDealerId = parentdealerid; this.Layer = parentdealerid == null ? 1 : idealerrepository.GetParentDealerLayer(Guid.Parse(parentdealerid.ToString())) + 1; return this; } }
6. Domain logic implementation of dealer objects in dealer aggregation:
public partial class Dealers { private readonly IDealerRepository idealerrepository; public Dealers (IDealerRepository idealerrepository) { this.idealerrepository = idealerrepository; } public Dealers RegisterDealer(Guid id,string name,string tel,decimal telmoney,List<Contact> contacts,Guid? parentid) { this.Id = id; this.Code = "Code " + name; this.Name = name; this.Tel = tel; this.TotalEleMoney = telmoney; if (telmoney < 2000) { this .CardType = CardType.Ordinary Member; } else if (telmoney >= 2000 && telmoney < 4000) { this .CardType = CardType.Silver Member; } else { this .CardType = CardType.Gold Member; } this.SubCount = 0; this.TotalPV = 0; this.JiangJInMoney = 0; this.Contacts = contacts; this.DealerTree = new DealerTree(idealerrepository).CreateDealerTree(parentid, id); return this; } }
7. The domain logic implementation of the login object in the login aggregation:
public partial class Login { public Login CreateLogin(string code,Guid dealerid) { this .Id = Guid.NewGuid(); // Mobile phone number this .Code = code; // Default initial password this .Password=MD5Encrption.GetMd5Str( " 111111 " ); this .DealerId = dealerid; return this ; } }
In this way, we have completed the basic database access, operation and implementation of related domain logic.
QQ discussion group: 309287205
Please pay attention to the WeChat public account for the advanced video of DDD actual combat: