DDD領域驅動設計:領域基礎設施層

來源:互聯網
上載者:User

其實這裡說的基礎設施層只是領域層的一些介面和基類而已,沒有其他的如日子工具等代碼,僅僅是為了說明領域層的一些基礎問題

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));        }    }     }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.