Exemplo n.º 1
0
        private void Execute(Guid guid, Action <Kada> callback, int counter = 0)
        {
            try
            {
                var kada = Kade.Find(guid.ToString());
                if (kada == null)
                {
                    throw new ArgumentException("Kada sa GUID-om '" + guid.ToString() + "' ne postoji!");
                }

                callback(kada);
                Kade.Update(kada);
            }
            catch
            {
                if (counter < 5)
                {
                    Thread.Sleep(25);
                    Execute(guid, callback, counter + 1);
                }
                else
                {
                    throw;
                }
            }
        }
Exemplo n.º 2
0
        /* bulk loading of worlds. use such pattern for production code */

        /*public void LoadWorldsFast(int repeat, World[] worlds)
         * {
         *      BulkReader.Reset(true);
         *      for (int i = 0; i < repeat; i++)
         *      {
         *              var id = Random.Next(10000) + 1;
         *              LazyWorlds[i] = BulkReader.Find<World>(id.ToString());
         *      }
         *      BulkReader.Execute();
         *      for (int i = 0; i < repeat; i++)
         *              worlds[i] = LazyWorlds[i].Value;
         * }*/

        /* multiple roundtrips loading of worlds. don't write such production code */
        public void LoadWorldsSlow(int repeat, World[] worlds)
        {
            for (int i = 0; i < repeat; i++)
            {
                var id = Random.Next(10000) + 1;
                worlds[i] = WorldRepository.Find(id);
            }
        }
Exemplo n.º 3
0
        public void Handle(MarkDone domainEvent)
        {
            var task = Repository.Find(domainEvent.taskURI);

            if (task == null)
            {
                throw new ArgumentException("Cant find task with uri: " + domainEvent.taskURI);
            }
            if (task.isDone)
            {
                throw new ArgumentException("Task " + task.name + " is already marked as done!");
            }
            task.isDone = true;
            Repository.Update(task);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Delete aggregate root defined by provided identifier.
        /// Deleted aggregate is returned.
        /// </summary>
        /// <typeparam name="TRoot">aggregate type</typeparam>
        /// <param name="repository">persistable repository</param>
        /// <param name="uri">aggregate identifier</param>
        /// <returns>deleted aggregate root</returns>
        public static TRoot Delete <TRoot>(this IPersistableRepository <TRoot> repository, string uri)
            where TRoot : IAggregateRoot
        {
            Contract.Requires(repository != null);
            Contract.Requires(uri != null);

            var oldValue = repository.Find(uri);

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

            repository.Delete(new[] { oldValue });
            return(oldValue);
        }
Exemplo n.º 5
0
        public static Task Delete <T>(this IPersistableRepository <T> repository, string uri)
            where T : class, IAggregateRoot
        {
            if (repository == null)
            {
                throw new ArgumentNullException("repository can't be null");
            }
            var oldValue = repository.Find(uri);

            return(oldValue.ContinueWith(t =>
            {
                if (t.Result == null)
                {
                    throw new ArgumentException(string.Format("Can't find {0} with URI: {1}", typeof(T).FullName, uri));
                }

                repository.Delete(new[] { t.Result }).Wait();
            }));
        }
Exemplo n.º 6
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);
        }