上篇文章主要講述了經銷商上下文的需求與POCO對象,這篇文章主要講述該界限上下文的倉儲與領域邏輯的實現。
關於界限上下文與EF Core數據訪問上下文參考產品上下文相應的實現,這里不再累述。
因為在經銷商上下文中有兩個聚合,一個是經銷商聚合,一個是登錄聚合,所以我們需要實現兩個倉儲接口:
1.經銷商倉儲接口定義:
public interface IDealerRepository { void CreateDealer<T>(T dealer) where T : class, IAggregationRoot; //獲取上級經銷商(當前代注冊經銷商)的層次結構 int GetParentDealerLayer(Guid dealerid); //將上級經銷商(代注冊經銷商)的子個數加一 void AddParentSubCount(Guid? parentdealerid); //減去父進銷商的電子幣(用於注冊和下單時,扣減經銷商的電子幣) void SubParentEleMoney(Guid parentdealerid, decimal subelemoney); //下訂單時,增加經銷商的PV void AddDealerPV(Guid dealerid, decimal orderpv); }
2.登錄倉儲接口定義:
public interface ILoginRepository { void CreateLogin<T>(T login) where T : class, IAggregationRoot; Guid UserLogin(string tel, string password); }
3.具體對應的倉儲實現在倉儲實現的項目中自己實現,主要通過EF Core完成數據庫的訪問與操作。
4.經銷商聚合中聯系人對象的領域邏輯實現:
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.經銷商聚合中經銷商層次結構對象的領域邏輯實現:
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.經銷商聚合中經銷商對象的領域邏輯實現:
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.普通會員; } else if (telmoney >= 2000 && telmoney < 4000) { this.CardType = CardType.銀卡會員; } else { this.CardType = CardType.金卡會員; } this.SubCount = 0; this.TotalPV = 0; this.JiangJInMoney = 0; this.Contacts = contacts; this.DealerTree = new DealerTree(idealerrepository).CreateDealerTree(parentid, id); return this; } }
7.登錄聚合中登錄對象的領域邏輯實現:
public partial class Login { public Login CreateLogin(string code,Guid dealerid) { this.Id = Guid.NewGuid(); //手機號 this.Code = code; //默認初始密碼 this.Password=MD5Encrption.GetMd5Str("111111"); this.DealerId = dealerid; return this; } }
這樣,我們就完成了基本數據庫的訪問、操作和相關領域邏輯的實現。
QQ討論群:309287205
DDD實戰進階視頻請關注微信公眾號: