领域驱动设计DDD与微服务实战案例进阶

领域驱动设计实战进阶第一波(九):实现经销商上下文仓储与领域逻辑

2018-05-08  本文已影响9人  malaoko

DDD实战进阶第一波(九):开发一般业务的大健康行业直销系统(实现经销商上下文仓储与领域逻辑)

上篇文章主要讲述了经销商上下文的需求与POCO对象,这篇文章主要讲述该界限上下文的仓储与领域逻辑的实现。

关于界限上下文与EF Core数据访问上下文参考产品上下文相应的实现,这里不再累述。

因为在经销商上下文中有两个聚合,一个是经销商聚合,一个是登录聚合,所以我们需要实现两个仓储接口:

1.经销商仓储接口定义:

publicinterface IDealerRepository

    {

        voidCreateDealer(T dealer)whereT :class, IAggregationRoot;

        //获取上级经销商(当前代注册经销商)的层次结构int GetParentDealerLayer(Guid dealerid);

        //将上级经销商(代注册经销商)的子个数加一voidAddParentSubCount(Guid? parentdealerid);

        //减去父进销商的电子币(用于注册和下单时,扣减经销商的电子币)voidSubParentEleMoney(Guid parentdealerid,decimal subelemoney);

        //下订单时,增加经销商的PVvoidAddDealerPV(Guid dealerid,decimal orderpv);

    }

2.登录仓储接口定义:

publicinterface ILoginRepository

    {

        voidCreateLogin(T login)whereT :class, IAggregationRoot;

        Guid UserLogin(stringtel,string password);

    }

3.具体对应的仓储实现在仓储实现的项目中自己实现,主要通过EF Core完成数据库的访问与操作。

4.经销商聚合中联系人对象的领域逻辑实现:

publicpartialclass Contact

    {

        publicContact CreateContact(Guid dealerid,stringname,stringtel,stringprovince,string city,

            stringzero,stringstreet,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)

            {

                case1:this.IsDefault = IsDefaultContact.默认;

                    break;

                case2:this.IsDefault = IsDefaultContact.非默认;

                    break;

            }

            returnthis;

        }

    }

5.经销商聚合中经销商层次结构对象的领域逻辑实现:

publicpartialclass DealerTree

    {

        privatereadonly IDealerRepository idealerrepository;

        public DealerTree(IDealerRepository idealerrepository)

        {

            this.idealerrepository = idealerrepository;

        }

        publicDealerTree 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;

            returnthis;

        }

    }

6.经销商聚合中经销商对象的领域逻辑实现:

publicpartialclass Dealers

    {

        privatereadonly IDealerRepository idealerrepository;

        public Dealers(IDealerRepository idealerrepository)

        {

            this.idealerrepository = idealerrepository;

        }

        publicDealers RegisterDealer(Guid id,stringname,stringtel,decimaltelmoney,List            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.普通会员;

            }

            elseif(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);

            returnthis;

        }

    }

7.登录聚合中登录对象的领域逻辑实现:

publicpartialclass Login

    {

        publicLogin CreateLogin(string code,Guid dealerid)

        {

            this.Id = Guid.NewGuid();

            //手机号this.Code = code;

            //默认初始密码this.Password=MD5Encrption.GetMd5Str("111111");

            this.DealerId = dealerid;

            returnthis;

        }

    }

这样,我们就完成了基本数据库的访问、操作和相关领域逻辑的实现。

QQ讨论群:309287205

DDD实战进阶视频请关注微信公众号:

上一篇下一篇

猜你喜欢

热点阅读