Ejemplo n.º 1
0
 private void RetainDeletedEntityKeys(IEnumerable <object> syncEntities)
 {
     _deletedEntityKeys = syncEntities
                          .Where(e => EntityAspect.Wrap(e).EntityState.IsDeleted())
                          .Select(e => EntityAspect.Wrap(e).EntityKey)
                          .ToList();
 }
Ejemplo n.º 2
0
        public override void OnSaving(TempHireEntities source, EntitySavingEventArgs args)
        {
            // Add necessary aggregate root object to the save list for validation and concurrency check
            List <EntityAspect> rootEas = args.Entities.OfType <IHasRoot>()
                                          .Select(e => EntityAspect.Wrap(e.Root))
                                          .Distinct()
                                          .Where(ea => ea != null && !ea.IsChanged && !ea.IsNullOrPendingEntity)
                                          .ToList();

            rootEas.ForEach(ea => ea.SetModified());
            rootEas.ForEach(ea => args.Entities.Add(ea.Entity));
        }
Ejemplo n.º 3
0
        private void RaiseDataChangedEvent(IEnumerable <object> savedEntities, IEnumerable <EntityKey> deletedEntityKeys)
        {
            var entityKeys = savedEntities
                             .Select(e => EntityAspect.Wrap(e).EntityKey)
                             .Concat(deletedEntityKeys)
                             .ToList();

            if (!entityKeys.Any())
            {
                return;
            }

            OnDataChanged(entityKeys);
        }
Ejemplo n.º 4
0
        public override bool ShouldImportEntity(object entity)
        {
            // Only import if the importing EntityManager holds a copy of the entity or its root aggregate in its cache
            if (EntityManager.FindEntity(EntityAspect.Wrap(entity).EntityKey) != null)
            {
                return(true);
            }

            var hasRoot = entity as IHasRoot;

            if (hasRoot != null)
            {
                return(EntityManager.FindEntity(EntityAspect.Wrap(hasRoot.Root).EntityKey) != null);
            }

            return(false);
        }
Ejemplo n.º 5
0
 private void OnSaved(object sender, EntitySavedEventArgs e)
 {
     try
     {
         if (!e.HasError)
         {
             var exportEntities = e.Entities
                                  .Where(entity => SyncInterceptor.ShouldExportEntity(entity) &&
                                         !_deletedEntityKeys
                                         .Contains(EntityAspect.Wrap(entity).EntityKey))
                                  .ToList();
             PublishEntities(exportEntities);
         }
         _deletedEntityKeys = null;
     }
     finally
     {
         IsSaving = false;
     }
 }
Ejemplo n.º 6
0
        protected override bool ValidateSave()
        {
            base.ValidateSave();

            // Create a sandbox to do the validation in.
            var em = new EntityManager(EntityManager);

            em.CacheStateManager.RestoreCacheState(EntityManager.CacheStateManager.GetCacheState());

            // Find all entities supporting custom validation
            var entities =
                em.FindEntities(EntityState.AllButDetached).OfType <EntityBase>().ToList();

            foreach (var e in entities)
            {
                var entityAspect = EntityAspect.Wrap(e);
                if (entityAspect.EntityState.IsDeletedOrDetached())
                {
                    continue;
                }

                var validationErrors = new VerifierResultCollection();
                e.Validate(validationErrors);

                validationErrors =
                    new VerifierResultCollection(entityAspect.ValidationErrors.Concat(validationErrors.Errors));
                validationErrors.Where(vr => !entityAspect.ValidationErrors.Contains(vr))
                .ForEach(entityAspect.ValidationErrors.Add);

                if (validationErrors.HasErrors)
                {
                    throw new EntityServerException(validationErrors.Select(v => v.Message).ToAggregateString("\n"),
                                                    null,
                                                    PersistenceOperation.Save, PersistenceFailure.Validation);
                }
            }

            return(true);
        }
Ejemplo n.º 7
0
        private void Validate(EntitySavingEventArgs args)
        {
            var allValidationErrors = new VerifierResultCollection();

            foreach (var entity in args.Entities)
            {
                var entityAspect = EntityAspect.Wrap(entity);
                if (entityAspect.EntityState.IsDeletedOrDetached())
                {
                    continue;
                }

                var validationErrors = Manager.VerifierEngine.Execute(entity);
                foreach (var d in _configuration.Delegates ?? new EntityManagerDelegate <T> [0])
                {
                    d.Validate(entity, validationErrors);
                }
                // Extract only validation errors
                validationErrors = validationErrors.Errors;

                validationErrors.Where(vr => !entityAspect.ValidationErrors.Contains(vr))
                .ForEach(entityAspect.ValidationErrors.Add);

                validationErrors.ForEach(allValidationErrors.Add);
            }

            if (allValidationErrors.HasErrors)
            {
                if (!ValidationErrorNotifiers.Any())
                {
                    throw new ValidationException(allValidationErrors.Select(v => v.Message).ToAggregateString("\n"));
                }

                ValidationErrorNotifiers.ForEach(s => s.OnValidationError(allValidationErrors));
                args.Cancel = true;
            }
        }
Ejemplo n.º 8
0
 public EntityFacts(object entity)
 {
     _entityAspect = EntityAspect.Wrap(entity);
 }
Ejemplo n.º 9
0
        /// <summary>
        /// Returns true if the provided entity is attached to the current UnitOfWork's EntityManager.
        /// </summary>
        /// <param name="entity">Entity to check if attached to current UnitOfWork.</param>
        public bool HasEntity(object entity)
        {
            var entityAspect = EntityAspect.Wrap(entity);

            return(!entityAspect.EntityState.IsDetached() && EntityManager == entityAspect.EntityManager);
        }