/// <summary>
        /// 根据给定的聚合根类型及其事件重建一个聚合根对象,该对象只用于显示,不能参与任何业务逻辑;
        /// 所以,该方法不允许在正常业务逻辑的代码中使用,该方法返回的聚合根也不受工作单元监控,只能用于查看聚合根在某个事件版本时的状态;
        /// </summary>
        public static AggregateRoot BuildAggregateRootForViewOnly(Type aggregateRootType, IEnumerable <AggregateRootEvent> evnts)
        {
            if (aggregateRootType == null)
            {
                throw new ArgumentNullException("TAggregateRoot");
            }
            if (evnts == null)
            {
                throw new ArgumentNullException("evnts");
            }

            AggregateRoot aggregateRoot = null;

            if (evnts.Count() > 0)
            {
                aggregateRoot = DependencyResolver.Resolve <IAggregateRootFactory>().CreateAggregateRoot(aggregateRootType);
                aggregateRoot.ReplayEvents(evnts);
            }

            return(aggregateRoot);
        }
Пример #2
0
        /// <summary>
        /// 尝试从快照获取聚合根
        /// </summary>
        private bool TryGetFromSnapshot(string aggregateRootId, long maxEventVersion, out T aggregateRoot)
        {
            aggregateRoot = null;

            var snapshot = _snapshotStoreProvider.GetSnapshotStore <T>().GetLastSnapshot(aggregateRootId, maxEventVersion);

            if (snapshot != null && snapshot.IsValid())
            {
                T aggregateRootFromSnapshot = DependencyResolver.Resolve <ISnapshotter>().RestoreFromSnapshot(snapshot) as T;
                if (aggregateRootFromSnapshot != null)
                {
                    //验证从快照还原出来的聚合根是否有效
                    ValidateAggregateRootFromSnapshot(aggregateRootFromSnapshot, aggregateRootId);
                    //如果从快照得到的聚合根有效,则将发生在该快照之后的事件进行重演
                    var committedEventsAfterSnapshot = _eventStoreProvider.GetEventStore <T>().GetEvents(aggregateRootId, snapshot.Version + 1, maxEventVersion);
                    aggregateRootFromSnapshot.ReplayEvents(committedEventsAfterSnapshot);
                    aggregateRoot = aggregateRootFromSnapshot;
                    return(true);
                }
            }

            return(false);
        }
Пример #3
0
        /// <summary>
        /// 注册组件到容器
        /// </summary>
        public Configuration RegisterComponents(params Assembly[] assemblies)
        {
            DependencyResolver.RegisterType(typeof(DefaultAggregateRootTypeProvider));
            DependencyResolver.RegisterType(typeof(DefaultAggregateRootEventCallbackMetaDataProvider));
            DependencyResolver.RegisterType(typeof(DefaultDomainHandlerMetaDataProvider));
            DependencyResolver.RegisterType(typeof(DefaultAggregateRootEventTypeProvider));
            DependencyResolver.RegisterType(typeof(DefaultSnapshotTypeProvider));
            DependencyResolver.RegisterType(typeof(DefaultTypeNameMapper));
            DependencyResolver.RegisterType(typeof(DefaultSyncMessageBus));
            DependencyResolver.RegisterType(typeof(Log4NetLoggerFactory));
            DependencyResolver.RegisterType(typeof(DefaultAggregateRootFactory));
            DependencyResolver.RegisterType(typeof(JsonNetSerializer));
            DependencyResolver.RegisterType(typeof(DefaultSnapshotter));
            DependencyResolver.RegisterType(typeof(NoSnapshotPolicy));
            DependencyResolver.RegisterType(typeof(Repository));
            DependencyResolver.RegisterType(typeof(JsonMessageSerializer));
            DependencyResolver.RegisterType(typeof(InMemorySubscriptionStorage));
            DependencyResolver.RegisterType(typeof(MsmqMessageTransport));
            DependencyResolver.RegisterType(typeof(DefaultAsyncMessageBus));

            DependencyResolver.Resolve <IAggregateRootTypeProvider>().RegisterAllAggregateRootTypesInAssemblies(assemblies);
            DependencyResolver.Resolve <IAggregateRootEventCallbackMetaDataProvider>().RegisterAllEventCallbackMetaDataInAssemblies(assemblies);
            DependencyResolver.Resolve <IDomainHandlerMetaDataProvider>().RegisterAllEventSubscribersInAssemblies(assemblies);

            DependencyResolver.RegisterTypes(TypeUtils.IsComponent, assemblies);
            DependencyResolver.RegisterTypes(TypeUtils.IsRepository, assemblies);
            DependencyResolver.RegisterTypes(TypeUtils.IsService, assemblies);
            DependencyResolver.RegisterTypes(TypeUtils.IsEventStore, assemblies);
            DependencyResolver.RegisterTypes(TypeUtils.IsSnapshotStore, assemblies);

            RegisterTypeNameMappings(assemblies);
            RegisterAggregateRootEvents(assemblies);
            InitializeSyncMessageBus(assemblies);

            return(this);
        }
Пример #4
0
 /// <summary>
 /// 在领域中以加锁的方式唤醒指定ID的聚合根并返回该聚合根。
 /// <remarks>
 /// 该方法的使用背景:
 /// 在应用EventSourcing的架构下,我们一般通过ID来关联聚合根;正因为这个原因导致如果我们要从
 /// 一个聚合根想去导航到另一个聚合根实现某种交互时,必须通过某种容易理解的措施获取ID所对应的聚合根的引用;
 /// 出于这个目的,我们在框架中提供此方法可以让当前聚合根能够方便地在领域中“唤醒”某个指定的聚合根。
 /// </remarks>
 /// </summary>
 /// <param name="id">要唤醒的聚合根的ID</param>
 protected T WakeupAggregateRootWithLock <T>(object id) where T : AggregateRoot
 {
     AssertCurrentAggregateRoot();
     return(DependencyResolver.Resolve <IRepository>().GetByIdWithLock <T>(id));
 }