Exemplo n.º 1
0
        /// <summary>
        /// Persist aggregate roots. Bulk persistance.
        /// Inserted aggregates will return new identifiers.
        /// Aggregate roots will be modified in place.
        /// For update aggregates, old aggregates will be loaded from change tracking or looked up using aggregate identifier.
        /// </summary>
        /// <typeparam name="TRoot">aggregate root type</typeparam>
        /// <param name="repository">persistable repository</param>
        /// <param name="insert">new aggregates</param>
        /// <param name="update">collection of changed aggregates</param>
        /// <param name="delete">remove aggregates</param>
        /// <returns>created identifiers</returns>
        public static string[] Persist <TRoot>(
            this IPersistableRepository <TRoot> repository,
            IEnumerable <TRoot> insert,
            IEnumerable <TRoot> update,
            IEnumerable <TRoot> delete)
            where TRoot : IAggregateRoot
        {
            Contract.Requires(repository != null);

            if (insert != null && insert.Any() ||
                update != null && update.Any() ||
                delete != null && delete.Any())
            {
                return
                    (repository.Persist(
                         insert,
                         update != null
                                                        ? (from u in update
                                                           let ct = u as IChangeTracking <TRoot>
                                                                    select new KeyValuePair <TRoot, TRoot>(ct != null ? ct.GetOriginalValue() : default(TRoot), u))
                         .ToList()
                                                        : null,
                         delete));
            }
            return(new string[0]);
        }
Exemplo n.º 2
0
 public static Task Update <T>(this IPersistableRepository <T> repository, IEnumerable <T> data)
     where T : class, IAggregateRoot
 {
     if (repository == null)
     {
         throw new ArgumentNullException("repository can't be null");
     }
     return(repository.Persist(null, data, null));
 }
Exemplo n.º 3
0
        /// <summary>
        /// Update changed aggregate root.
        /// Aggregate is modified in place.
        /// </summary>
        /// <typeparam name="TRoot">aggregate type</typeparam>
        /// <param name="repository">persistable repository</param>
        /// <param name="oldValue">old aggregate instance</param>
        /// <param name="newValue">new aggregate instance</param>
        public static void Update <TRoot>(this IPersistableRepository <TRoot> repository, TRoot oldValue, TRoot newValue)
            where TRoot : IAggregateRoot
        {
            Contract.Requires(repository != null);
            Contract.Requires(oldValue != null);
            Contract.Requires(newValue != null);

            repository.Persist(null, new[] { new KeyValuePair <TRoot, TRoot>(oldValue, newValue) }, null);
        }
Exemplo n.º 4
0
 public static Task Update <T>(this IPersistableRepository <T> repository, T oldValue, T newValue)
     where T : class, IAggregateRoot
 {
     if (repository == null)
     {
         throw new ArgumentNullException("repository can't be null");
     }
     return(repository.Persist(null, new[] { new KeyValuePair <T, T>(oldValue, newValue) }, null));
 }
Exemplo n.º 5
0
        /// <summary>
        /// Update changed aggregate root.
        /// Aggregate is modified in place.
        /// </summary>
        /// <typeparam name="TRoot">aggregate type</typeparam>
        /// <param name="repository">persistable repository</param>
        /// <param name="data">changed aggregate</param>
        public static void Update <TRoot>(this IPersistableRepository <TRoot> repository, TRoot data)
            where TRoot : IAggregateRoot
        {
            Contract.Requires(repository != null);

            var ct = data as IChangeTracking <TRoot>;

            repository.Persist(null, new[] { new KeyValuePair <TRoot, TRoot>(ct != null ? ct.GetOriginalValue() : default(TRoot), data) }, null);
        }
Exemplo n.º 6
0
        /// <summary>
        /// Delete aggregate roots.
        /// </summary>
        /// <typeparam name="TRoot">aggregate type</typeparam>
        /// <param name="repository">persistable repository</param>
        /// <param name="data">aggregates to delete</param>
        public static void Delete <TRoot>(this IPersistableRepository <TRoot> repository, IEnumerable <TRoot> data)
            where TRoot : IAggregateRoot
        {
            Contract.Requires(repository != null);

            if (data != null && data.Any())
            {
                repository.Persist(null, null, data);
            }
        }
Exemplo n.º 7
0
        /// <summary>
        /// Insert new aggregate roots.
        /// Aggregates are modified in place.
        /// </summary>
        /// <typeparam name="TRoot">aggregate type</typeparam>
        /// <param name="repository">persistable repository</param>
        /// <param name="data">new aggregates</param>
        /// <returns>created indentifiers</returns>
        public static string[] Insert <TRoot>(this IPersistableRepository <TRoot> repository, IEnumerable <TRoot> data)
            where TRoot : IAggregateRoot
        {
            Contract.Requires(repository != null);

            if (data != null && data.Any())
            {
                return(repository.Persist(data, null, null));
            }
            return(new string[0]);
        }
Exemplo n.º 8
0
 public static Task <string[]> Persist <T>(
     this IPersistableRepository <T> repository,
     IEnumerable <T> insert,
     IEnumerable <T> update,
     IEnumerable <T> delete)
     where T : class, IAggregateRoot
 {
     if (repository == null)
     {
         throw new ArgumentNullException("repository can't be null");
     }
     return
         (repository.Persist(
              insert,
              update != null ? update.Select(it => new KeyValuePair <T, T>(default(T), it)) : null,
              delete));
 }
Exemplo n.º 9
0
        //TODO: remove!?
        /// <summary>
        /// Change aggregate root defined by identifier using provided action.
        /// </summary>
        /// <typeparam name="TRoot">aggregate type</typeparam>
        /// <param name="repository">persistable repository</param>
        /// <param name="uri">aggregate identifier</param>
        /// <param name="update">change method</param>
        /// <returns>found and changed aggregate</returns>
        public static TRoot Update <TRoot>(this IPersistableRepository <TRoot> repository, string uri, Action <TRoot> update)
            where TRoot : IAggregateRoot
        {
            Contract.Requires(repository != null);
            Contract.Requires(uri != null);
            Contract.Requires(update != null);

            var found = repository.Find(uri);

            if (found == null)
            {
                throw new ArgumentException(string.Format("Can't find {0} with URI: {1}", typeof(TRoot).FullName, uri));
            }

            var ct       = found as IChangeTracking <TRoot>;
            var original = ct != null
                                ? ct.GetOriginalValue()
                                : found is ICloneable ? (TRoot)((ICloneable)found).Clone() : default(TRoot);

            update(found);
            repository.Persist(null, new[] { new KeyValuePair <TRoot, TRoot>(original, found) }, null);
            return(found);
        }