示例#1
0
        /// <inheritdoc/>
        public KeyMapping Replay(Session session)
        {
            if (session.Operations.IsRegisteringOperation)
            {
                throw new InvalidOperationException(Strings.ExRunningOperationRegistrationMustBeFinished);
            }

            var         executionContext     = new OperationExecutionContext(session);
            bool        isSystemOperationLog = LogType == OperationLogType.SystemOperationLog;
            KeyMapping  keyMapping           = null;
            Transaction transaction          = null;

            using (session.Activate()) {
                using (isSystemOperationLog ? session.OpenSystemLogicOnlyRegion() : null)
                    using (var tx = session.OpenTransaction(TransactionOpenMode.New)) {
                        foreach (var operation in operations)
                        {
                            operation.Prepare(executionContext);
                        }

                        session.Query.Many <Entity>(executionContext.KeysToPrefetch).Run();

                        foreach (var operation in operations)
                        {
                            var identifierToKey = new Dictionary <string, Key>();
                            var handler         = new EventHandler <OperationCompletedEventArgs>((sender, e) => {
                                foreach (var pair in e.Operation.IdentifiedEntities)
                                {
                                    identifierToKey.Add(pair.Key, pair.Value);
                                }
                            });

                            session.Operations.OutermostOperationCompleted += handler;
                            try {
                                operation.Execute(executionContext);
                            }
                            finally {
                                session.Operations.OutermostOperationCompleted -= handler;
                            }

                            foreach (var pair in operation.IdentifiedEntities)
                            {
                                string identifier = pair.Key;
                                var    oldKey     = pair.Value;
                                var    newKey     = identifierToKey.GetValueOrDefault(identifier);
                                if (newKey != null)
                                {
                                    executionContext.AddKeyMapping(oldKey, newKey);
                                }
                            }
                        }

                        keyMapping = new KeyMapping(executionContext.KeyMapping);

                        tx.Complete();
                    }
                return(keyMapping);
            }
        }
 /// <summary>
 /// Converts the sequence to transactional.
 /// In fact, it does nothing if current transaction is available;
 /// otherwise it opens a new transaction, caches the sequence enumeration result,
 /// closes the transaction and returns cached sequence enumerator.
 /// </summary>
 /// <typeparam name="T">The type of item in sequence.</typeparam>
 /// <param name="source">The sequence to convert.</param>
 /// <param name="session">The session.</param>
 /// <param name="isolationLevel">The isolation level.</param>
 /// <returns>"Transactional" version of sequence.</returns>
 public static IEnumerable <T> ToTransactional <T>(this IEnumerable <T> source, Session session, IsolationLevel isolationLevel)
 {
     using (session.Activate(true))
         using (var tx = session.OpenAutoTransaction(isolationLevel)) {
             foreach (var item in source)
             {
                 yield return(item);
             }
             tx.Complete();
         }
 }
 private void EnsureIsLoaded(int?maxItemCount)
 {
     if (CheckStateIsLoaded())
     {
         if (State.IsFullyLoaded)
         {
             return;
         }
         var requestedItemCount = maxItemCount.HasValue
   ? (int)maxItemCount
   : int.MaxValue;
         if (State.CachedItemCount > requestedItemCount)
         {
             return;
         }
     }
     using (Session.Activate())
         Session.Handler.FetchEntitySet(Owner.Key, Field, maxItemCount);
 }