示例#1
0
        public void SetUpdated(IUpdateAuditable entity, IExecutionContext executionContext)
        {
            const string msg = "Cannot set an entity as updated if it has not yet been created. Property not set: ";

            if (entity == null)
            {
                throw new ArgumentNullException(nameof(entity));
            }
            if (executionContext == null)
            {
                throw new ArgumentNullException(nameof(executionContext));
            }
            if (executionContext.UserContext.UserId == null)
            {
                throw new NotPermittedException("User must be logged in to update an ICreateAuditable entity");
            }

            if (entity.CreateDate == DateTime.MinValue)
            {
                throw new InvalidOperationException(msg + nameof(entity.CreateDate));
            }

            if (entity.CreatorId < 1)
            {
                throw new InvalidOperationException(msg + nameof(entity.CreatorId));
            }

            entity.UpdateDate = executionContext.ExecutionDate;
            entity.UpdaterId  = executionContext.UserContext.UserId.Value;
        }
示例#2
0
        public void SetUpdated(IUpdateAuditable entity, IExecutionContext executionContext)
        {
            Condition.Requires(entity).IsNotNull();
            Condition.Requires(entity.CreateDate).IsGreaterThan(DateTime.MinValue);
            Condition.Requires(entity.CreatorId).IsGreaterThan(0);
            Condition.Requires(executionContext).IsNotNull();
            Condition.Requires(executionContext.UserContext.UserId).IsNotNull("User must be logged in to update an ICreateAuditable entity");

            entity.UpdateDate = executionContext.ExecutionDate;
            entity.UpdaterId  = executionContext.UserContext.UserId.Value;
        }
示例#3
0
        /// <summary>
        /// Maps an EF model that inherits from IUpdateAuditable into a UpdateAuditData object. If the
        /// db record is null then an ArgumentNullException is thrown.
        /// </summary>
        /// <param name="model">IUpdateAuditable EF database record.</param>
        public UpdateAuditData MapUpdateAuditData(IUpdateAuditable model)
        {
            if (model == null)
            {
                throw new ArgumentNullException(nameof(model));
            }
            ValidateUserProperty(model.Creator, nameof(model.Creator));
            ValidateUserProperty(model.Updater, nameof(model.Updater));

            var auditData = new UpdateAuditData();

            auditData.CreateDate = model.CreateDate;
            auditData.UpdateDate = model.UpdateDate;
            auditData.Creator    = _userMicroSummaryMapper.Map(model.Creator);
            auditData.Updater    = _userMicroSummaryMapper.Map(model.Updater);

            return(auditData);
        }
        private bool SetAuditInfo(object entity, object[] currentState, string[] propertyNames, bool onSave)
        {
            IPersistentObject <T> persistentObject = entity as IPersistentObject <T>;

            if (persistentObject == null)
            {
                return(false);
            }

            IInsertAuditable insertAuditable = entity as IInsertAuditable;
            IUpdateAuditable updateAuditable = entity as IUpdateAuditable;

            if (insertAuditable == null && updateAuditable == null)
            {
                return(false);
            }

            DateTime time         = TimeProvider.Now.ToUniversalTime();
            string   identityName = IdentityProvider.IdentityName;

            if (identityName == null)
            {
                throw new InvalidOperationException("Unknown IdentityName");
            }

            Type entityType = entity.GetType();

            if (insertAuditable != null && (onSave || persistentObject.IsTransient))
            {
                IInsertAuditableProperties insertAuditableProperties = entity as IInsertAuditableProperties;
                string createdAtPropertyName =
                    insertAuditableProperties != null
                        ? insertAuditableProperties.CreatedAtPropertyName
                        : CreatedAtPropertyName;
                string createdByPropertyName =
                    insertAuditableProperties != null
                        ? insertAuditableProperties.CreatedByPropertyName
                        : CreatedByPropertyName;

                Set(entityType, propertyNames, currentState, createdAtPropertyName, time);
                Set(entityType, propertyNames, currentState, createdByPropertyName, identityName);

                insertAuditable.CreatedAt = time;
                insertAuditable.CreatedBy = identityName;
            }
            else if (updateAuditable != null)
            {
                IUpdateAuditableProperties updateAuditableProperties = entity as IUpdateAuditableProperties;
                string lastModifiedAtPropertyName =
                    updateAuditableProperties != null
                        ? updateAuditableProperties.LastModifiedAtPropertyName
                        : LastModifiedAtPropertyName;
                string lastModifiedByPropertyName =
                    updateAuditableProperties != null
                        ? updateAuditableProperties.LastModifiedByPropertyName
                        : LastModifiedByPropertyName;

                Set(entityType, propertyNames, currentState, lastModifiedAtPropertyName, time);
                Set(entityType, propertyNames, currentState, lastModifiedByPropertyName, identityName);

                updateAuditable.LastModifiedAt = time;
                updateAuditable.LastModifiedBy = identityName;
            }

            return(true);
        }