1、DDD领域驱动设计实践篇之如何提取模型

2、DDD领域驱动设计之聚合、实体、值对象

其实这里说的基础设施层只是领域层的一些接口和基类而已,没有其他的如日子工具等代码,仅仅是为了说明领域层的一些基础问题

1、领域事件简单实现代码,都是来至ASP.NET设计模式书中的代码

namespace DDD.Infrastructure.Domain.Events
{
public interface IDomainEvent
{
}
}
namespace DDD.Infrastructure.Domain.Events
{
public interface IDomainEventHandler<T> : IDomainEventHandler
where T : IDomainEvent
{
void Handle(T e);
} public interface IDomainEventHandler
{ } }
namespace DDD.Infrastructure.Domain.Events
{
public interface IDomainEventHandlerFactory
{
IEnumerable<IDomainEventHandler<T>> GetDomainEventHandlersFor<T>(T domainEvent)
where T : IDomainEvent;
} }
namespace DDD.Infrastructure.Domain.Events
{
public class StructureMapDomainEventHandlerFactory : IDomainEventHandlerFactory
{
public IEnumerable<IDomainEventHandler<T>> GetDomainEventHandlersFor<T>
(T domainEvent) where T : IDomainEvent
{
return ObjectFactory.GetAllInstances<IDomainEventHandler<T>>();
}
} }
namespace DDD.Infrastructure.Domain.Events
{
public static class DomainEvents
{
public static IDomainEventHandlerFactory DomainEventHandlerFactory { get; set; } public static void Raise<T>(T domainEvent) where T : IDomainEvent
{
var handlers = DomainEventHandlerFactory.GetDomainEventHandlersFor(domainEvent);
foreach (var item in handlers)
{
item.Handle(domainEvent);
}
}
}
}

2、领域层接口代码,很多都是来至MS NLayer代码和google code

namespace DDD.Infrastructure.Domain
{
public interface IEntity<out TId>
{
TId Id { get; }
}
}
namespace DDD.Infrastructure.Domain
{
public interface IAggregateRoot : IAggregateRoot<string>
{
} public interface IAggregateRoot<out TId> : IEntity<TId>
{
}
}
namespace DDD.Infrastructure.Domain
{
public abstract class EntityBase : EntityBase<string>
{
protected EntityBase()
:this(null)
{
} protected EntityBase(string id)
{
this.Id = id;
} public override string Id
{
get
{
return base.Id;
}
set
{
base.Id = value;
if (string.IsNullOrEmpty(this.Id))
{
this.Id = EntityBase.NewId();
}
}
} public static string NewId()
{
return Guid.NewGuid().ToString("N");
}
} public abstract class EntityBase<TId> : IEntity<TId>
{
public virtual TId Id
{
get;
set;
} public virtual IEnumerable<BusinessRule> Validate()
{
return new 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));
}
} }
namespace DDD.Infrastructure.Domain
{
public interface IRepository<TEntity> : IRepository<TEntity, string>
where TEntity : IAggregateRoot
{
} public interface IRepository<TEntity, in TId>
where TEntity : IAggregateRoot<TId>
{
void Modify(TEntity entity);
void Add(TEntity entity);
void Remove(TId id); IQueryable<TEntity> Where(Expression<Func<TEntity, bool>> predicate);
IQueryable<TEntity> All();
TEntity Find(TId id);
}
}
namespace DDD.Infrastructure.Domain
{
public class BusinessRule
{
private string _property;
private string _rule; public BusinessRule(string rule)
{
this._rule = rule;
} public BusinessRule(string property, string rule)
{
this._property = property;
this._rule = rule;
} public string Property
{
get { return _property; }
set { _property = value; }
} public string Rule
{
get { return _rule; }
set { _rule = value; }
}
} }
namespace DDD.Infrastructure.Domain
{
/// <summary>
/// Abstract Base Class for Value Objects
/// Based on CodeCamp Server codebase http://code.google.com/p/codecampserver
/// </summary>
/// <typeparam name="TObject">The type of the object.</typeparam>
[Serializable]
public class ValueObject<TObject> : IEquatable<TObject> where TObject : class
{ /// <summary>
/// Implements the operator ==.
/// </summary>
/// <param name="left">The left.</param>
/// <param name="right">The right.</param>
/// <returns>The result of the operator.</returns>
public static bool operator ==(ValueObject<TObject> left, ValueObject<TObject> right)
{
if (ReferenceEquals(left, null))
return ReferenceEquals(right, null); return left.Equals(right);
} /// <summary>
/// Implements the operator !=.
/// </summary>
/// <param name="left">The left.</param>
/// <param name="right">The right.</param>
/// <returns>The result of the operator.</returns>
public static bool operator !=(ValueObject<TObject> left, ValueObject<TObject> right)
{
return !(left == right);
} /// <summary>
/// Equalses the specified candidate.
/// </summary>
/// <param name="candidate">The candidate.</param>
/// <returns>
/// true if the current object is equal to the <paramref name="candidate"/> parameter; otherwise, false.
/// </returns>
public override bool Equals(object candidate)
{
if (candidate == null)
return false; var other = candidate as TObject; return Equals(other);
} /// <summary>
/// Indicates whether the current object is equal to another object of the same type.
/// </summary>
/// <param name="other">An object to compare with this object.</param>
/// <returns>
/// true if the current object is equal to the <paramref name="other"/> parameter; otherwise, false.
/// </returns>
public virtual bool Equals(TObject other)
{
if (other == null)
return false; Type t = GetType(); FieldInfo[] fields = t.GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public); foreach (FieldInfo field in fields)
{
object otherValue = field.GetValue(other);
object thisValue = field.GetValue(this); //if the value is null...
if (otherValue == null)
{
if (thisValue != null)
return false;
}
//if the value is a datetime-related type...
else if ((typeof(DateTime).IsAssignableFrom(field.FieldType)) ||
((typeof(DateTime?).IsAssignableFrom(field.FieldType))))
{
string dateString1 = ((DateTime)otherValue).ToLongDateString();
string dateString2 = ((DateTime)thisValue).ToLongDateString();
if (!dateString1.Equals(dateString2))
{
return false;
}
continue;
}
//if the value is any collection...
else if (typeof(IEnumerable).IsAssignableFrom(field.FieldType))
{
IEnumerable otherEnumerable = (IEnumerable)otherValue;
IEnumerable thisEnumerable = (IEnumerable)thisValue; if (!otherEnumerable.Cast<object>().SequenceEqual(thisEnumerable.Cast<object>()))
return false;
}
//if we get this far, just compare the two values...
else if (!otherValue.Equals(thisValue))
return false;
} return true;
} /// <summary>
/// Serves as a hash function for a particular type.
/// </summary>
/// <returns>
/// A hash code for the current <see cref="T:System.Object"/>.
/// </returns>
public override int GetHashCode()
{
IEnumerable<FieldInfo> fields = GetFields(); const int startValue = 17;
const int multiplier = 59; int hashCode = startValue; foreach (FieldInfo field in fields)
{
object value = field.GetValue(this); if (value != null)
hashCode = hashCode * multiplier + value.GetHashCode();
} return hashCode;
} /// <summary>
/// Gets the fields.
/// </summary>
/// <returns>FieldInfo collection</returns>
private IEnumerable<FieldInfo> GetFields()
{
Type t = GetType(); var fields = new List<FieldInfo>(); while (t != typeof(object))
{
fields.AddRange(t.GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public)); t = t.BaseType;
} return fields;
} }
}

  

DDD领域驱动设计之领域基础设施层的更多相关文章

  1. DDD领域驱动设计之领域服务

    1.DDD领域驱动设计实践篇之如何提取模型 2.DDD领域驱动设计之聚合.实体.值对象 3.DDD领域驱动设计之领域基础设施层 什么是领域服务,DDD书中是说,有些类或者方法,放实体A也不好,放实体B ...

  2. 基于ABP落地领域驱动设计-04.领域服务和应用服务的最佳实践和原则

    目录 系列文章 领域服务 应用服务 学习帮助 系列文章 基于ABP落地领域驱动设计-00.目录和前言 基于ABP落地领域驱动设计-01.全景图 基于ABP落地领域驱动设计-02.聚合和聚合根的最佳实践 ...

  3. DDD领域驱动设计:领域事件

    1 前置阅读 在阅读本文章之前,你可以先阅读: DDD领域驱动设计是什么 DDD领域驱动设计:实体.值对象.聚合根 DDD领域驱动设计:仓储 MediatR一个优秀的.NET中介者框架 2 什么是领域 ...

  4. DDD领域驱动设计之运用层代码

    1.DDD领域驱动设计实践篇之如何提取模型 2.DDD领域驱动设计之聚合.实体.值对象 3.DDD领域驱动设计之领域基础设施层 4.DDD领域驱动设计之领域服务 5.整体DEMO代码 什么是运用层,说 ...

  5. 解构领域驱动设计(一):为什么DDD能够解决软件复杂性

    1 为什么我要研究领域驱动设计 1.1 设计方法各样且代码无法反映设计 我大概从2017年10月份开始研究DDD,当时在一家物流信息化的公司任职架构师,研究DDD的初衷在于为团队寻找一种软件设计的方法 ...

  6. 如何使用ABP进行软件开发(2) 领域驱动设计和三层架构的对比

    简述 上一篇简述了ABP框架中的一些基础理论,包括ABP前后端项目的分层结构,以及后端项目中涉及到的知识点,例如DTO,应用服务层,整洁架构,领域对象(如实体,聚合,值对象)等. 笔者也曾经提到,AB ...

  7. 基于ABP落地领域驱动设计-01.全景图

    什么是领域驱动设计? 领域驱动设计(简称:DDD)是一种针对复杂需求的软件开发方法.将软件实现与不断发展的模型联系起来,专注于核心领域逻辑,而不是基础设施细节.DDD适用于复杂领域和大规模应用,而不是 ...

  8. 基于ABP落地领域驱动设计-05.实体创建和更新最佳实践

    目录 系列文章 数据传输对象 输入DTO最佳实践 不要在输入DTO中定义不使用的属性 不要重用输入DTO 输入DTO中验证逻辑 输出DTO最佳实践 对象映射 学习帮助 系列文章 基于ABP落地领域驱动 ...

  9. 基于ABP落地领域驱动设计-06.正确区分领域逻辑和应用逻辑

    目录 系列文章 领域逻辑和应用逻辑 多应用层 示例:正确区分应用逻辑和领域逻辑 学习帮助 系列文章 基于ABP落地领域驱动设计-00.目录和前言 基于ABP落地领域驱动设计-01.全景图 基于ABP落 ...

随机推荐

  1. DOS:将某文件夹下面的所有某一类型文件名输出

    C:\Users\lv>cd /d C:\Siemens\Teamcenter11\lib C:\Siemens\Teamcenter11\lib>dir /B *.lib >lis ...

  2. maven使用

    Maven是一个项目管理和构建自动化工具.但是对于我们程序员来说,我们最关心的是它的项目构建功能,它定义了项目开发的几个标准步骤:编译,发布,单元测试及部署以帮助项目开发 最简单的时候场景是,在pom ...

  3. (python) 标准模块sys和os的使用

    一.sys模块 包含了系统的相关的功能.我们来学习sys.argv,它包含命令行参数. 例子:定义了一个add函数,用来实现两个整数的相加. #! coding=utf-8 # usersys.py ...

  4. 进阶——scrapy登录豆瓣解决cookie传递问题并爬取用户参加过的同城活动©seven_clear

    最近在用scrapy重写以前的爬虫,由于豆瓣的某些信息要登录后才有权限查看,故要实现登录功能.豆瓣登录偶尔需要输入验证码,这个在以前写的爬虫里解决了验证码的问题,所以只要搞清楚scrapy怎么提交表单 ...

  5. 接口测试总结<转>

    本文主要分为两个部分: 第一部分:主要从问题出发,引入接口测试的相关内容并与前端测试进行简单对比,总结两者之前的区别与联系.但该部分只交代了怎么做和如何做?并没有解释为什么要做? 第二部分:主要介绍为 ...

  6. Git很好的教程

    本文地址:http://www.cnblogs.com/yhLinux/p/4067064.html 很好的Git教程,作为初学者,跟着作者的教程走了一遍之后,基本熟悉了Git的常用操作,此教程简洁明 ...

  7. (转)详解Linux Top 命令

    top 命令是最流行的性能监视工具之一,我们必需了解.它是一个优秀的交互式工具,用于监视性能.它提供系统整体性能,但报告进程信息才是 top 命令的长处.top 命令交互界面如下图所视:

  8. torch-ios框架XCODE使用备忘

    1.首先编译框架   ./generate_ios_framework 2.把框架包含进project  在general-link Frameweork and Libraries 加入这个框架,注 ...

  9. Xml生成节点声明

    XmlDocument xmlDoc = new XmlDocument(); XmlDeclaration declaration = xmlDoc.CreateXmlDeclaration(&qu ...

  10. 命令行解析Crash文件

    做了快两年的开发了,没有写过博客,最近公司app上架,程序崩溃被拒绝了,可是给的crash文件,又看不出哪里的问题,网上各种搜,终于找到了解决的办法,想想还是写个博客吧,希望给哪些也遇到这类问题的朋友 ...