領域驅動設計DDD實戰進階第一波(九):開發通常業務的大健康行業直銷系統(實現經銷商上下文倉儲與領域邏輯)

上篇文章主要講述了經銷商上下文的需求與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;
    }
}
複製代碼

這樣,咱們就完成了基本數據庫的訪問、操做和相關領域邏輯的實現。微信

DDD實戰進階視頻請關注微信公衆號:ide

相關文章
相關標籤/搜索