Example #1
0
        public async Task <TDocument> GetOrCreateMutableAsync(Func <Task <TDocument> > factoryAsync = null)
        {
            TDocument document = null;

            if (!_isVolatile)
            {
                document = await DocumentStore.GetOrCreateMutableAsync(factoryAsync);

                if (_memoryCache.TryGetValue <TDocument>(_options.CacheKey, out var cached) && document == cached)
                {
                    throw new InvalidOperationException("Can't load for update a cached object");
                }
            }
            else
            {
                var volatileCache = ShellScope.Get <TDocument>(typeof(TDocument));
                if (volatileCache != null)
                {
                    document = volatileCache;
                }
                else
                {
                    document = await GetFromDistributedCacheAsync()
                               ?? await(factoryAsync?.Invoke() ?? Task.FromResult((TDocument)null))
                               ?? new TDocument();

                    ShellScope.Set(typeof(TDocument), document);
                }
            }

            document.Identifier = IdGenerator.GenerateId();

            return(document);
        }
        /// <inheritdoc />
        public async Task <T> GetOrCreateMutableAsync <T>(Func <Task <T> > factoryAsync = null) where T : class, new()
        {
            var loaded = ShellScope.Get <T>(typeof(T));

            if (loaded != null)
            {
                return(loaded);
            }

            var document = await GetDocumentAsync <T>()
                           ?? await(factoryAsync?.Invoke() ?? Task.FromResult((T)null))
                           ?? new T();

            ShellScope.Set(typeof(T), document);

            return(document);
        }