Пример #1
0
        ProjectionStream LoadProjectionStream(Type projectionType, IBlobId projectionId)
        {
            string projectionName = projectionType.GetContractId();

            ReadResult <ProjectionVersions> result = GetProjectionVersions(projectionName);

            if (result.IsSuccess)
            {
                ProjectionVersion liveVersion = result.Data.GetLive();
                if (liveVersion is null)
                {
                    log.Warn(() => $"Unable to find projection `live` version. ProjectionId:{projectionId} ProjectionName:{projectionName} ProjectionType:{projectionType.Name}");
                    return(ProjectionStream.Empty());
                }

                ISnapshot snapshot = null;
                if (projectionType.IsSnapshotable())
                {
                    snapshot = snapshotStore.Load(projectionName, projectionId, liveVersion);
                }
                else
                {
                    snapshot = new NoSnapshot(projectionId, projectionName);
                }

                return(LoadProjectionStream(projectionType, liveVersion, projectionId, snapshot));
            }

            return(ProjectionStream.Empty());
        }
        public void HandleCommand(CreateProjectCommand command)
        {
            if (string.IsNullOrEmpty(command.ProjectName))
            {
                throw new Exception("Project is now alloed to have empty name");
            }
            if (command.UserId == Guid.Empty)
            {
                throw new Exception("User is not allowed to have empty Key");
            }

            var userKey          = new SimpleKey(command.UserId.ToString());
            var userBeforeUpdate = _snapshotStore.Load <User>(userKey);
            var user             = _snapshotStore.Load <User>(userKey);

            if (user.ProjectCount >= 5)
            {
                throw new Exception("User is not allowed to have more than 5 projects");
            }

            var projectKey = new SimpleKey(command.ProjectId.ToString());

            try {
                var project = _snapshotStore.Create(projectKey, new Project(command.ProjectName, command.UserId));
                user.AddProject();
            }
            catch (Exception) {
                _snapshotStore.Update(userKey, userBeforeUpdate);
                _snapshotStore.Delete <Project>(projectKey);

                throw new Exception($"Failed to apply {nameof(CreateProjectCommand)}");
            }
        }
Пример #3
0
        public IProjectionGetResult <T> Get <T>(IBlobId projectionId) where T : IProjectionDefinition
        {
            if (ReferenceEquals(null, projectionId))
            {
                throw new ArgumentNullException(nameof(projectionId));
            }

            Type   projectionType = typeof(T);
            string contractId     = projectionType.GetContractId();

            try
            {
                ProjectionVersion liveVersion = GetProjectionVersions(contractId).GetLive();
                if (ReferenceEquals(null, liveVersion))
                {
                    log.Warn(() => $"Unable to find `live` version for projection with contract id {contractId} and name {projectionType.Name}");
                    return(new ProjectionGetResult <T>(default(T)));
                }

                ISnapshot                snapshot    = snapshotStore.Load(contractId, projectionId, liveVersion);
                ProjectionStream         stream      = LoadProjectionStream(projectionType, liveVersion, projectionId, snapshot);
                IProjectionGetResult <T> queryResult = stream.RestoreFromHistory <T>();

                return(queryResult);
            }
            catch (Exception ex)
            {
                log.ErrorException($"Unable to load projection. ProjectionId:{projectionId} ProjectionContractId:{contractId} ProjectionName:{projectionType.Name}", ex);
                return(new ProjectionGetResult <T>(default(T)));
            }
        }
Пример #4
0
        public TAggregateRoot RestoreAggregate <TAggregateRoot>(IIdentity aggregateRootId) where TAggregateRoot : IAggregateRoot
        {
            var snapshot = _snapshotStore.Load(aggregateRootId.ToString());

            if (snapshot != null)
            {
                return(_serializeManager.DeserializeAggregate <TAggregateRoot>(snapshot.Payload));
            }
            return(default(TAggregateRoot));
        }
Пример #5
0
        public IProjectionGetResult <T> Get <T>(IBlobId projectionId) where T : IProjectionDefinition
        {
            try
            {
                Type   projectionType = typeof(T);
                string contractId     = projectionType.GetContractId();

                ProjectionVersion liveVersion = GetProjectionVersions(contractId).GetLive();

                ISnapshot                snapshot    = snapshotStore.Load(contractId, projectionId, liveVersion);
                ProjectionStream         stream      = LoadProjectionStream(projectionType, liveVersion, projectionId, snapshot);
                IProjectionGetResult <T> queryResult = stream.RestoreFromHistory <T>();

                return(queryResult);
            }
            catch (Exception ex)
            {
                log.ErrorException(ex.Message, ex);
                return(new ProjectionGetResult <T>(default(T)));
            }
        }
Пример #6
0
        ProjectionStream LoadProjectionStream(Type projectionType, IBlobId projectionId)
        {
            string projectionName = projectionType.GetContractId();

            ReadResult <ProjectionVersions> result = GetProjectionVersions(projectionName);

            if (result.IsSuccess)
            {
                ProjectionVersion liveVersion = result.Data.GetLive();
                if (liveVersion is null)
                {
                    log.Warn(() => $"Unable to find projection `live` version. ProjectionId:{projectionId} ProjectionName:{projectionName} ProjectionType:{projectionType.Name}{Environment.NewLine}AvailableVersions:{Environment.NewLine}{result.Data.ToString()}");
                    return(ProjectionStream.Empty());
                }

                ISnapshot snapshot = projectionType.IsSnapshotable()
                    ? snapshotStore.Load(projectionName, projectionId, liveVersion)
                    : new NoSnapshot(projectionId, projectionName);

                return(LoadProjectionStream(projectionType, liveVersion, projectionId, SnapshotMeta.From(snapshot), () => snapshot));
            }

            return(ProjectionStream.Empty());
        }
Пример #7
0
        ProjectionStream LoadProjectionStream(Type projectionType, IBlobId projectionId)
        {
            string projectionName = projectionType.GetContractId();

            try
            {
                ProjectionVersion liveVersion = GetProjectionVersions(projectionName).GetLive();
                if (ReferenceEquals(null, liveVersion))
                {
                    log.Warn(() => $"Unable to find `live` version for projection with contract id {projectionName} and name {projectionType.Name}");
                    return(ProjectionStream.Empty());
                }

                ISnapshot        snapshot = snapshotStore.Load(projectionName, projectionId, liveVersion);
                ProjectionStream stream   = LoadProjectionStream(projectionType, liveVersion, projectionId, snapshot);

                return(stream);
            }
            catch (Exception ex)
            {
                log.ErrorException($"Unable to load projection stream. ProjectionId:{projectionId} ProjectionContractId:{projectionName} ProjectionName:{projectionType.Name}", ex);
                return(ProjectionStream.Empty());
            }
        }
Пример #8
0
        public User HandleQuery(GetByIdQuery query)
        {
            var userKey = new SimpleKey(query.Id.ToString());

            return(_snapshotStore.Load <User>(userKey));
        }
Пример #9
0
        public Project HandleQuery(GetByIdQuery query)
        {
            var projectKey = new SimpleKey(query.Id.ToString());

            return(_snapshotStore.Load <Project>(projectKey));
        }