/// <summary>
        ///		If previous Seek failed and writer is not in a ready
        ///		state (file accessor is null) Flush does nothing
        /// </summary>
        /// <remarks>
        ///		DOCO:
        ///		Note that if storage transaction is rolled back the changes cannot be resubmitted if transaction is managed outside of the repository
        ///		and therefore can span multiple flushes. If on the other hand the transaction is managed in repository, and therefore guaranteed to
        ///		span one flush at most, the dirty flag is only cleared when the transaction ends and flush can be re-tried.
        ///		Thus in the former scenario whenever transaction fails writer should be disposed immediately (possibly after salvaging unsaved
        ///		items from it) much like with NHibernate's session.
        ///		In the latter scenario it may possible to re-flush data after IO failure but the previous pattern may still be preferable.
        /// </remarks>
        internal void Flush()
        {
            _clearDirtyFlagWhenTransactionCompletes = false;
            bool dropUnsavedItemsExplicitly;

            // locking to prevent simultaneous Write()
            lock (this)
            {
                using (var scope = _transactionManager.GetTransactionScope())
                {
                    // Transaction scope is not necessary when called from the RepositoryWriter as it must have already set the scope.
                    // However, when called as a preparation for commitment of externally controlled transaction (especially master managed tran-n
                    // when enlistment methods are executed on a worker thread) the scope is necessary to ensure transaction context is set up.
                    dropUnsavedItemsExplicitly = scope.NoTransaction || scope.IsTransactionOwner;

                    if (null != _currentAccessor && _dirty)
                    {
                        _currentAccessor.Encryptor = this.Encryptor;
                        _currentAccessor.Coder     = this.Coder;
                        _currentAccessor.Flush();
                    }

                    scope.Complete();
                }

                // the dirty flag does not depend on transaction spanning multiple flushes; it can only be reliably left ON when local transaction is started and finished
                // in repository during a single flush;
                if (dropUnsavedItemsExplicitly || _transactionManager.CanIOTransactionSpanMultipleRepositoryCalls)
                {
                    // transaction already committed or no transaction was used (which is the same)
                    // or transaction will live outside repository and dirty state cannot be used for resubmition anyways
                    _dirty = false;
                }
                else
                {
                    // shall recieve notification of transaction completion and will have to clear dirty flag there
                    _clearDirtyFlagWhenTransactionCompletes = true;
                }

                if (dropUnsavedItemsExplicitly)
                {
                    DropUnsavedItems();
                }
                else
                {
                    Check.Ensure(_transactionManager.PendingTransaction != null, "If there's transaction and it lives londer than scope it must be registered as pending and subscribed to");
                }
            }
        }