zoukankan      html  css  js  c++  java
  • Software--电商平台--Module 5 Order & Payment

    2018-01-10  14:11:30

    电商平台 订购和支付 模块

    一: 整体示意图

    二:构建一个框架来处理 领域模型内部发生的事情--领域事件

    1. IDomainEvent 标识模型中的 DomainEvent
    2. IDomainEventHandler<T> where T : IDomainEvent 事件处理程序必须实现的接口。
    3. IDomainEventHandlerFactory 用来获取给定领域事件的处理程序的集合
    4. IEnumerableExtension 获取到 IDomainEventHandler 集合之后, 调用它们的处理方法并将领域事件作为动作的实参。
      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
      
      namespace Agathas.Storefront.Infrastructure.Domain.Events
      {
          public static class IEnumerableExtensions
          {
              public static void ForEach<T>(this IEnumerable<T> source, Action<T> action)
              {
                  foreach (T item in source)
                      action(item);
              }
          }
      
      }
    5. 静态类 DomainEvents -- 为了引发事件。

      支持领域事件所需的框架。

      

     

    二 : 领域对象 

    1. Payment  值对象, 继承自 Infrastructure  -- ValueObjectBase    
    2. OrderItem 实体  继承自 EntityBase<int> 
    3. 与OrderItem 相关联的 Order 

    EntityBase<int>

    using System.Collections.Generic;
    
    namespace Agathas.Storefront.Infrastructure.Domain
    {
        public abstract class EntityBase<TId>
        {
            private List<BusinessRule> _brokenRules = new List<BusinessRule>();
    
            public TId Id { get; set; }
    
            protected abstract void Validate();
    
            public IEnumerable<BusinessRule> GetBrokenRules()
            {
                _brokenRules.Clear();
                Validate();
                return _brokenRules;
            }
    
            protected void AddBrokenRule(BusinessRule businessRule)
            {
                _brokenRules.Add(businessRule);
            }
    
            public override bool Equals(object entity)
            {
                return entity != null
                   && entity is EntityBase<TId>
                   && this == (EntityBase<TId>)entity;
            }
    
            public override int GetHashCode()
            {
                return this.Id.GetHashCode();
            }
    
            public static bool operator ==(EntityBase<TId> entity1,
               EntityBase<TId> entity2)
            {
                if ((object)entity1 == null && (object)entity2 == null)
                {
                    return true;
                }
    
                if ((object)entity1 == null || (object)entity2 == null)
                {
                    return false;
                }
    
                if (entity1.Id.ToString() == entity2.Id.ToString())
                {
                    return true;
                }
    
                return false;
            }
    
            public static bool operator !=(EntityBase<TId> entity1,
                EntityBase<TId> entity2)
            {
                return (!(entity1 == entity2));
            }
        }
    
    }

    ValueObjectBase

    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace Agathas.Storefront.Infrastructure.Domain
    {
        public abstract class ValueObjectBase
        {
            private List<BusinessRule> _brokenRules = new List<BusinessRule>();
    
            public ValueObjectBase()
            {
            }
    
            protected abstract void Validate();
    
            public void ThrowExceptionIfInvalid()
            {
                _brokenRules.Clear();
                Validate();
                if (_brokenRules.Count() > 0)
                {
                    StringBuilder issues = new StringBuilder();
                    foreach (BusinessRule businessRule in _brokenRules)
                        issues.AppendLine(businessRule.Rule);
    
                    throw new ValueObjectIsInvalidException(issues.ToString());
                }
            }
    
            protected void AddBrokenRule(BusinessRule businessRule)
            {
                _brokenRules.Add(businessRule);
            }
        }
    }

       

  • 相关阅读:
    安全编码1
    VPP tips
    VPP概述汇总
    C语言安全编码摘录
    TCP-proxy
    Scipy Lecture Notes学习笔记(一)Getting started with Python for science 1.4. Matplotlib: plotting
    Scipy Lecture Notes学习笔记(一)Getting started with Python for science 1.3. NumPy: creating and manipulating numerical data
    Scipy Lecture Notes学习笔记(一)Getting started with Python for science 1.2. The Python language
    Scipy Lecture Notes学习笔记(一)Getting started with Python for science 1.1. Python scientific computing ecosystem
    25马5跑道,求最快的五匹马的需要比赛的次数
  • 原文地址:https://www.cnblogs.com/masterSoul/p/8258475.html
Copyright © 2011-2022 走看看