Example #1
0
 /// <inheritdoc />
 public IScope CreateDetachedScope(
     IsolationLevel isolationLevel           = IsolationLevel.Unspecified,
     RepositoryCacheMode repositoryCacheMode = RepositoryCacheMode.Unspecified,
     IEventDispatcher eventDispatcher        = null,
     IScopedNotificationPublisher scopedNotificationPublisher = null,
     bool?scopeFileSystems = null)
 => new Scope(this, _coreDebugSettings, _mediaFileManager, _eventAggregator, _loggerFactory.CreateLogger <Scope>(), _fileSystems, true, null, isolationLevel, repositoryCacheMode, eventDispatcher, scopedNotificationPublisher, scopeFileSystems);
Example #2
0
 public IScope CreateScope(
     IsolationLevel isolationLevel                      = IsolationLevel.Unspecified,
     RepositoryCacheMode repositoryCacheMode            = RepositoryCacheMode.Unspecified,
     IScopedNotificationPublisher notificationPublisher = null,
     bool?scopeFileSystems = null,
     bool callContext      = false,
     bool autoComplete     = false) => _scope;
 public virtual bool TryQuery <T>(out IEnumerable <T> result, Func <T, bool> predicate = null,
                                  RepositoryCacheMode cacheMode = DefaultCacheMode)
     where T : class, new()
 {
     result = Query(predicate ?? DefaultPredicate, cacheMode);
     return(result != null && result.Any());
 }
Example #4
0
        /// <inheritdoc />
        public IScope CreateScope(
            IsolationLevel isolationLevel                      = IsolationLevel.Unspecified,
            RepositoryCacheMode repositoryCacheMode            = RepositoryCacheMode.Unspecified,
            IScopedNotificationPublisher notificationPublisher = null,
            bool?scopeFileSystems = null,
            bool callContext      = false,
            bool autoComplete     = false)
        {
            Scope ambientScope = AmbientScope;

            if (ambientScope == null)
            {
                IScopeContext ambientContext = AmbientContext;
                ScopeContext  newContext     = ambientContext == null ? new ScopeContext() : null;
                var           scope          = new Scope(this, _coreDebugSettings, _eventAggregator, _loggerFactory.CreateLogger <Scope>(), _fileSystems, false, newContext, isolationLevel, repositoryCacheMode, notificationPublisher, scopeFileSystems, callContext, autoComplete);
                // assign only if scope creation did not throw!
                PushAmbientScope(scope);
                if (newContext != null)
                {
                    PushAmbientScopeContext(newContext);
                }
                return(scope);
            }

            var nested = new Scope(this, _coreDebugSettings, _eventAggregator, _loggerFactory.CreateLogger <Scope>(), _fileSystems, ambientScope, isolationLevel, repositoryCacheMode, notificationPublisher, scopeFileSystems, callContext, autoComplete);

            PushAmbientScope(nested);
            return(nested);
        }
Example #5
0
 /// <inheritdoc />
 public IScope CreateDetachedScope(
     IsolationLevel isolationLevel           = IsolationLevel.Unspecified,
     RepositoryCacheMode repositoryCacheMode = RepositoryCacheMode.Unspecified,
     IEventDispatcher eventDispatcher        = null,
     bool?scopeFileSystems = null)
 {
     return(new Scope(this, true, null, isolationLevel, repositoryCacheMode, eventDispatcher, scopeFileSystems));
 }
Example #6
0
 /// <inheritdoc />
 Core.Scoping.IScope Core.Scoping.IScopeProvider.CreateDetachedScope(IsolationLevel isolationLevel,
                                                                     RepositoryCacheMode repositoryCacheMode, IEventDispatcher?eventDispatcher,
                                                                     IScopedNotificationPublisher?scopedNotificationPublisher, bool?scopeFileSystems) =>
 (Core.Scoping.IScope)CreateDetachedScope(
     isolationLevel,
     repositoryCacheMode,
     eventDispatcher,
     scopedNotificationPublisher,
     scopeFileSystems);
Example #7
0
 // initializes a new scope in a nested scopes chain, with its parent
 public Scope(ScopeProvider scopeProvider, Scope parent,
              IsolationLevel isolationLevel           = IsolationLevel.Unspecified,
              RepositoryCacheMode repositoryCacheMode = RepositoryCacheMode.Unspecified,
              IEventDispatcher eventDispatcher        = null,
              bool?scopeFileSystems = null,
              bool callContext      = false)
     : this(scopeProvider, parent, null, false, isolationLevel, repositoryCacheMode, eventDispatcher, scopeFileSystems, callContext)
 {
 }
Example #8
0
 // initializes a new scope
 public Scope(ScopeProvider scopeProvider,
     ILogger logger, FileSystems fileSystems, bool detachable, ScopeContext scopeContext,
     IsolationLevel isolationLevel = IsolationLevel.Unspecified,
     RepositoryCacheMode repositoryCacheMode = RepositoryCacheMode.Unspecified,
     IEventDispatcher eventDispatcher = null,
     bool? scopeFileSystems = null,
     bool callContext = false,
     bool autoComplete = false)
     : this(scopeProvider, logger, fileSystems, null, scopeContext, detachable, isolationLevel, repositoryCacheMode, eventDispatcher, scopeFileSystems, callContext, autoComplete)
 { }
Example #9
0
        /// <inheritdoc />
        public IScope CreateScope(
            IsolationLevel isolationLevel           = IsolationLevel.Unspecified,
            RepositoryCacheMode repositoryCacheMode = RepositoryCacheMode.Unspecified,
            IEventDispatcher eventDispatcher        = null,
            bool?scopeFileSystems = null,
            bool callContext      = false)
        {
            var ambient = AmbientScope;

            if (ambient == null)
            {
                var ambientContext = AmbientContext;
                var newContext     = ambientContext == null ? new ScopeContext() : null;
                var scope          = new Scope(this, false, newContext, isolationLevel, repositoryCacheMode, eventDispatcher, scopeFileSystems, callContext);
                // assign only if scope creation did not throw!
                SetAmbient(scope, newContext ?? ambientContext);
                return(scope);
            }

            // replace noScope with a real one
            var noScope = ambient as NoScope;

            if (noScope != null)
            {
#if DEBUG_SCOPES
                Disposed(noScope);
#endif
                // peta poco nulls the shared connection after each command unless there's a trx
                var database = noScope.DatabaseOrNull;
                if (database != null && database.InTransaction)
                {
                    throw new Exception("NoScope is in a transaction.");
                }
                var ambientContext = AmbientContext;
                var newContext     = ambientContext == null ? new ScopeContext() : null;
                var scope          = new Scope(this, noScope, newContext, isolationLevel, repositoryCacheMode, eventDispatcher, scopeFileSystems, callContext);
                // assign only if scope creation did not throw!
                SetAmbient(scope, newContext ?? ambientContext);
                return(scope);
            }

            var ambientScope = ambient as Scope;
            if (ambientScope == null)
            {
                throw new Exception("Ambient scope is not a Scope instance.");
            }

            var nested = new Scope(this, ambientScope, isolationLevel, repositoryCacheMode, eventDispatcher, scopeFileSystems, callContext);
            SetAmbient(nested, AmbientContext);
            return(nested);
        }
        public override IQueryable <T> Query <T>(RepositoryCacheMode cacheMode = DefaultCacheMode)
        {
            // Restore the previous mode afterwards, just in case.
            CacheMode?previousMode = null;

            return(SessionFunc(s =>
            {
                previousMode = s.CacheMode;
                s.CacheMode = cacheMode.ToCacheMode();
                return s.Query <T>().CacheMode(cacheMode.ToCacheMode()); // TODO: TBD: probably the right thing to do here, but be careful of the previous mode...
            }, s =>
            {
                // Restore from the previous mode or current one if necessary.
                s.CacheMode = previousMode ?? s.CacheMode;
            }));
        }
Example #11
0
 /// <inheritdoc />
 Cms.Core.Scoping.IScope Cms.Core.Scoping.IScopeProvider.CreateScope(
     IsolationLevel isolationLevel                      = IsolationLevel.Unspecified,
     RepositoryCacheMode repositoryCacheMode            = RepositoryCacheMode.Unspecified,
     IEventDispatcher?eventDispatcher                   = null,
     IScopedNotificationPublisher?notificationPublisher = null,
     bool?scopeFileSystems = null,
     bool callContext      = false,
     bool autoComplete     = false) =>
 (Cms.Core.Scoping.IScope)CreateScope(
     isolationLevel,
     repositoryCacheMode,
     eventDispatcher,
     notificationPublisher,
     scopeFileSystems,
     callContext,
     autoComplete);
Example #12
0
        // initializes a new scope, replacing a NoScope instance
        public Scope(ScopeProvider scopeProvider, NoScope noScope,
                     ScopeContext scopeContext,
                     IsolationLevel isolationLevel           = IsolationLevel.Unspecified,
                     RepositoryCacheMode repositoryCacheMode = RepositoryCacheMode.Unspecified,
                     IEventDispatcher eventDispatcher        = null,
                     bool?scopeFileSystems = null,
                     bool callContext      = false)
            : this(scopeProvider, null, scopeContext, false, isolationLevel, repositoryCacheMode, eventDispatcher, scopeFileSystems, callContext)
        {
            // steal everything from NoScope
            _database = noScope.DatabaseOrNull;
            _messages = noScope.MessagesOrNull;

            // make sure the NoScope can be replaced ie not in a transaction
            if (_database != null && _database.InTransaction)
            {
                throw new Exception("NoScope instance is not free.");
            }
        }
 public abstract IQueryable <T> Query <T>(RepositoryCacheMode cacheMode = DefaultCacheMode)
     where T : class, new();
 public virtual IEnumerable <T> Query <T>(Func <T, bool> predicate,
                                          RepositoryCacheMode cacheMode = DefaultCacheMode)
     where T : class, new()
 => Query <T>(cacheMode).Where(predicate ?? DefaultPredicate);
Example #15
0
 public virtual IEnumerable <T> GetAll <T>(Func <T, bool> predicate        = null
                                           , RepositoryCacheMode cacheMode = Normal)
     where T : class, new()
 {
     return(Transact(r => r.Query(predicate, cacheMode).ToArray()));
 }
Example #16
0
        // initializes a new scope
        private Scope(ScopeProvider scopeProvider,
                      Scope parent, ScopeContext scopeContext, bool detachable,
                      IsolationLevel isolationLevel           = IsolationLevel.Unspecified,
                      RepositoryCacheMode repositoryCacheMode = RepositoryCacheMode.Unspecified,
                      IEventDispatcher eventDispatcher        = null,
                      bool?scopeFileSystems = null,
                      bool callContext      = false)
        {
            _scopeProvider       = scopeProvider;
            _scopeContext        = scopeContext;
            _isolationLevel      = isolationLevel;
            _repositoryCacheMode = repositoryCacheMode;
            _eventDispatcher     = eventDispatcher;
            _scopeFileSystem     = scopeFileSystems;
            _callContext         = callContext;
            Detachable           = detachable;

#if DEBUG_SCOPES
            _scopeProvider.RegisterScope(this);
            Console.WriteLine("create " + _instanceId.ToString("N").Substring(0, 8));
#endif

            if (detachable)
            {
                if (parent != null)
                {
                    throw new ArgumentException("Cannot set parent on detachable scope.", "parent");
                }
                if (scopeContext != null)
                {
                    throw new ArgumentException("Cannot set context on detachable scope.", "scopeContext");
                }

                // detachable creates its own scope context
                _scopeContext = new ScopeContext();

                // see note below
                if (scopeFileSystems == true)
                {
                    _fscope = FileSystemProviderManager.Current.Shadow(Guid.NewGuid());
                }

                return;
            }

            if (parent != null)
            {
                ParentScope = parent;

                // cannot specify a different mode!
                if (repositoryCacheMode != RepositoryCacheMode.Unspecified && parent.RepositoryCacheMode != repositoryCacheMode)
                {
                    throw new ArgumentException("Cannot be different from parent.", "repositoryCacheMode");
                }

                // cannot specify a dispatcher!
                if (_eventDispatcher != null)
                {
                    throw new ArgumentException("Cannot be specified on nested scope.", "eventDispatcher");
                }

                // cannot specify a different fs scope!
                if (scopeFileSystems != null && parent._scopeFileSystem != scopeFileSystems)
                {
                    throw new ArgumentException("Cannot be different from parent.", "scopeFileSystems");
                }
            }
            else
            {
                // the FS scope cannot be "on demand" like the rest, because we would need to hook into
                // every scoped FS to trigger the creation of shadow FS "on demand", and that would be
                // pretty pointless since if scopeFileSystems is true, we *know* we want to shadow
                if (scopeFileSystems == true)
                {
                    _fscope = FileSystemProviderManager.Current.Shadow(Guid.NewGuid());
                }
            }
        }
Example #17
0
 public virtual Task <IEnumerable <T> > GetAllAsync <T>(Func <T, bool> predicate      = null,
                                                        RepositoryCacheMode cacheMode = Normal)
     where T : class, new()
 {
     return(Task.Run(() => GetAll(predicate, cacheMode)));
 }
Example #18
0
 public virtual T GetOneOrDefault <T>(Func <T, bool> predicate = null, RepositoryCacheMode cacheMode = Normal)
     where T : class, new()
 {
     return(GetAll(predicate, cacheMode).SingleOrDefault());
 }
Example #19
0
 public virtual Task <T> GetOneOrDefaultAsync <T>(Func <T, bool> predicate = null, RepositoryCacheMode cacheMode = Normal)
     where T : class, new()
 {
     return(Task.Run(() => GetOneOrDefault(predicate, cacheMode)));
 }
 internal static CacheMode ToCacheMode(this RepositoryCacheMode mode)
 {
     return(LazyCacheModeMap.Value[mode]);
 }
Example #21
0
        // initializes a new scope
        private Scope(ScopeProvider scopeProvider,
                      ILogger logger, FileSystems fileSystems, Scope parent, ScopeContext scopeContext, bool detachable,
                      IsolationLevel isolationLevel           = IsolationLevel.Unspecified,
                      RepositoryCacheMode repositoryCacheMode = RepositoryCacheMode.Unspecified,
                      IEventDispatcher eventDispatcher        = null,
                      bool?scopeFileSystems = null,
                      bool callContext      = false,
                      bool autoComplete     = false)
        {
            _scopeProvider = scopeProvider;
            _logger        = logger;

            Context = scopeContext;

            _isolationLevel      = isolationLevel;
            _repositoryCacheMode = repositoryCacheMode;
            _eventDispatcher     = eventDispatcher;
            _scopeFileSystem     = scopeFileSystems;
            _callContext         = callContext;
            _autoComplete        = autoComplete;

            Detachable = detachable;

#if DEBUG_SCOPES
            _scopeProvider.RegisterScope(this);
            Console.WriteLine("create " + InstanceId.ToString("N").Substring(0, 8));
#endif

            if (detachable)
            {
                if (parent != null)
                {
                    throw new ArgumentException("Cannot set parent on detachable scope.", nameof(parent));
                }
                if (scopeContext != null)
                {
                    throw new ArgumentException("Cannot set context on detachable scope.", nameof(scopeContext));
                }
                if (autoComplete)
                {
                    throw new ArgumentException("Cannot auto-complete a detachable scope.", nameof(autoComplete));
                }

                // detachable creates its own scope context
                Context = new ScopeContext();

                // see note below
                if (scopeFileSystems == true)
                {
                    _fscope = fileSystems.Shadow();
                }

                return;
            }

            if (parent != null)
            {
                ParentScope = parent;

                // cannot specify a different mode!
                // TODO: means that it's OK to go from L2 to None for reading purposes, but writing would be BAD!
                // this is for XmlStore that wants to bypass caches when rebuilding XML (same for NuCache)
                if (repositoryCacheMode != RepositoryCacheMode.Unspecified && parent.RepositoryCacheMode > repositoryCacheMode)
                {
                    throw new ArgumentException($"Value '{repositoryCacheMode}' cannot be lower than parent value '{parent.RepositoryCacheMode}'.", nameof(repositoryCacheMode));
                }

                // cannot specify a dispatcher!
                if (_eventDispatcher != null)
                {
                    throw new ArgumentException("Value cannot be specified on nested scope.", nameof(eventDispatcher));
                }

                // cannot specify a different fs scope!
                // can be 'true' only on outer scope (and false does not make much sense)
                if (scopeFileSystems != null && parent._scopeFileSystem != scopeFileSystems)
                {
                    throw new ArgumentException($"Value '{scopeFileSystems.Value}' be different from parent value '{parent._scopeFileSystem}'.", nameof(scopeFileSystems));
                }
            }
            else
            {
                // the FS scope cannot be "on demand" like the rest, because we would need to hook into
                // every scoped FS to trigger the creation of shadow FS "on demand", and that would be
                // pretty pointless since if scopeFileSystems is true, we *know* we want to shadow
                if (scopeFileSystems == true)
                {
                    _fscope = fileSystems.Shadow();
                }
            }
        }
Example #22
0
 public IScope CreateScope(IsolationLevel isolationLevel = IsolationLevel.Unspecified, RepositoryCacheMode repositoryCacheMode = RepositoryCacheMode.Unspecified, IEventDispatcher eventDispatcher = null, bool?scopeFileSystems = null, bool callContext = false, bool autoComplete = false)
 {
     return(_scope);
 }
Example #23
0
 public IScope CreateDetachedScope(
     IsolationLevel isolationLevel                      = IsolationLevel.Unspecified,
     RepositoryCacheMode repositoryCacheMode            = RepositoryCacheMode.Unspecified,
     IScopedNotificationPublisher notificationPublisher = null,
     bool?scopeFileSystems = null) => throw new NotImplementedException();
Example #24
0
 public IScope CreateDetachedScope(IsolationLevel isolationLevel = IsolationLevel.Unspecified, RepositoryCacheMode repositoryCacheMode = RepositoryCacheMode.Unspecified, IEventDispatcher eventDispatcher = null, bool?scopeFileSystems = null)
 {
     throw new NotImplementedException();
 }