/// <summary>
        /// Gets the instance of the aggregate root with the specified identifier.
        /// </summary>
        /// <param name="id">The identifier of the aggregate root.</param>
        /// <returns>The instance of the aggregate root with the specified identifier.</returns>
        public override TAggregateRoot Get <TAggregateRoot>(Guid id)
        {
            TAggregateRoot aggregateRoot = this.CreateAggregateRootInstance <TAggregateRoot>();

            if (this.snapshotProvider != null && this.snapshotProvider.HasSnapshot(typeof(TAggregateRoot), id))
            {
                ISnapshot snapshot = snapshotProvider.GetSnapshot(typeof(TAggregateRoot), id);
                aggregateRoot.BuildFromSnapshot(snapshot);
                var eventsAfterSnapshot = this.domainEventStorage.LoadEvents(typeof(TAggregateRoot), id, snapshot.Version);
                if (eventsAfterSnapshot != null && eventsAfterSnapshot.Count() > 0)
                {
                    aggregateRoot.BuildFromHistory(eventsAfterSnapshot);
                }
            }
            else
            {
                aggregateRoot.ID = id;
                var evnts = this.domainEventStorage.LoadEvents(typeof(TAggregateRoot), id);
                if (evnts != null && evnts.Count() > 0)
                {
                    aggregateRoot.BuildFromHistory(evnts);
                }
                else
                {
                    throw new RepositoryException("The aggregate (id={0}) cannot be found in the domain repository.", id);
                }
            }
            return(aggregateRoot);
        }
        /// <summary>
        /// Gets the instance of the aggregate root with the specified identifier.
        /// </summary>
        /// <param name="id">The identifier of the aggregate root.</param>
        /// <returns>The instance of the aggregate root with the specified identifier.</returns>
        public override TAggregateRoot Get <TAggregateRoot>(Guid id)
        {
            var aggregateRoot = this.CreateAggregateRootInstance <TAggregateRoot>();

            if (this._snapshotProvider != null && this._snapshotProvider.HasSnapshot(typeof(TAggregateRoot), id))
            {
                var snapshot = _snapshotProvider.GetSnapshot(typeof(TAggregateRoot), id);
                aggregateRoot.BuildFromSnapshot(snapshot);
                var eventsAfterSnapshot = this._domainEventStorage.LoadEvents(typeof(TAggregateRoot), id, snapshot.Version);
                var historicalEvents    = eventsAfterSnapshot as IDomainEvent[] ?? eventsAfterSnapshot.ToArray();
                if (eventsAfterSnapshot != null && historicalEvents.Any())
                {
                    aggregateRoot.BuildFromHistory(historicalEvents);
                }
            }
            else
            {
                aggregateRoot.Id = id;
                var evnts            = this._domainEventStorage.LoadEvents(typeof(TAggregateRoot), id);
                var historicalEvents = evnts as IDomainEvent[] ?? evnts.ToArray();
                if (evnts != null && historicalEvents.Any())
                {
                    aggregateRoot.BuildFromHistory(historicalEvents);
                }
                else
                {
                    throw new RepositoryException("The aggregate (id={0}) cannot be found in the domain repository.", id);
                }
            }
            return(aggregateRoot);
        }
Example #3
0
        public Host(ILogger <Host> logger, ITimer timer,
                    ISnapshotProvider snapshotProvider,
                    IFileHandler fileHandler, IOptions <Settings> settings)
        {
            this.logger      = logger;
            this.timer       = timer;
            this.fileHandler = fileHandler;
            this.settings    = settings;

            timer.OnElapsed(async() =>
            {
                using (var stream = await snapshotProvider.GetSnapshot())
                {
                    fileHandler.SaveStream(stream);
                }
            });
        }