Ejemplo n.º 1
0
        private CreateIndex(IndexKeysDefinition <TDto> definition, CreateIndexOptions options)
        {
            Precondition.For(definition, nameof(definition)).NotNull();

            Definition = definition;
            Options    = options;
        }
Ejemplo n.º 2
0
        protected override async Task <CommandBusResult> EnqueueInternalAsync(ICommand cmd)
        {
            Precondition.For(cmd, nameof(cmd)).IsValidCommand();
            CommandBusResult result = await HandleCommand(cmd);

            return(result);
        }
Ejemplo n.º 3
0
        public async Task InvokeAndSaveAsync(Type domainObjectType, ICommand cmd,
                                             IEnumerable <CommandMethodMapping> commandMapping)
        {
            Precondition.For(domainObjectType, nameof(domainObjectType)).NotNull("Type has to be set!");
            Precondition.For(cmd, nameof(cmd)).IsValidCommand();
            Precondition.For(commandMapping, nameof(commandMapping)).NotNull();

            var type = cmd.GetType();

            logger.LogTrace("Invoking Command \"{type}\" for \"{domainObjectType}\"", domainObjectType);
            var currentTry = 0;

            while (currentTry < retryCount)
            {
                if (currentTry > 0)
                {
                    logger.LogTrace("Retrying Command \"{type}\" for \"{domainObjectType}\"...");
                }
                AppendResult result = await InvokeAndSaveInternalAsync(domainObjectType, cmd, commandMapping);

                if (result.HadWrongVersion)
                {
                    currentTry++;
                }
                else
                {
                    break;
                }
            }
        }
Ejemplo n.º 4
0
        public async Task <AppendResult> SaveAsync <T>(T domainObject, bool preventVersionCheck)
            where T : class, IDomainObject
        {
            Precondition.For(domainObject, nameof(domainObject))
            .NotNull("The domainObject to be saved must not be null!");

            string type = typeof(T).FullName;

            if (!domainObject.HasUncommittedEvents)
            {
                string id = domainObject.Id;
                logger.LogDebug("\"{type}\"-{id} had no events to save", type, id);

                return(AppendResult.NoUpdate);
            }

            int count = domainObject.GetUncommittedEvents().Count;

            logger.LogTrace("Saving \"{type}\" with {count} events...", type, count);
            Stopwatch watch = Stopwatch.StartNew();

            bool         check  = domainObject.CheckVersionOnSave && !preventVersionCheck;
            AppendResult result = await SaveUncomittedEventsAsync(domainObject, check);

            watch.Stop();

            if (string.IsNullOrWhiteSpace(result.CommitId) || result.HadWrongVersion)
            {
                string reason = result.Message;
                logger.LogWarning("{count}  events not saved for \"{type}\" reason; {reason}", count, type,
                                  reason);
            }
            else
            {
                var ms = watch.ElapsedMilliseconds;
                logger.LogTrace("Saved {count} events for \"{type}\" in {ms}ms", count, type,
                                ms);

                //especially for direct denormalizer its important to continue on the state that wasreally  persisted.
                //e.g. in case of persistance errors. otherweise we don't have a real projection of persisted events /auditlog
                List <IEvent> persistedEvents = await ByAppendResult(result);

                if (!result.HadWrongVersion)
                {
                    foreach (IEvent @event in persistedEvents)
                    {
                        configuration.PostSavePipeline?.Invoke(@event);

                        if (denormalizerPipeline != null)
                        {
                            await denormalizerPipeline.HandleAsync(@event);
                        }
                    }
                }

                domainObject.CommitChanges(result.CurrentVersion);
            }

            return(result);
        }
        protected EventSourcedControllerBase(ICommandBus bus, IDomainObjectRepository domainObjectRepository)
        {
            Precondition.For(() => bus).NotNull();

            CommandBus             = bus;
            DomainObjectRepository = domainObjectRepository;
        }
Ejemplo n.º 6
0
        protected UserCommandBase(string domainObjectId, string userId) : base(domainObjectId)
        {
            Precondition.For(domainObjectId, nameof(domainObjectId)).NotNullOrWhiteSpace();
            Precondition.For(userId, nameof(userId)).NotNullOrWhiteSpace();

            UserId = userId;
        }
Ejemplo n.º 7
0
        public void WithCondition(Predicate <ICommand> condition)
        {
            Precondition.For(condition, nameof(condition))
            .NotNull("When a condition has to be set it should not be null");

            this.condition = condition;
        }
Ejemplo n.º 8
0
        public InMemoryCommandBus(ICommandPipeline handler)
        {
            Precondition.For(() => handler).NotNull();
            this.handler = handler;

            subject.Subscribe(async msg => await HandleCommand(msg));
        }
Ejemplo n.º 9
0
 public EventMapper(IEventSerializer serializer, IEventHash hash)
 {
     Precondition.For(hash, nameof(hash)).NotNull();
     Precondition.For(serializer, nameof(serializer)).NotNull();
     this.serializer = serializer;
     this.eventHash  = hash;
 }
        public MongoXmlRepository(IMongoDatabase db, string collectionName)
        {
            Precondition.For(() => db).NotNull();
            Precondition.For(() => collectionName).NotNullOrWhiteSpace();

            Collection = db.GetCollection <MongoStoredKey>(collectionName);
        }
Ejemplo n.º 11
0
        public MongoEventSubscriber(IMongoDatabase db, ILoggerFactory loggerFactory)
        {
            Precondition.For(db, nameof(db)).NotNull();
            Precondition.For(loggerFactory, nameof(loggerFactory)).NotNull();

            logger = loggerFactory.CreateLogger <MongoEventSubscriber>();
            repo   = new MongoCommitRepository(db);
        }
Ejemplo n.º 12
0
        public static DenormalizerConfiguration SetMongoDbEventSubscriber(this DenormalizerConfiguration config, IMongoDatabase db)
        {
            Precondition.For(() => config).NotNull();
            Precondition.For(() => db).NotNull();

            config.Subscriber = new MongoEventSubscriber(db);
            return(config);
        }
Ejemplo n.º 13
0
        public static DenormalizerConfiguration SetMongoEventPositionGateway(this DenormalizerConfiguration config, IMongoDatabase db)
        {
            Precondition.For(() => config).NotNull();
            Precondition.For(() => db).NotNull();

            config.StreamPositionGateway = new MongoStreamPositionGateway(db, null);
            return(config);
        }
Ejemplo n.º 14
0
        public ConventionCommandInvoker(IDomainObjectRepository repository, ILoggerFactory loggerFactory)
        {
            Precondition.For(() => repository).NotNull("Repository has to be set!");
            Precondition.For(() => loggerFactory).NotNull("loggerFactory has to be set!");

            logger          = loggerFactory.CreateLogger <ConventionCommandInvoker>();
            this.repository = repository;
        }
Ejemplo n.º 15
0
        public void ItSucceedsWhenRequirementsAreFullfilled()
        {
            var model = new Model {
                Foo = "aaa"
            };

            Precondition.For(model, nameof(model)).IsValidModel();
        }
Ejemplo n.º 16
0
        public void ApplyEvents(ICollection <IEvent> eventsToCommit)
        {
            Precondition.For(eventsToCommit, nameof(eventsToCommit)).NotNull().True(i => i.Count > 0);

            ApplyCommitId(eventsToCommit);

            committedEvents.AddRange(eventsToCommit);
        }
Ejemplo n.º 17
0
        public static DenormalizerConfiguration SetServiceProviderDenormalizerActivator(this DenormalizerConfiguration config)
        {
            Precondition.For(() => config).NotNull();

            config.Activator = new ServiceCollectionActivator();

            return(config);
        }
Ejemplo n.º 18
0
        public EventsourceDIContext(IDomainObjectActivator domainObjectActivator, IStateActivator stateActivator)
        {
            Precondition.For(domainObjectActivator, nameof(domainObjectActivator)).NotNull();
            Precondition.For(stateActivator, nameof(stateActivator)).NotNull();

            DomainObjectActivator = domainObjectActivator;
            StateActivator        = stateActivator;
        }
Ejemplo n.º 19
0
        public string Resolve(Type domainObject, string id, string @namespace = null)
        {
            Precondition.For(id, nameof(id)).NotNullOrWhiteSpace();

            string result = BuildKey(domainObject, id, @namespace);

            return(result);
        }
Ejemplo n.º 20
0
        protected MongoRepositoryBase(IMongoDatabase database, string collectionName)
        {
            Precondition.For(database, nameof(database)).NotNull();
            Precondition.For(collectionName, nameof(collectionName)).NotNullOrWhiteSpace().MinLength(3);

            Database   = database;
            Collection = database.GetCollection <TDto>(collectionName);
        }
        protected DomainObjectRepositoryBase(EventSourceConfiguration configuration)
        {
            Precondition.For(configuration, nameof(configuration))
            .NotNull("Configuration for domainobject repository must not be null!");

            this.configuration = configuration;
            logger             = configuration.LoggerFactory.CreateLogger(GetType());
        }
Ejemplo n.º 22
0
        public InMemoryCommandBus(ICommandPipeline handler, ILoggerFactory loggerFactory) : base(loggerFactory)
        {
            Precondition.For(() => handler).NotNull();
            logger       = loggerFactory.CreateLogger <InMemoryCommandBus>();
            this.handler = handler;

            subject.Subscribe(async msg => await HandleCommand(msg));
        }
Ejemplo n.º 23
0
        public static EventSourceConfiguration SetDefaultActivator(this EventSourceConfiguration config)
        {
            Precondition.For(() => config).NotNull();
            var activator = new ActivatorDomainObjectActivator();

            config.Activator      = activator;
            config.StateActivator = activator;
            return(config);
        }
Ejemplo n.º 24
0
        public static EventSourceConfiguration SetDomainObjectAssemblies(this EventSourceConfiguration config, params Assembly[] assembliesWithDomainObjects)
        {
            Precondition.For(() => config).NotNull();
            Precondition.For(() => assembliesWithDomainObjects).NotNull().True(x => x.Any());

            config.DomainObjectAssemblies = assembliesWithDomainObjects;

            return(config);
        }
Ejemplo n.º 25
0
        public string Resolve <T>(T domainObject) where T : class, IDomainObject
        {
            Precondition.For(domainObject, nameof(domainObject)).NotNull();
            Precondition.For(domainObject.Id, nameof(domainObject.Id)).NotNullOrWhiteSpace();

            string result = BuildKey(domainObject.GetType(), domainObject.Id, domainObject.Namespace);

            return(result);
        }
Ejemplo n.º 26
0
        public static DenormalizerConfiguration SetDenormalizerAssemblies(this DenormalizerConfiguration config, params Assembly[] assemblies)
        {
            Precondition.For(() => config).NotNull();
            Precondition.For(() => assemblies).NotNull().True(x => x.Any());

            config.DenormalizerAssemblies = assemblies;

            return(config);
        }
Ejemplo n.º 27
0
        public static IServiceCollection AddMongoEventPositionGateway(this IServiceCollection services,
                                                                      IMongoDatabase db)
        {
            Precondition.For(() => db).NotNull();

            services.AddSingleton <IStreamPositionGateway, MongoStreamPositionGateway>();

            return(services);
        }
Ejemplo n.º 28
0
        protected Task <string> CreateIndexAsync(CreateIndex <TDto> index)
        {
            Precondition.For(index, nameof(index)).NotNull();

            Task <string> task = index.Options == null
                ? Collection.Indexes.CreateOneAsync(index.Definition)
                : Collection.Indexes.CreateOneAsync(index.Definition, index.Options);

            return(task);
        }
        public IDomainObject Resolve(Type domainObjectType, string id)
        {
            Precondition.For(domainObjectType, nameof(domainObjectType))
            .NotNull()
            .True(i => DomainObjectInfo.IsAssignableFrom(i.GetTypeInfo()));

            Precondition.For(id, nameof(id)).NotNullOrWhiteSpace();

            return((IDomainObject)Activator.CreateInstance(domainObjectType, id));
        }
Ejemplo n.º 30
0
        public IMongoCollection <EventCommit> GetCollectionByAggregateType(string streamName)
        {
            Precondition.For(streamName, nameof(streamName)).NotNullOrWhiteSpace();

            string name = GetCollectionNameByCommit(streamName);

            IMongoCollection <EventCommit> collection = cache.GetOrAdd(name, x => ResolveNew(name).Result);

            return(collection);
        }