/// <summary>
        /// Use JsonFileRepository <typeparamref name="Repository"/> as the repository for <paramref name="request"/>.
        /// </summary>
        /// <remarks>
        /// Matches the Model to a DbModel by name.
        /// </remarks>
        /// <typeparam name="Repository"></typeparam>
        /// <param name="request"></param>
        /// <returns></returns>
        public static Type UseDbContext <Context>(this ModelRepositoryMapRequest request, Action <DbContextOptionsBuilder> optionsAction = null)
            where Context : DbContext
        {
            if (optionsAction == null)
            {
                request.Services.AddDbContext <Context>();
            }
            else
            {
                request.Services.AddDbContext <Context>(options => optionsAction(options));
            }

            // Lookup a Db Model type by trying to match by name from the context's properties.
            var dbSetProperty = typeof(Context).GetProperties()
                                .Where(item => item.PropertyType.IsGenericType && item.PropertyType.GetGenericTypeDefinition() == typeof(DbSet <>))
                                .FirstOrDefault(item => item.PropertyType.GetGenericArguments().First().Name == request.ModelType.Name);

            // If we couldn't find a match then return doing nothing as this is as far as we can go.
            if (dbSetProperty == null)
            {
                return(null);
            }

            // If we found a set, extract is type as our db model.
            var dbModelType = dbSetProperty.PropertyType.GetGenericArguments().First();


            // Create a repository class with the right types look up the DbSet's model type by name.
            var ret = typeof(DbContextRepository <, ,>).MakeGenericType(request.ModelType, typeof(Context), dbModelType);

            return(ret);
        }
Exemple #2
0
        /// <summary>
        /// Use JsonFileRepository <typeparamref name="Repository"/> as the repository for <paramref name="request"/>.
        /// </summary>
        /// <typeparam name="Repository"></typeparam>
        /// <param name="request"></param>
        /// <returns></returns>
        public static Type UseJsonFileStore(this ModelRepositoryMapRequest request, Action <JsonFileRepositoryStoreOptions> setupAction)
        {
            request.Services.AddJsonFileRepositoriesStore();

            var ret = typeof(JsonFileRepository <>).MakeGenericType(request.ModelType); // TODO cope with primary key types other than Guid.

            if (setupAction != null)
            {
                request.Services.Configure(setupAction);
            }

            return(ret);
        }
        /// <summary>
        /// Use JsonFileRepository <typeparamref name="Repository"/> as the repository for <paramref name="request"/>.
        /// </summary>
        /// <typeparam name="Repository"></typeparam>
        /// <param name="request"></param>
        /// <returns></returns>
        public static Type UseDbContext <Context, DbModel>(this ModelRepositoryMapRequest request, Action <DbContextOptionsBuilder> optionsAction = null)
            where Context : DbContext
        {
            if (optionsAction == null)
            {
                request.Services.AddDbContext <Context>();
            }
            else
            {
                request.Services.AddDbContext <Context>(options => optionsAction(options));
            }

            // Create a repository class with the right types look up the DbSet's model type by name.
            var ret = typeof(DbContextRepository <, ,>).MakeGenericType(request.ModelType, typeof(Context), typeof(DbModel));

            return(ret);
        }
Exemple #4
0
 /// <summary>
 /// Use JsonFileRepository <typeparamref name="Repository"/> as the repository for <paramref name="request"/>.
 /// </summary>
 /// <typeparam name="Repository"></typeparam>
 /// <param name="request"></param>
 /// <returns></returns>
 public static Type UseJsonFileStore(this ModelRepositoryMapRequest request)
 {
     return(UseJsonFileStore(request, setupAction: null));
 }
 /// <summary>
 /// Use JsonFileRepository <typeparamref name="Repository"/> as the repository for <paramref name="request"/>.
 /// </summary>
 /// <typeparam name="Repository"></typeparam>
 /// <param name="request"></param>
 /// <returns></returns>
 public static Type UseDbContext <Context, DbModel>(this ModelRepositoryMapRequest request)
     where Context : DbContext
 {
     return(UseDbContext <Context, DbModel>(request, optionsAction: null));
 }
Exemple #6
0
        /// <summary>
        /// Use MemoryRepository <typeparamref name="Repository"/> as the repository for <paramref name="request"/>.
        /// </summary>
        /// <typeparam name="Repository"></typeparam>
        /// <param name="request"></param>
        /// <returns></returns>
        public static Type UseMemoryStore(this ModelRepositoryMapRequest request)
        {
            request.Services.AddMemoryRepositoryStore();

            return(typeof(MemoryRepository <>).MakeGenericType(request.ModelType)); // TODO cope with primary key types other than Guid.
        }
Exemple #7
0
 /// <summary>
 /// Use <typeparamref name="Repository"/> as the repository for <paramref name="request"/>.
 /// </summary>
 /// <typeparam name="Repository"></typeparam>
 /// <param name="request"></param>
 /// <returns></returns>
 public static Type Use <Repository>(this ModelRepositoryMapRequest request)
 {
     return(typeof(Repository));
 }