public virtual SaveValidation <T> Update(T entity)
        {
            var output = new SaveValidation <T>()
            {
                IsSuccess = true
            };

            if (entity.Id == default(Guid))
            {
                output.IsSuccess = false;
                output.Validation.Errors.Add("No se puede actualizar una entidad sin Id");
            }

            if (entity.Id != default(Guid) && DbSet.All(x => x.Id != entity.Id))
            {
                output.IsSuccess = false;
                output.Validation.Errors.Add("No existe una entity con ese id");
            }

            if (output.IsSuccess)
            {
                DbSet.Update(entity);
                DbContext.SaveChanges();
            }

            return(output);
        }
        public virtual SaveValidation <T> Update(T entity)
        {
            var output = new SaveValidation <T>
            {
                SaveValidationSuccesful = true
            };

            if (entity.Id == default(Guid))
            {
                output.SaveValidationSuccesful = false;
                output.Validation.Messages.Add("No se puede actualizar una entidad sin Id");
            }

            //if (entity.Id != default(Guid) && !DbSet.Any(x => x.Id == entity.Id))
            if (entity.Id != default(Guid) && DbSet.All(x => x.Id != entity.Id)) // esta es mejor porque tiene mejor performance
            {
                output.SaveValidationSuccesful = false;
                output.Validation.Messages.Add("No existe una entity con ese id");
            }

            if (output.SaveValidationSuccesful)
            {
                DbSet.Update(entity);
            }

            return(output);
        }
        public virtual SaveValidation <T> Add(T entity)
        {
            var output = new SaveValidation <T>()
            {
                IsSuccess = true
            };

            if (entity.Id == default(Guid))
            {
                entity.Id = Guid.NewGuid();
            }

            if (DbSet.Any(x => x.Id == entity.Id))
            {
                output.IsSuccess = false;
                output.Validation.Errors.Add("Ya existe una entidad con ese id");
            }

            if (output.IsSuccess)
            {
                DbSet.Add(entity);
                DbContext.SaveChanges();
            }

            return(output);
        }
예제 #4
0
        public virtual SaveValidation <T> Add(T entity)
        {
            var output = new SaveValidation <T>(true);

            if (entity.Id == Guid.Empty)
            {
                entity.Id = Guid.NewGuid();
            }
            if (DbSet.ContainsKey(entity.Id))
            {
                output.SaveValidationSuccesful = false;
                output.SaveValidationMessages.Add("Ya existe con este GUID");
            }

            if (output.SaveValidationSuccesful)
            {
                DbSet.Add(entity.Id, entity);
            }

            return(output);
        }
예제 #5
0
        public virtual SaveValidation <T> Update(T entity)
        {
            var output = new SaveValidation <T>();

            if (entity.Id == default(Guid))
            {
                output.IsSuccess = false;
                output.Validation.Errors.Add("No se puede modificar entidad sin GUID.");
            }
            if (entity.Id != default(Guid) && !DbSet.ContainsKey(entity.Id))
            {
                output.IsSuccess = false;
                output.Validation.Errors.Add("No existe ninguna entidad con este GUID.");
            }
            if (output.IsSuccess)
            {
                DbSet[entity.Id] = entity;
            }

            return(output);
        }
예제 #6
0
        public virtual SaveValidation <T> Add(T entity)
        {
            var output = new SaveValidation <T>(true);

            if (entity.Id == Guid.Empty)
            {
                entity.Id = Guid.NewGuid();
            }
            if (DbSet.ContainsKey(entity.Id))
            {
                output.IsSuccess = false;
                output.Validation.Errors.Add("Ya existe entidad con este GUID");
            }

            if (output.IsSuccess)
            {
                DbSet.Add(entity.Id, entity);
            }

            return(output);
        }
예제 #7
0
        public virtual SaveValidation <T> Update(T entity)
        {
            var output = new SaveValidation <T>();

            if (entity.Id == default(Guid))
            {
                output.SaveValidationSuccesful = false;
                output.SaveValidationMessages.Add("Cannot update an entity without GUID.");
            }
            if (entity.Id != default(Guid) && !DbSet.ContainsKey(entity.Id))
            {
                output.SaveValidationSuccesful = false;
                output.SaveValidationMessages.Add("An entity with this GUID doesn't exist");
            }

            if (output.SaveValidationSuccesful)
            {
                DbSet[entity.Id] = entity;
            }

            return(output);
        }
예제 #8
0
        public virtual SaveValidation <T> Save <T>() where T : Entity
        {
            var output = new SaveValidation <T>();

            CurrentValidation = Validate();

            if (CurrentValidation.IsSuccess)
            {
                var repo = DepCon.Resolve <IRepository <T> >();

                if (this.Id == Guid.Empty)
                {
                    output = repo.Add(this as T);
                }
                else
                {
                    output = repo.Update(this as T);
                }
            }

            output.Validation = CurrentValidation;

            return(output);
        }