码迷,mamicode.com
首页 > 其他好文 > 详细

DDD_Demo

时间:2020-06-10 22:41:48      阅读:81      评论:0      收藏:0      [点我收藏+]

标签:用户id   tom   agg   阶段   vol   rest   value   values   count   

现在是挖个坑,实验阶段


--Domain
   --OrderAggregateRoot
     --Order.cs
     --OrderItem.cs
     --ValueObject
     --Event

    public class Order : AggregateRoot<Guid>
    {
        #region 属性

        /// <summary>
        /// 订单号
        /// </summary>
        public string OrderNo { get; private set; }

        /// <summary>
        /// 是否删除
        /// </summary>
        public bool IsDelete { get; private set; }

        public CreateUserInfo CreateUser { get; private set; }

        /// <summary>
        /// 子订单数
        /// </summary>
        public int TotalItemCount { get; private set; }

        private List<OrderItem> _orderItems = new List<OrderItem>();

        public IReadOnlyList<OrderItem> OrderItems => _orderItems;

        #endregion

        #region 构造函数

        private Order()
        {

        }

        private Order(string orderNo, CreateUserInfo createUserInfo, List<OrderItem> items)
        {
            this.CreateUser = createUserInfo;
            this.OrderNo = orderNo;
            this._orderItems = items;

            RestTotalCount();

            Check.NotNull(this.CreateUser, "用户不能为空!");
        }

        #endregion


        #region 对外公开方法


        internal Order CreateOrder(string orderNo, Guid userId, CreateUserInfo createUserInfo, List<OrderItem> items)
        {

            return new Order(orderNo, createUserInfo, items);
        }

        /// <summary>
        /// 设置删除(删除改为未删除,未删除改为删除) 修改状态的同时把
        /// </summary>
        internal void SetIsDelete()
        {
            this.IsDelete = !this.IsDelete;
        }

        /// <summary>
        /// 修改子订单数量
        /// </summary>
        internal void SetOrderItemCount(Guid itemId, int count)
        {
            var orderItem = this._orderItems.FirstOrDefault(p => p.Id == itemId);
            Check.NotNull(orderItem, "未找到此子订单");
            orderItem.SetCount(count);
            RestTotalCount();
        }

        #endregion

        #region 私有方法

        /// <summary>
        /// 重新计算订单的总子订单数
        /// </summary>
        private void RestTotalCount()
        {
            this.TotalItemCount = this._orderItems.Sum(p => p.Count);
        }

        #endregion
    }


    public class OrderItem : Entity<Guid>
    {
        /// <summary>
        /// orderId
        /// </summary>
        public Guid OrderId { get; private set; }

        /// <summary>
        /// 商品名称
        /// </summary>
        public string ProductName { get; private set; }

        /// <summary>
        /// 商品数量
        /// </summary>
        public int Count { get; private set; }

        private OrderItem()
        {

        }

        public OrderItem(Guid orderId, string productName, int count)
        {
            OrderId = orderId;
            ProductName = productName;
            Count = count;
        }

        /// <summary>
        /// 设置子订单数
        /// </summary>
        public void SetCount(int count)
        {
            this.Count = count;
        }


    public class CreateUserInfo : Volo.Abp.Domain.Values.ValueObject
    {
        /// <summary>
        /// 用户Id
        /// </summary>
        public Guid UserId { get; private set; }

        /// <summary>
        /// 用户姓名
        /// </summary>
        public string UserName { get; private set; }

        /// <summary>
        /// 用户此订单预留手机号
        /// </summary>
        public string Tel { get; private set; }

        /// <summary>
        /// 收件地址(应该作为一个值对象(省市区Code))
        /// </summary>
        public string Address { get; private set; }


        private CreateUserInfo()
        {
            
        }

        internal CreateUserInfo(Guid userId, string userName, string tel, string address)
        {
            this.UserId = userId;
            this.UserName = userName;
            this.Tel = tel;
            this.Address = address;

            Check.NotNullOrWhiteSpace(userName, "用户姓名不能为空");
            // ...
        }

        protected override IEnumerable<object> GetAtomicValues()
        {
            yield return UserName;
            yield return Tel;
            yield return Address;
        }
    }


    public class OrderItemCountChangedEvent
    {

        public Guid OrderId { get; private set; }

        public Guid OrderItemId { get; private set; }

        public int OldCount { get; private set; }
        public int NewCount { get; private set; }

        public OrderItemCountChangedEvent(Guid orderId, Guid orderItemId, int oldCount, int newCount)
        {
            OrderId = orderId;
            OrderItemId = orderItemId;
            OldCount = oldCount;
            NewCount = newCount;
        }
    }

DDD_Demo

标签:用户id   tom   agg   阶段   vol   rest   value   values   count   

原文地址:https://www.cnblogs.com/caiyangcc/p/13089751.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!