protected override IQueryable <TEntity> AsQueryable <TEntity>(IFetchQueryStrategy <TEntity> fetchStrategy)
 {
     return(_context
            .Set <TEntity>()
            .AsQueryable()
            .ApplyFetchingOptions(Conventions, fetchStrategy));
 }
        /// <summary>
        /// Finds an entity with the given primary key values in the repository.
        /// </summary>
        /// <typeparam name="TEntity">The type of the of the entity.</typeparam>
        /// <param name="fetchStrategy">Defines the child objects that should be retrieved when loading the entity.</param>
        /// <param name="keyValues">The values of the primary key for the entity to be found.</param>
        /// <returns>The entity found in the repository.</returns>
        public override TEntity Find <TEntity>(IFetchQueryStrategy <TEntity> fetchStrategy, params object[] keyValues)
        {
            Guard.NotEmpty(keyValues, nameof(keyValues));

            if (fetchStrategy == null)
            {
                var entityType = typeof(TEntity);
                var store      = InMemoryCache.Instance.GetDatabaseStore(DatabaseName);

                if (!store.ContainsKey(entityType))
                {
                    return(default(TEntity));
                }

                var key = Combine(keyValues);

                store[entityType].TryGetValue(key, out object entity);

                var result = (TEntity)Convert.ChangeType(entity, entityType);

                return(result);
            }

            return(base.Find(fetchStrategy, keyValues));
        }
Пример #3
0
        /// <summary>
        /// Finds an entity with the given primary key values in the repository.
        /// </summary>
        /// <typeparam name="TEntity">The type of the of the entity.</typeparam>
        /// <param name="fetchStrategy">Defines the child objects that should be retrieved when loading the entity</param>
        /// <param name="keyValues">The values of the primary key for the entity to be found.</param>
        /// <returns>The entity found in the repository.</returns>
        public IQueryResult <TEntity> Find <TEntity>(IFetchQueryStrategy <TEntity> fetchStrategy, params object[] keyValues) where TEntity : class
        {
            Guard.NotEmpty(keyValues, nameof(keyValues));

            var options = new QueryOptions <TEntity>()
                          .Include(Conventions.GetByPrimaryKeySpecification <TEntity>(keyValues));

            if (fetchStrategy != null)
            {
                options.Include(fetchStrategy);
            }

            var selectorFunc = IdentityFunction <TEntity> .Instance;

            QueryBuilder.CreateSelectStatement <TEntity>(
                Conventions,
                options,
                fetchStrategy != null,
                out var sql,
                out var parameters,
                out var navigationProperties,
                out var getPropertyFromColumnAliasCallback);

            var mapper = new Mapper <TEntity>(Conventions, navigationProperties, getPropertyFromColumnAliasCallback);

            ExecuteSchemaValidate(typeof(TEntity));

            return(_dbHelper.ExecuteObject <TEntity>(sql, parameters, reader => mapper.Map <TEntity>(reader, selectorFunc)));
        }
Пример #4
0
        private static string FormatGetOrSetFindKey <T>([NotNull] object[] keys, [CanBeNull] IFetchQueryStrategy <T> fetchStrategy)
        {
            Guard.NotEmpty(keys, nameof(keys));

            var f = FormatFetchQueryStrategy(fetchStrategy);

            return($"GetOrSetFind<{Name<T>()}>: [ \n\tKeys = {string.Join(", ", keys.Select(x => x.ToString()).ToArray())},\n\t{f} ]");
        }
        /// <summary>
        /// Returns the fetch strategy, or a default valued if the sequence is empty.
        /// </summary>
        /// <typeparam name="T">The type of the entity.</typeparam>
        /// <param name="source">The source.</param>
        /// <param name="conventions">The configurable conventions.</param>
        /// <returns>The fetching strategy.</returns>
        public static IFetchQueryStrategy <T> DefaultIfFetchStrategyEmpty <T>([CanBeNull] this IFetchQueryStrategy <T> source, [NotNull] IRepositoryConventions conventions)
        {
            Guard.NotNull(conventions, nameof(conventions));

            return(source != null && source.PropertyPaths.Any()
                ? source
                : FetchQueryStrategy <T> .Default(conventions));
        }
        /// <summary>
        /// Specifies the related objects to include in the query results.
        /// </summary>
        /// <param name="path">The dot-separated list of related objects to return in the query results.</param>
        /// <returns>The current instance.</returns>
        public QueryOptions <T> Fetch([NotNull] string path)
        {
            Guard.NotEmpty(path, nameof(path));

            _fetchStrategy = _fetchStrategy ?? new FetchQueryStrategy <T>();

            _fetchStrategy.Fetch(path);

            return(this);
        }
        public override TEntity Find <TEntity>(IFetchQueryStrategy <TEntity> fetchStrategy, params object[] keyValues) where TEntity : class
        {
            Guard.NotEmpty(keyValues, nameof(keyValues));

            var key           = string.Join(":", keyValues);
            var blobContainer = GetBlobContainer <TEntity>();
            var result        = DownloadEntity <TEntity>(blobContainer, key);

            return(result);
        }
Пример #8
0
 internal static Task <ICacheQueryResult <T> > GetOrSetFindAsync <T>(
     [NotNull] this ICacheProvider cacheProvider,
     [NotNull] object[] keys,
     [CanBeNull] IFetchQueryStrategy <T> fetchStrategy,
     [NotNull] Func <Task <T> > getter,
     [NotNull] ILogger logger)
 => GetOrSetAsync <T, T>(
     cacheProvider,
     FormatGetOrSetFindKey <T>(keys, fetchStrategy),
     getter,
     logger);
        /// <summary>
        /// Includes the fetch strategy which defines the child objects that should be retrieved when loading the entity and combines it with the current property pahts collection.
        /// </summary>
        /// <param name="fetchStrategy">Defines the child objects that should be retrieved when loading the entity.</param>
        /// <returns>The current instance.</returns>
        public QueryOptions <T> Include([NotNull] IFetchQueryStrategy <T> fetchStrategy)
        {
            Guard.NotNull(fetchStrategy, nameof(fetchStrategy));

            var paths       = _fetchStrategy != null ? ((IFetchQueryStrategy <T>)_fetchStrategy).PropertyPaths : new List <string>();
            var mergedPaths = paths.Union(fetchStrategy.PropertyPaths).ToList();

            _fetchStrategy = _fetchStrategy ?? new FetchQueryStrategy <T>();

            mergedPaths.ForEach(path => _fetchStrategy.Fetch(path));

            return(this);
        }
        public override TEntity Find <TEntity>(IFetchQueryStrategy <TEntity> fetchStrategy, params object[] keyValues)
        {
            Guard.NotEmpty(keyValues, nameof(keyValues));

            if (fetchStrategy == null)
            {
                var result = _context.Set <TEntity>().Find(keyValues);

                return(result);
            }

            return(base.Find(fetchStrategy, keyValues));
        }
Пример #11
0
        /// <summary>
        /// Finds an entity with the given primary key values in the repository.
        /// </summary>
        /// <typeparam name="TEntity">The type of the of the entity.</typeparam>
        /// <param name="fetchStrategy">Defines the child objects that should be retrieved when loading the entity.</param>
        /// <param name="keyValues">The values of the primary key for the entity to be found.</param>
        /// <returns>The entity found in the repository.</returns>
        public virtual TEntity Find <TEntity>([CanBeNull] IFetchQueryStrategy <TEntity> fetchStrategy, [NotNull] params object[] keyValues) where TEntity : class
        {
            Guard.NotEmpty(keyValues, nameof(keyValues));

            var options = new QueryOptions <TEntity>()
                          .WithFilter(Conventions.GetByPrimaryKeySpecification <TEntity>(keyValues));

            var query = AsEnumerable(fetchStrategy);

            var result = query
                         .ApplySpecificationOptions(options)
                         .FirstOrDefault();

            return(result);
        }
        public override TEntity Find <TEntity>(IFetchQueryStrategy <TEntity> fetchStrategy, params object[] keyValues)
        {
            Guard.NotEmpty(keyValues, nameof(keyValues));

            if (fetchStrategy == null)
            {
                if (_db.TryFind <TEntity>(keyValues, out object entity))
                {
                    return((TEntity)Convert.ChangeType(entity, typeof(TEntity)));
                }

                return(default(TEntity));
            }

            return(base.Find(fetchStrategy, keyValues));
        }
        /// <summary>
        /// Finds an entity with the given primary key values in the repository.
        /// </summary>
        /// <typeparam name="TEntity">The type of the of the entity.</typeparam>
        /// <param name="fetchStrategy">Defines the child objects that should be retrieved when loading the entity.</param>
        /// <param name="keyValues">The values of the primary key for the entity to be found.</param>
        /// <returns>The entity found in the repository.</returns>
        public override TEntity Find <TEntity>(IFetchQueryStrategy <TEntity> fetchStrategy, params object[] keyValues)
        {
            Guard.NotEmpty(keyValues, nameof(keyValues));

            try
            {
                var blob = GetBlobkBlobReference(keyValues);

                return(Deserialize <TEntity>(blob.DownloadText()));
            }
            catch (StorageException storageException)
            {
                if (storageException.RequestInformation.HttpStatusCode == 404)
                {
                    return(default(TEntity));
                }

                throw;
            }
        }
Пример #14
0
        /// <summary>
        /// Finds an entity with the given primary key values in the repository.
        /// </summary>
        /// <typeparam name="TEntity">The type of the of the entity.</typeparam>
        /// <param name="fetchStrategy">Defines the child objects that should be retrieved when loading the entity.</param>
        /// <param name="keyValues">The values of the primary key for the entity to be found.</param>
        /// <returns>The entity found in the repository.</returns>
        public virtual TEntity Find <TEntity>([CanBeNull] IFetchQueryStrategy <TEntity> fetchStrategy, [NotNull] params object[] keyValues) where TEntity : class
        {
            Guard.NotEmpty(keyValues, nameof(keyValues));

            var options = new QueryOptions <TEntity>()
                          .Include(Conventions.GetByPrimaryKeySpecification <TEntity>(keyValues));

            var query = AsQueryable <TEntity>();

            if (fetchStrategy != null)
            {
                query = ApplyFetchingOptions(query, options.Include(fetchStrategy));
            }

            var result = query
                         .ApplySpecificationOptions(options)
                         .FirstOrDefault();

            return(result);
        }
Пример #15
0
        /// <summary>
        /// Finds an entity with the given primary key values in the repository.
        /// </summary>
        /// <typeparam name="TEntity">The type of the of the entity.</typeparam>
        /// <param name="fetchStrategy">Defines the child objects that should be retrieved when loading the entity.</param>
        /// <param name="keyValues">The values of the primary key for the entity to be found.</param>
        /// <returns>The entity found in the repository.</returns>
        public override TEntity Find <TEntity>(IFetchQueryStrategy <TEntity> fetchStrategy, params object[] keyValues)
        {
            Guard.NotEmpty(keyValues, nameof(keyValues));

            ThrowsIfNotTableEntity <TEntity>();

            string partitionKey = string.Empty;
            string rowKey;

            if (keyValues.Length == 1)
            {
                rowKey = keyValues[0].ToString();
            }
            else
            {
                partitionKey = keyValues[0].ToString();
                rowKey       = keyValues[1].ToString();
            }

            var opertation  = InvokeRetrieve <TEntity>(partitionKey, rowKey);
            var tableResult = Table.Execute(opertation);

            return(tableResult.Result as TEntity);
        }
Пример #16
0
 /// <summary>
 /// Returns the entity's query.
 /// </summary>
 /// <typeparam name="TEntity">The type of the of the entity.</typeparam>
 /// <returns>The entity's query.</returns>
 protected abstract IQueryable <TEntity> AsQueryable <TEntity>(IFetchQueryStrategy <TEntity> fetchStrategy) where TEntity : class;
 public TEntity Get(TKey1 key1, TKey2 key2, TKey3 key3, IFetchQueryStrategy <TEntity> fetchStrategy)
 {
     return(_underlyingService.Get(key1, key2, key3, fetchStrategy));
 }
 protected override IEnumerable <TEntity> AsEnumerable <TEntity>(IFetchQueryStrategy <TEntity> fetchStrategy)
 {
     return(DownloadEntities <TEntity>());
 }
        public async Task <TEntity> FindAsync <TEntity>(CancellationToken cancellationToken, IFetchQueryStrategy <TEntity> fetchStrategy, params object[] keyValues) where TEntity : class
        {
            Guard.NotEmpty(keyValues, nameof(keyValues));

            if (fetchStrategy == null)
            {
                return(await _context.Set <TEntity>().FindAsync(keyValues, cancellationToken).ConfigureAwait(false));
            }

            var options = new QueryOptions <TEntity>()
                          .WithFilter(Conventions.GetByPrimaryKeySpecification <TEntity>(keyValues));

            var query = AsQueryable(fetchStrategy);

            var result = await query
                         .ApplySpecificationOptions(options)
                         .FirstOrDefaultAsync(cancellationToken)
                         .ConfigureAwait(false);

            return(result);
        }
        public async Task <TEntity> FindAsync <TEntity>(CancellationToken cancellationToken, IFetchQueryStrategy <TEntity> fetchStrategy, params object[] keyValues) where TEntity : class
        {
            Guard.NotEmpty(keyValues, nameof(keyValues));

            var key           = string.Join(":", keyValues);
            var blobContainer = await GetBlobContainerAsync <TEntity>().ConfigureAwait(false);

            var result = await DownloadEntityAsync <TEntity>(blobContainer, key, cancellationToken).ConfigureAwait(false);

            return(result);
        }
Пример #21
0
 /// <summary>
 /// Returns the entity's query.
 /// </summary>
 /// <typeparam name="TEntity">The type of the of the entity.</typeparam>
 /// <returns>The entity's query.</returns>
 protected abstract IEnumerable <TEntity> AsEnumerable <TEntity>([CanBeNull] IFetchQueryStrategy <TEntity> fetchStrategy) where TEntity : class;
        /// <summary>
        /// Apply a fetching options to the specified entity's query.
        /// </summary>
        /// <returns>The entity's query with the applied options.</returns>
        public static IEnumerable <T> ApplyFetchingOptions <T>([NotNull] this IEnumerable <T> query, [CanBeNull] IFetchQueryStrategy <T> fetchStrategy, [NotNull] Func <Type, IEnumerable <object> > innerQueryCallback) where T : class
        {
            Guard.NotNull(query, nameof(query));
            Guard.NotNull(innerQueryCallback, nameof(innerQueryCallback));

            var fetchingPaths = fetchStrategy.DefaultIfFetchStrategyEmpty().PropertyPaths.ToList();
            var fetchHelper   = new FetchHelper(innerQueryCallback);

            if (fetchingPaths.Any())
            {
                query = fetchingPaths.NormalizePropertyPaths().Aggregate(query, (current, path) => fetchHelper.Include(current, path));
            }

            return(query);
        }
Пример #23
0
        /// <summary>
        /// Asynchronously finds an entity with the given primary key values in the repository.
        /// </summary>
        /// <typeparam name="TEntity">The type of the of the entity.</typeparam>
        /// <param name="cancellationToken">A <see cref="T:System.Threading.CancellationToken" /> to observe while waiting for the task to complete.</param>
        /// <param name="fetchStrategy">Defines the child objects that should be retrieved when loading the entity</param>
        /// <param name="keyValues">The values of the primary key for the entity to be found.</param>
        /// <returns>The <see cref="T:System.Threading.Tasks.Task" /> that represents the asynchronous operation, containing the entity found in the repository.</returns>
        public override async Task <IQueryResult <TEntity> > FindAsync <TEntity>(CancellationToken cancellationToken, IFetchQueryStrategy <TEntity> fetchStrategy, params object[] keyValues)
        {
            Guard.NotEmpty(keyValues, nameof(keyValues));

            if (fetchStrategy == null)
            {
                var result = await _context.Set <TEntity>().FindAsync(keyValues, cancellationToken);

                return(new QueryResult <TEntity>(result));
            }

            return(await base.FindAsync(cancellationToken, fetchStrategy, keyValues));
        }
        public static IQueryable <T> ApplyFetchingOptions <T>(this IQueryable <T> query, IRepositoryConventions conventions, IFetchQueryStrategy <T> fetchStrategy) where T : class
        {
            Guard.NotNull(query, nameof(query));
            Guard.NotNull(conventions, nameof(conventions));

            var fetchingPaths = fetchStrategy.DefaultIfFetchStrategyEmpty(conventions).PropertyPaths.ToList();

            if (fetchingPaths.Any())
            {
                query = fetchingPaths.Aggregate(query, (current, path) => current.Include(path));
            }

            return(query);
        }
Пример #25
0
 public TEntity Find <TEntity>(IFetchQueryStrategy <TEntity> fetchStrategy, params object[] keyValues) where TEntity : class
 {
     return(_underlyingContext.Find <TEntity>(fetchStrategy, keyValues));
 }
 public Task <TEntity> FindAsync(TKey1 key1, TKey2 key2, TKey3 key3, IFetchQueryStrategy <TEntity> fetchStrategy, CancellationToken cancellationToken = new CancellationToken())
 {
     return(_underlyingRepo.FindAsync(key1, key2, key3, fetchStrategy, cancellationToken));
 }
Пример #27
0
 private static string FormatFetchQueryStrategy <T>([CanBeNull] IFetchQueryStrategy <T> fetchStrategy)
 {
     return(fetchStrategy != null?fetchStrategy.ToString() : $"FetchQueryStrategy<{Name<T>()}>: [ null ]");
 }
 public TEntity Find(TKey1 key1, TKey2 key2, TKey3 key3, IFetchQueryStrategy <TEntity> fetchStrategy)
 {
     return(_underlyingRepo.Find(key1, key2, key3, fetchStrategy));
 }
 /// <summary>
 /// Finds an entity with the given primary key values in the repository.
 /// </summary>
 /// <typeparam name="TEntity">The type of the of the entity.</typeparam>
 /// <param name="fetchStrategy">Defines the child objects that should be retrieved when loading the entity</param>
 /// <param name="keyValues">The values of the primary key for the entity to be found.</param>
 /// <returns>The entity found in the repository.</returns>
 public IQueryResult <TEntity> Find <TEntity>(IFetchQueryStrategy <TEntity> fetchStrategy, params object[] keyValues) where TEntity : class
 {
     return(_context.Find <TEntity>(fetchStrategy, keyValues));
 }
        /// <summary>
        /// Asynchronously finds an entity with the given primary key values in the repository.
        /// </summary>
        /// <typeparam name="TEntity">The type of the of the entity.</typeparam>
        /// <param name="cancellationToken">A <see cref="System.Threading.CancellationToken" /> to observe while waiting for the task to complete.</param>
        /// <param name="keyValues">The values of the primary key for the entity to be found.</param>
        /// <param name="fetchStrategy">Defines the child objects that should be retrieved when loading the entity</param>
        /// <returns>The <see cref="System.Threading.Tasks.Task" /> that represents the asynchronous operation, containing the entity found in the repository.</returns>
        public Task <IQueryResult <TEntity> > FindAsync <TEntity>(CancellationToken cancellationToken, IFetchQueryStrategy <TEntity> fetchStrategy, params object[] keyValues) where TEntity : class
        {
            if (_context is IRepositoryContextAsync contextAsync)
            {
                return(contextAsync.FindAsync(cancellationToken, fetchStrategy, keyValues));
            }

            return(RunAsync <IQueryResult <TEntity> >(() => Find(fetchStrategy, keyValues), cancellationToken));
        }