應用程式框架實戰十八:DDD分層架構之聚合
阿新 • • 發佈:2018-12-27
namespace Util.Domains { /// <summary> /// 實體 /// </summary> public interface IEntity { } /// <summary> /// 實體 /// </summary> /// <typeparam name="TKey">標識型別</typeparam> public interface IEntity<out TKey> : IEntity { /// <summary>/// 標識 /// </summary> TKey Id { get; } } } namespace Util.Domains { /// <summary> /// 聚合根 /// </summary> public interface IAggregateRoot : IEntity { /// <summary> /// 版本號(樂觀鎖) /// </summary> byte[] Version { get; set; } } /// <summary> /// 聚合根 /// </summary> /// <typeparam name="TKey">標識型別</typeparam> public interface IAggregateRoot<out TKey> : IEntity<TKey>, IAggregateRoot { } } namespace Util.Domains { /// <summary> /// 聚合根 /// </summary>/// <typeparam name="TKey">標識型別</typeparam> public abstract class AggregateRoot<TKey> : EntityBase<TKey>, IAggregateRoot<TKey> { /// <summary> /// 初始化聚合根 /// </summary> /// <param name="id">標識</param> protected AggregateRoot( TKey id ) : base( id ) { } /// <summary> /// 版本號(樂觀鎖) /// </summary> public byte[] Version { get; set; } } } using System; using System.ComponentModel.DataAnnotations; using Util.Validations; namespace Util.Domains { /// <summary> /// 聚合根 /// </summary> public abstract class AggregateRoot : AggregateRoot<Guid> { /// <summary> /// 初始化聚合根 /// </summary> /// <param name="id">標識</param> protected AggregateRoot( Guid id ) : base( id ){ } /// <summary> /// 驗證 /// </summary> protected override void Validate( ValidationResultCollection results ) { if ( Id == Guid.Empty ) results.Add( new ValidationResult( "Id不能為空" ) ); } } }