示例#1
0
        protected void ProjectToTable(
            IRepository <TOriginal> originalRepository,
            IRepository <TArchive> archiveRepository,
            IDbConnectionContextFactory connectionCtxFactory,
            ICPRContext ctx,
            T cmd,
            ILoggerFactory loggerFactory,
            IConfiguration config)
        {
            var id   = s_srcKey(cmd);
            var orig = originalRepository.Get(id);
            var arch = Activator.CreateInstance <TArchive>();

            if (orig != null)
            {
                s_mapper.Map(orig, arch);

                arch.LUID            = Guid.NewGuid();
                arch.DeletedByUserID = ctx.UserID;
                arch.DeletedOn       = DateTime.UtcNow;

                archiveRepository.Insert(arch);
                originalRepository.Delete(orig);
            }
        }
示例#2
0
        public virtual void Project(ICPRContext ctx, T cmd)
        {
            if (m_destinationRepository == null)
            {
                throw new Exception("You must pass a repository into the constructor to use ICPRDatabaseProject.Project(ICPRContext ctx, T cmd).");
            }

            ProjectToTable(m_destinationRepository, null, ctx, cmd, null, null);
        }
示例#3
0
        public virtual void Project(ICPRContext ctx, T cmd)
        {
            if (m_originalRepository == null || m_archiveRepository == null)
            {
                throw new Exception("You must pass repositories into the constructor to use ICPRDatabaseProject.Project(ICPRContext ctx, T cmd).");
            }

            ProjectToTable(m_originalRepository, m_archiveRepository, null, ctx, cmd, null, null);
        }
示例#4
0
        protected virtual void ExecuteValidations <T>(ICPRContext currentCtx, CPRExecutionResult result, T cmd)
            where T : CPRCommand <T>
        {
            var validators = cmd.Validators.ToList();

            result.AddErrors(validators.SelectMany(v => v.Validate(currentCtx, cmd)));
            if (cmd is ICPRValidator <T> )
            {
                result.AddErrors(((ICPRValidator <T>)cmd).Validate(currentCtx, cmd));
            }
        }
示例#5
0
        protected override void ExecuteValidations <T>(ICPRContext currentCtx, CPRExecutionResult result, T cmd)
        {
            base.ExecuteValidations(currentCtx, result, cmd);

            result.AddErrors(cmd.Validators.OfType <ICPRDatabaseValidator <T> >().SelectMany(v => v.Validate(
                                                                                                 m_dbContextFactory,
                                                                                                 currentCtx,
                                                                                                 cmd,
                                                                                                 m_loggerFactory,
                                                                                                 m_config)));
        }
示例#6
0
        protected override void ExecuteProjections <T>(ICPRContext currentCtx, T cmd)
        {
            var executedProjections = new List <ICPRProjection <T> >();

            try
            {
                var projections = cmd.Projections.ToList();

                if (projections.Any(x => !(x is ICPRProjection <T> || x is ICPRDatabaseProjection <T>)))
                {
                    throw new Exception("Projections cannot derive from ICPRProjectionBase directly. This is just a marker interface. Use ICPRProjection<T> instead.");
                }

                var projectionCmd   = cmd as ICPRProjection <T>;
                var dbProjectionCmd = cmd as ICPRDatabaseProjection <T>;

                // If the class has specifically added this to their projections we respect their order
                if (projectionCmd != null && !projections.OfType <ICPRProjection <T> >().Contains(projectionCmd))
                {
                    projectionCmd.Project(currentCtx, cmd);
                }
                if (dbProjectionCmd != null && !projections.OfType <ICPRDatabaseProjection <T> >().Contains(dbProjectionCmd))
                {
                    dbProjectionCmd.Project(m_dbContextFactory, currentCtx, cmd, m_loggerFactory, m_config);
                }
                foreach (var projection in projections.OfType <ICPRDatabaseProjection <T> >())
                {
                    projection.Project(m_dbContextFactory, currentCtx, cmd, m_loggerFactory, m_config);
                }
                foreach (var projection in projections.OfType <ICPRProjection <T> >())
                {
                    executedProjections.Add(projection);
                    projection.Project(currentCtx, cmd);
                }
            }
            catch (Exception ex)
            {
                executedProjections.Reverse();
                foreach (var projection in executedProjections)
                {
                    // for others that do not participate in transaction we unroll
                    projection.Unproject(currentCtx, cmd);
                }
                throw;
            }
        }
示例#7
0
        protected virtual void ExecuteProjections <T>(ICPRContext currentCtx, T cmd)
            where T : CPRCommand <T>
        {
            var executedProjections = new List <ICPRProjection <T> >();
            var projections         = cmd.Projections.ToList();

            try
            {
                if (projections.Any(x => !(x is ICPRProjection <T>)))
                {
                    throw new Exception("Projections cannot derive from ICPRProjectionBase directly. This is just a marker interface. Use ICPRProjection<T> instead.");
                }

                var projectionCmd = cmd as ICPRProjection <T>;

                // If the class has specifically added this to their projections we respect their order
                if (projectionCmd != null && !projections.OfType <ICPRProjection <T> >().Contains(projectionCmd))
                {
                    projectionCmd.Project(currentCtx, cmd);
                }
                if (cmd is ICPRProjection <T> )
                {
                    ((ICPRProjection <T>)cmd).Project(currentCtx, cmd);
                }
                foreach (var projection in projections.OfType <ICPRProjection <T> >())
                {
                    executedProjections.Add(projection);

                    projection.Project(currentCtx, cmd);
                }
            }
            catch (Exception ex)
            {
                executedProjections.Reverse();
                foreach (var projection in executedProjections)
                {
                    projection.Unproject(currentCtx, cmd);
                }
                throw;
            }
        }
示例#8
0
 public IEnumerable <CPRSecurityError> Authenticate(ICPRContext ctx, OverridenDataTestCommand cmd)
 {
     yield break;
 }
示例#9
0
 protected override void Intercept(IDbConnectionContextFactory connectionCtxFactory, ICPRContext ctx, bool created, DataTable dest, ILoggerFactory loggerFactory, IConfiguration config)
 {
     InterceptCalled();
 }
示例#10
0
        public virtual void Project(IDbConnectionContextFactory connectionCtxFactory, ICPRContext ctx, T cmd, ILoggerFactory loggerFactory, IConfiguration config)
        {
            var rep = new Repository <TDest>(connectionCtxFactory, loggerFactory, config);

            ProjectToTable(rep, connectionCtxFactory, ctx, cmd, loggerFactory, config);
        }
示例#11
0
 public virtual void Unproject(ICPRContext ctx, T cmd)
 {
     // NOP
 }
示例#12
0
        protected virtual void Intercept(IDbConnectionContextFactory connectionCtxFactory, ICPRContext ctx, bool created, TDest dest, ILoggerFactory loggerFactory, IConfiguration config)
        {
            if (dest is ICPRTable)
            {
                var table = (ICPRTable)dest;

                if (created)
                {
                    table.CreatedByUserID = ctx.UserID;
                    table.CreatedOn       = DateTime.UtcNow;
                }

                table.ModifiedByUserID = ctx.UserID;
                table.ModifiedOn       = DateTime.UtcNow;
            }
        }
示例#13
0
        protected virtual void ProjectToTable(IRepository <TDest> repository, IDbConnectionContextFactory connectionCtxFactory, ICPRContext ctx, T cmd, ILoggerFactory loggerFactory, IConfiguration config)
        {
            var id   = s_srcKey(cmd);
            var dest = repository.Get(id);

            var newObject = (dest == null);

            if (newObject)
            {
                dest = Activator.CreateInstance <TDest>();
            }

            s_mapper.Map(cmd, dest);

            Intercept(connectionCtxFactory, ctx, newObject, dest, loggerFactory, config);

            if (newObject)
            {
                repository.Insert(dest);
            }
            else
            {
                repository.Update(dest);
            }
        }
示例#14
0
        public virtual void Project(IDbConnectionContextFactory connectionCtxFactory, ICPRContext ctx, T cmd, ILoggerFactory loggerFactory, IConfiguration config)
        {
            if (m_originalRepository != null)
            {
                throw new Exception("You must either pass in both repositories to use ICPRDatabaseProject.Project with an IDbConnectionContextFactory.");
            }
            if (m_archiveRepository != null)
            {
                throw new Exception("You must either pass in both repositories to use ICPRDatabaseProject.Project with an IDbConnectionContextFactory.");
            }

            var originalRepository = new Repository <TOriginal>(connectionCtxFactory, loggerFactory, config);
            var archiveRepository  = new Repository <TArchive>(connectionCtxFactory, loggerFactory, config);

            ProjectToTable(originalRepository, archiveRepository, connectionCtxFactory, ctx, cmd, loggerFactory, config);
        }
示例#15
0
 public virtual void Unproject(ICPRContext ctx, T cmd)
 {
     // NOP, can only work in transactions!
 }