Esempio n. 1
0
        /// <summary>
        /// Saves the specified entity record.
        /// </summary>
        /// <returns>A task representing an asynchronous LoadAll operation.</returns>
        /// <param name="entries">The source entity.</param>
        /// <param name="operation">The source bulk save operation.</param>
        public virtual async Task <IEnumerable <T> > SaveManyAsync(IEnumerable <T> entries, BulkSaveOperation operation)
        {
            if (entries == null)
            {
                throw new ArgumentNullException(nameof(entries));
            }

            List <T> updates = new List <T>();
            List <T> creates = new List <T>();

            foreach (T entry in entries)
            {
                if (entry is IValidateable)
                {
                    IValidateable validateable = entry as IValidateable;
                    if (!validateable.IsValid())
                    {
                        throw new RecordValidationException(RecordValidationException.DefaultMessage);
                    }
                }
            }

            return((operation == BulkSaveOperation.Create)
                ? await _repository.InsertAsync(entries)
                : await _repository.UpdateAsync(entries));
        }
Esempio n. 2
0
        /// <summary>
        /// Validates if the object is ok.
        /// </summary>
        /// <param name="target">The target.</param>
        /// <exception cref="BadArgumentsException">If the object is bad.</exception>
        public static void Validate(IValidateable target)
        {
            var reasons = target.IsValid().ToList();

            if (false == reasons.Any())
            {
                return;
            }

            var result = string.Join(",", reasons.Select(r => $"{r.key} : {r.reason}"));

            throw new BadArgumentsException(result);
        }
Esempio n. 3
0
 /// <summary>
 /// Saves the specified entity record.
 /// </summary>
 /// <returns>A task representing an asynchronous LoadAll operation.</returns>
 /// <param name="entry">The source entity.</param>
 public virtual async Task <T> SaveAsync(T entry)
 {
     if (entry == null)
     {
         throw new ArgumentException(nameof(entry));
     }
     if (entry is IValidateable)
     {
         IValidateable validateable = entry as IValidateable;
         if (!validateable.IsValid())
         {
             throw new RecordValidationException(RecordValidationException.DefaultMessage);
         }
     }
     return((entry.Id != Guid.Empty)
         ? await _repository.UpdateAsync(entry, entry.Id)
         : await _repository.InsertAsync(entry));
 }
Esempio n. 4
0
        /// <summary>
        /// Saves the specified entity record.
        /// </summary>
        /// <param name="entry">The source T.</param>
        public T Save(T entry)
        {
            if (entry == null)
            {
                throw new ArgumentNullException(nameof(entry));
            }
            if (entry is IValidateable)
            {
                IValidateable validateable = entry as IValidateable;
                if (!validateable.IsValid())
                {
                    throw new RecordValidationException(RecordValidationException.DefaultMessage);
                }
            }

            return((entry.Id != Guid.Empty)
                ? _repository.Update(entry, entry.Id)
                : _repository.Insert(entry));
        }