Exemple #1
0
        public Task CommitAsync(EventProviderTransaction transaction)
        {
            Contract.Requires(transaction != null);
            Contract.Ensures(Contract.Result <Task>() != null);

            throw new NotImplementedException();
        }
Exemple #2
0
        public void Commit(EventProviderTransaction transaction)
        {
            Contract.Requires(transaction != null);

            // static checker fails when using ForAll
            //Contract.Ensures(Contract.ForAll(transaction.Revisions, o => o.Committed));
            //Contract.EnsuresOnThrow<Exception>(Contract.ForAll(transaction.Revisions, o => o.Committed == false));
        }
        private static IEnumerable <IProjectionContext> CreateProjectionContext(EventProviderTransaction eventProviderTransaction)
        {
            Contract.Requires(eventProviderTransaction != null);
            Contract.Ensures(Contract.Result <IEnumerable <IProjectionContext> >() != null);

            var contexts = new Collection <IProjectionContext>();

            // go through each revision in the transaction
            foreach (var revision in eventProviderTransaction.Revisions.OrderBy(x => x.Version))
            {
                Contract.Assume(revision != null);

                // check for domain event revision
                var domainEventRevision = revision as DomainEventRevision;

                if (domainEventRevision == null)
                {
                    // must be a snapshot revision
                    var snapshotRevision = revision as SnapshotRevision;
                    Contract.Assume(snapshotRevision != null);

                    // convert snapshot revision to snapshot projection context
                    contexts.Add(new ProjectionContext(eventProviderTransaction.EventProvider,
                                                       eventProviderTransaction.Identity,
                                                       eventProviderTransaction.Command,
                                                       eventProviderTransaction.Metadata,
                                                       revision.Version,
                                                       snapshotRevision.Snapshot));
                }
                else
                {
                    // revision is domain event revision, go through each domain event and create a projection context
                    foreach (var domainEvent in domainEventRevision.DomainEvents)
                    {
                        Contract.Assume(domainEvent != null);

                        contexts.Add(new ProjectionContext(eventProviderTransaction.EventProvider,
                                                           eventProviderTransaction.Identity,
                                                           eventProviderTransaction.Command,
                                                           eventProviderTransaction.Metadata,
                                                           revision.Version,
                                                           domainEvent));
                    }
                }
            }

            return(contexts);
        }
        public void Dispatch(EventProviderTransaction eventProviderTransaction)
        {
            // find projection managers from aggregate root type
            var managers = _projectionManagerFactory.GetManagers(eventProviderTransaction.EventProvider.AggregateRootType);

            // check if any managers exist
            if (managers.Any())
            {
                // create projection contexts grouped by event provider and ordered by revision
                var eventProviderContexts = CreateProjectionContext(eventProviderTransaction).ToArray();

                SendContextsToProjectionManagers(eventProviderTransaction.EventProvider, managers, eventProviderContexts);
            }

            // save transaction identity to know its been processed
            _processedTransactions.Add(eventProviderTransaction.Identity);
        }
        protected override async Task CommitTransactionAsync(EventProviderTransaction transaction)
        {
            // establish command
            var command = new CommitTransactionCommand(_serializer,
                                                       _typeFactory,
                                                       transaction,
                                                       transaction.Revisions);

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

                // execute
                await command.ExecuteAsync(conn);
            }
        }
Exemple #6
0
        private ICollection <EventProviderTransactionCollection> GetResult()
        {
            var transactionCollectionGroups = _revisions.GroupBy(x => new { x.EventProviderId, x.AggregateRootId }).ToArray();

            var transactionCollections = new EventProviderTransactionCollection[transactionCollectionGroups.Length];

            // loop through each event provider creating transactions
            for (var eventProviderGroupIndex = 0; eventProviderGroupIndex < transactionCollections.Length; eventProviderGroupIndex++)
            {
                // get current event provider group
                var eventProviderGroup = transactionCollectionGroups[eventProviderGroupIndex];

                // create transaction array for event provider
                var transactions = new EventProviderTransaction[eventProviderGroup.Select(x => x.TransactionId).Distinct().Count()];

                // filter our event provider's domain events
                var eventProviderEvents = _sqlDomainEvents
                                          .Where(x => x.EventProviderId == eventProviderGroup.Key.EventProviderId)
                                          .ToArray();

                // order revisions by version then group by transaction
                var eventProviderTransactions = eventProviderGroup
                                                .OrderBy(x => x.EventProviderVersion)
                                                .GroupBy(x => x.TransactionId)
                                                .ToArray();

                // create event provider
                var eventProvider = new EventProvider(eventProviderGroup.Key.EventProviderId, _aggregateRootType, eventProviderGroup.Key.AggregateRootId);

                // add new event provider transaction collection to result
                transactionCollections[eventProviderGroupIndex] = new EventProviderTransactionCollection(eventProvider, transactions);

                // go through each transaction adding revisions and domain events
                for (var eventProviderTransactionIterator = 0; eventProviderTransactionIterator < eventProviderTransactions.Length; eventProviderTransactionIterator++)
                {
                    transactions[eventProviderTransactionIterator] = CreateEventProviderTransaction(eventProvider,
                                                                                                    eventProviderEvents,
                                                                                                    eventProviderTransactions[eventProviderTransactionIterator].ToArray());
                }
            }

            return(transactionCollections);
        }
        public CommitTransactionCommand(SqlSerializer serializer,
                                        ITypeFactory typeFactory,
                                        EventProviderTransaction transaction,
                                        IReadOnlyCollection <EventStreamRevision> uncommittedRevisions)
        {
            Contract.Requires(serializer != null);
            Contract.Requires(typeFactory != null);
            Contract.Requires(transaction != null);
            Contract.Requires(uncommittedRevisions != null);

            _serializer  = serializer;
            _typeFactory = typeFactory;

            _result = new SqlParameter("result", SqlDbType.Int)
            {
                Direction = ParameterDirection.ReturnValue
            };

            _command = CreateSqlCommand(transaction, uncommittedRevisions);
        }
        private SqlCommand CreateSqlCommand(EventProviderTransaction transaction, IReadOnlyCollection <EventStreamRevision> uncommittedRevisions)
        {
            var sqlCommand = new SqlCommand("[dbo].[CreateTransaction]");

            sqlCommand.CommandType = CommandType.StoredProcedure;

            var command       = transaction.Command;
            var eventProvider = transaction.EventProvider;
            var commandType   = command.GetType();

            // set return parameter
            sqlCommand.Parameters.Add(_result);

            // set parameters
            sqlCommand.Parameters.Add("@transactionId", SqlDbType.UniqueIdentifier).Value = transaction.Identity.Value;
            sqlCommand.Parameters.Add("@metadata", SqlDbType.VarBinary).Value             = _serializer.SerializeObject(transaction.Metadata);

            sqlCommand.Parameters.Add("@commandId", SqlDbType.UniqueIdentifier).Value       = command.CommandId;
            sqlCommand.Parameters.Add("@commandTypeId", SqlDbType.Binary, 16).Value         = _typeFactory.GetHash(commandType);
            sqlCommand.Parameters.Add("@commandTypeFullName", SqlDbType.VarChar, 512).Value = commandType.FullName;
            sqlCommand.Parameters.Add("@commandData", SqlDbType.VarBinary).Value            = _serializer.SerializeObject(command);

            sqlCommand.Parameters.Add("@eventProviderId", SqlDbType.UniqueIdentifier).Value       = eventProvider.Identity.Value;
            sqlCommand.Parameters.Add("@aggregateRootId", SqlDbType.UniqueIdentifier).Value       = eventProvider.AggregateRootIdentity.Value;
            sqlCommand.Parameters.Add("@aggregateRootTypeId", SqlDbType.Binary, 16).Value         = _typeFactory.GetHash(eventProvider.AggregateRootType.Type);
            sqlCommand.Parameters.Add("@aggregateRootTypeFullName", SqlDbType.VarChar, 512).Value = eventProvider.AggregateRootType.Type.FullName;
            sqlCommand.Parameters.Add("@eventProviderDescriptor", SqlDbType.VarChar).Value        = transaction.Descriptor.Value;

            // event user defined table type
            sqlCommand.Parameters.Add(new SqlParameter("@events", SqlDbType.Structured)
            {
                TypeName = "[dbo].[udttEvent]",
                Value    = GetEventDataTable(uncommittedRevisions)
            });

            return(sqlCommand);
        }
        public void Dispatch(EventProviderTransaction eventProviderTransaction)
        {
            Contract.Requires(eventProviderTransaction != null);

            throw new NotImplementedException();
        }
Exemple #10
0
 protected override void TransactionCommitted(EventProviderTransaction transaction)
 {
     _dispatcher.Dispatch(transaction);
 }
        public void Dispatch(EventProviderTransaction eventProviderTransaction)
        {
            Contract.Assume(Queue != null);

            Queue.Add(new QueueItem <EventProviderTransaction>(eventProviderTransaction));
        }
 public abstract void Project(EventProviderTransaction transaction);
        protected override void TransactionCommitted(EventProviderTransaction transaction)
        {
            Contract.Requires(transaction != null);

            throw new NotImplementedException();
        }