Пример #1
0
        public Task <IEventStream> GetEventStreamAsync <TAggregateRoot>(AggregateRootIdentity identity) where TAggregateRoot : class, IAggregateRoot
        {
            Contract.Requires(identity != null);
            Contract.Ensures(Contract.Result <Task <IEventStream> >() != null);

            throw new NotImplementedException();
        }
        public Task <TAggregateRoot> GetByIdentityAsync(AggregateRootIdentity identity)
        {
            Contract.Requires(identity != null);
            Contract.Ensures(Contract.Result <Task <TAggregateRoot> >() != null);

            throw new NotImplementedException();
        }
        public TAggregateRoot Build(AggregateRootIdentity identity, TAggregateRootState state)
        {
            Contract.Requires(identity != null);
            Contract.Requires(state != null);
            Contract.Ensures(Contract.Result <TAggregateRoot>() != null);

            throw new NotImplementedException();
        }
Пример #4
0
        public AggregateRootCommandHandlingResult(Guid commandId, AggregateRootIdentity aggregateRootIdentity)
            : base(commandId)
        {
            Contract.Requires(commandId != Guid.Empty);
            Contract.Requires(aggregateRootIdentity != null);

            AggregateRootIdentity = aggregateRootIdentity;
        }
        protected virtual Task <TAggregateRoot> GetAggregateRootAsync(AggregateRootIdentity identity)
        {
            Contract.Requires(identity != null);
            Contract.Ensures(Contract.Result <Task <TAggregateRoot> >() != null);

            // get aggregate root
            return(_repository.GetByIdentityAsync(identity));
        }
Пример #6
0
        public EventStoreCommandHandlingResult(Guid commandId, AggregateRootIdentity aggregateRootIdentity, TransactionIdentity transactionIdentity)
            : base(commandId, aggregateRootIdentity)
        {
            Contract.Requires(commandId != Guid.Empty);
            Contract.Requires(aggregateRootIdentity != null);
            Contract.Requires(transactionIdentity != null);

            TransactionIdentity = transactionIdentity;
        }
Пример #7
0
        protected override TAggregateRoot GetAggregateRoot(AggregateRootIdentity identity)
        {
            // retrieve from cache or get from base handler and store in cache
            var aggregateRoot = _cache.AddOrGetExisting(GetCacheKey(identity), new Lazy <TAggregateRoot>(() => base.GetAggregateRoot(identity)));

            Contract.Assume(aggregateRoot != null);

            return(aggregateRoot);
        }
Пример #8
0
        protected override async Task <TAggregateRoot> GetAggregateRootAsync(AggregateRootIdentity identity)
        {
            // retrieve from cache or get from base handler and store in cache
            var task = _cache.AddOrGetExisting(GetCacheKey(identity), new Lazy <Task <TAggregateRoot> >(() => base.GetAggregateRootAsync(identity)));

            Contract.Assume(task != null);

            return(await task);
        }
Пример #9
0
        private static string GetCacheKey(AggregateRootIdentity identity)
        {
            Contract.Requires(identity != null);
            Contract.Ensures(string.IsNullOrWhiteSpace(Contract.Result <string>()) == false);

            var cacheKey = $"{typeof(TAggregateRoot).FullName}::{identity.Value}";

            Contract.Assume(string.IsNullOrWhiteSpace(cacheKey) == false);

            return(cacheKey);
        }
        public EventProvider(EventProviderIdentity identity,
                             AggregateRootType type,
                             AggregateRootIdentity aggregateRootIdentity)
        {
            Contract.Requires(type != null);
            Contract.Requires(aggregateRootIdentity != null);
            Contract.Requires(identity != null);

            Identity              = identity;
            AggregateRootType     = type;
            AggregateRootIdentity = aggregateRootIdentity;
        }
        public static DomainEventCollection Create(Create command)
        {
            var identity = new AggregateRootIdentity(command.AggregateRootId);

            var domainEvent = new Created(Guid.NewGuid(), identity, command.BeginningBalance);

            var state = new AccountState(domainEvent);

            var aggregateRoot = new AccountAggregateRoot(identity, state);

            return(new DomainEventCollection(aggregateRoot, domainEvent));
        }
Пример #12
0
        private SqlCommand CreateSqlCommand(AggregateRootType aggregateRootType, AggregateRootIdentity identity)
        {
            var sqlCommand = new SqlCommand("[dbo].[GetDomainEvents]");

            sqlCommand.CommandType = CommandType.StoredProcedure;

            // set parameters
            sqlCommand.Parameters.Add("@aggregateRootId", SqlDbType.UniqueIdentifier).Value = identity.Value;
            sqlCommand.Parameters.Add("@aggregateRootTypeId", SqlDbType.Binary, 16).Value   = _typeFactory.GetHash(aggregateRootType.Type);

            return(sqlCommand);
        }
Пример #13
0
        public TAggregateRoot GetByIdentity(AggregateRootIdentity identity)
        {
            Contract.Assume(identity != null);

            var eventStream = _eventStore.GetEventStream <TAggregateRoot>(identity);

            // build aggregate root from event stream
            var aggregateRoot = _eventStreamProcessor.Process(eventStream);

            // set external state tracker
            aggregateRoot.State.ExternalStateTracker = new EventProviderStateTracker(eventStream.EventProvider, eventStream.LatestVersion, _guidGenerator);

            return(aggregateRoot);
        }
Пример #14
0
        public GetDomainEventsCommand(SqlSerializer serializer,
                                      ITypeFactory typeFactory,
                                      AggregateRootType aggregateRootType,
                                      AggregateRootIdentity aggregateRootIdentity)
        {
            Contract.Requires(serializer != null);
            Contract.Requires(aggregateRootType != null);
            Contract.Requires(typeFactory != null);
            Contract.Requires(aggregateRootIdentity != null);

            _serializer            = serializer;
            _typeFactory           = typeFactory;
            _aggregateRootType     = aggregateRootType;
            _aggregateRootIdentity = aggregateRootIdentity;

            _command = CreateSqlCommand(aggregateRootType, aggregateRootIdentity);
        }
Пример #15
0
        public IEventStream GetEventStream <TAggregateRoot>(AggregateRootIdentity identity) where TAggregateRoot : class, IAggregateRoot
        {
            try
            {
                // create new event provider type
                var eventProviderType = new AggregateRootType(typeof(TAggregateRoot));

                // get event stream
                var eventStream = GetEventStream(eventProviderType, identity);
                Contract.Assume(eventStream != null);

                // return event stream
                return(eventStream);
            }
            catch (Exception ex)
            {
                throw new EventStoreException(ErrorGettingEventStream, ex);
            }
        }
Пример #16
0
 protected abstract Task <EventStream> GetEventStreamAsync(AggregateRootType eventProviderType, AggregateRootIdentity aggregateRootIdentity);
        protected override async Task <EventStream> GetEventStreamAsync(AggregateRootType eventProviderType, AggregateRootIdentity identity)
        {
            // establish command
            var command = new GetDomainEventsCommand(_serializer, _typeFactory, eventProviderType, identity);

            // create connection
            using (var conn = new SqlConnection(_connectionString))
            {
                // connection needs to be open before executing
                await conn.OpenAsync();

                // execute
                return(await command.ExecuteAsync(conn));
            }
        }
        protected async Task <ICommandHandlingResult> HandleAsync(ICommandHandlerContext <TCommand> context, AggregateRootIdentity identity)
        {
            Contract.Requires(context != null);
            Contract.Requires(identity != null);
            Contract.Ensures(Contract.Result <Task>() != null);

            // use identity to get aggregate root
            TAggregateRoot aggregateRoot = await GetAggregateRootAsync(identity);

            // execute command
            aggregateRoot.Execute(context.Command);

            // commit changes
            return(await _repository.CommitAsync(context, aggregateRoot));
        }
        protected ICommandHandlingResult Handle(ICommandHandlerContext <TCommand> context, AggregateRootIdentity identity)
        {
            Contract.Requires(context != null);
            Contract.Requires(identity != null);
            Contract.Ensures(Contract.Result <ICommandHandlingResult>() != null);

            // use identity to get aggregate root
            TAggregateRoot aggregateRoot = GetAggregateRoot(identity);

            // execute command
            aggregateRoot.Execute(context.Command);

            // commit changes
            return(_repository.Commit(context, aggregateRoot));
        }
Пример #20
0
 protected abstract EventStream GetEventStream(AggregateRootType eventProviderType, AggregateRootIdentity aggregateRootIdentity);
 private AccountAggregateRoot(AggregateRootIdentity identity, AccountState state)
     : base(identity, state)
 {
     Contract.Requires(identity != null);
     Contract.Requires(state != null);
 }