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领域驱动设计之领域基础设施层的更多相关文章
- DDD领域驱动设计之领域服务
1.DDD领域驱动设计实践篇之如何提取模型 2.DDD领域驱动设计之聚合.实体.值对象 3.DDD领域驱动设计之领域基础设施层 什么是领域服务,DDD书中是说,有些类或者方法,放实体A也不好,放实体B ...
- 基于ABP落地领域驱动设计-04.领域服务和应用服务的最佳实践和原则
目录 系列文章 领域服务 应用服务 学习帮助 系列文章 基于ABP落地领域驱动设计-00.目录和前言 基于ABP落地领域驱动设计-01.全景图 基于ABP落地领域驱动设计-02.聚合和聚合根的最佳实践 ...
- DDD领域驱动设计:领域事件
1 前置阅读 在阅读本文章之前,你可以先阅读: DDD领域驱动设计是什么 DDD领域驱动设计:实体.值对象.聚合根 DDD领域驱动设计:仓储 MediatR一个优秀的.NET中介者框架 2 什么是领域 ...
- DDD领域驱动设计之运用层代码
1.DDD领域驱动设计实践篇之如何提取模型 2.DDD领域驱动设计之聚合.实体.值对象 3.DDD领域驱动设计之领域基础设施层 4.DDD领域驱动设计之领域服务 5.整体DEMO代码 什么是运用层,说 ...
- 解构领域驱动设计(一):为什么DDD能够解决软件复杂性
1 为什么我要研究领域驱动设计 1.1 设计方法各样且代码无法反映设计 我大概从2017年10月份开始研究DDD,当时在一家物流信息化的公司任职架构师,研究DDD的初衷在于为团队寻找一种软件设计的方法 ...
- 如何使用ABP进行软件开发(2) 领域驱动设计和三层架构的对比
简述 上一篇简述了ABP框架中的一些基础理论,包括ABP前后端项目的分层结构,以及后端项目中涉及到的知识点,例如DTO,应用服务层,整洁架构,领域对象(如实体,聚合,值对象)等. 笔者也曾经提到,AB ...
- 基于ABP落地领域驱动设计-01.全景图
什么是领域驱动设计? 领域驱动设计(简称:DDD)是一种针对复杂需求的软件开发方法.将软件实现与不断发展的模型联系起来,专注于核心领域逻辑,而不是基础设施细节.DDD适用于复杂领域和大规模应用,而不是 ...
- 基于ABP落地领域驱动设计-05.实体创建和更新最佳实践
目录 系列文章 数据传输对象 输入DTO最佳实践 不要在输入DTO中定义不使用的属性 不要重用输入DTO 输入DTO中验证逻辑 输出DTO最佳实践 对象映射 学习帮助 系列文章 基于ABP落地领域驱动 ...
- 基于ABP落地领域驱动设计-06.正确区分领域逻辑和应用逻辑
目录 系列文章 领域逻辑和应用逻辑 多应用层 示例:正确区分应用逻辑和领域逻辑 学习帮助 系列文章 基于ABP落地领域驱动设计-00.目录和前言 基于ABP落地领域驱动设计-01.全景图 基于ABP落 ...
随机推荐
- CS0234: 命名空间“System.Web.Mvc”中不存在类型或命名空间名称“Html、Ajax”(是否缺少程序集引用?)
从SVN上down下来的程序,编译报了一大堆的错,发现是缺少引用,但是明明引用了,后来打开引用,发现system.web.mvc这个引用打着叹号,如图: 后来重新引用了本机的system.web.mv ...
- Win7上的ASP.NET MVC3项目在Win10上运行的一个坑
先解释一下问题:我原来的电脑环境是Win7+VS2015,因为新换了个电脑环境变成Win10+VS2015了,所以就把原先的项目复制到新的机器上,那么问题来了,原先的一个项目在VS2015上打开竟然直 ...
- ajax实现手机获取验证码
<script type="text/javascript"> var InterValObj; //timer变量,控制时间 var count = 60; //间隔 ...
- jsp实现回车登录
<body onkeydown="if(event.keyCode==13){login()}"> 内容0...... </body> 注:body里面加上 ...
- SAX解析xml文件
需要做一个银行名字的列表. 因为有很多,所以想到了用xml来保存,然后uongDAX解析. public class BankSelectActivity extends BaseActivity{ ...
- 透明activity来实现悬浮蔗罩
第一步, activity的代码, 关键代码是进出动画采用淡入淡出方式,让蔗罩出现消失更自然,全屏 public class RobotGuidceV2Activity extends RoboAct ...
- 第十一章:使用Apriori算法进行关联分析
- SQL Server 全文索引创建
在安装数据库管理系统SQL Server 后,默认情况下全文索引的服务是没有开启的 ,所以首先需要先开启服务,在sql server配置管理器中 (sql server configuration M ...
- html 页面中显示单行省略号
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <t ...
- Microsoft Office 2010/2013安装组件预设
日常维护中,多台电脑需要安装Office,可是Office包含的组件又很多(Excel/Word/PPT/OUTLOOK/ACCESS等),有些是不需要的,默认情况下Office都默认安装,一个一个调 ...