Software--电商平台--Module 5 Order & Payment

2023-03-12,,

2018-01-10  14:11:30

电商平台 订购和支付 模块

一: 整体示意图

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

    IDomainEvent 标识模型中的 DomainEvent
    IDomainEventHandler<T> where T : IDomainEvent 事件处理程序必须实现的接口。
    IDomainEventHandlerFactory 用来获取给定领域事件的处理程序的集合
    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);
    }
    } }

    静态类 DomainEvents -- 为了引发事件。

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

  

 

二 : 领域对象

    Payment  值对象, 继承自 Infrastructure  -- ValueObjectBase  
    OrderItem 实体  继承自 EntityBase<int>
    与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);
}
}
}

   

Software--电商平台--Module 5 Order & Payment的相关教程结束。

《Software--电商平台--Module 5 Order & Payment.doc》

下载本文的Word格式文档,以方便收藏与打印。