Exemple #1
0
        /// <inheritdoc/>
        public virtual IRepository CreateRepository(Type entityType, Type keyType, ApplicationModelType modelType)
        {
            if (entityType == null)
            {
                throw new ArgumentNullException(nameof(entityType));
            }
            if (keyType == null)
            {
                throw new ArgumentNullException(nameof(keyType));
            }
            var pluginName = modelType switch
            {
                ApplicationModelType.WriteModel => this.Options.Persistence.DefaultWriteModelRepository?.PluginName,
                ApplicationModelType.ReadModel => this.Options.Persistence.DefaultReadModelRepository?.PluginName,
                _ => throw new NotSupportedException($"The specified {nameof(ApplicationModelType)} '{modelType}' is not supported")
            };

            if (this.Options.Persistence.Repositories.TryGetValue(entityType.FullName !, out var repositoryOptions))
            {
                pluginName = repositoryOptions.PluginName;
            }
            if (string.IsNullOrWhiteSpace(pluginName) ||
                !this.PluginManager.TryGetPlugin <IRepositoryPlugin>(pluginName, out var factory))
            {
                return((IRepository)ActivatorUtilities.CreateInstance(this.ServiceProvider, typeof(DistributedCacheRepository <,>).MakeGenericType(entityType, keyType)));
            }
            return(factory.CreateRepository(entityType, keyType));
        }
    }
 internal static IServiceCollection AddRepositories(this IServiceCollection services, IEnumerable <Type> entityTypes, ServiceLifetime lifetime, ApplicationModelType modelType)
 {
     foreach (Type entityType in entityTypes)
     {
         services.AddRepository(entityType, lifetime, modelType);
     }
     return(services);
 }
        /// <summary>
        /// Creates a new <see cref="IRepository"/>
        /// </summary>
        /// <param name="factory">The extended <see cref="IRepositoryFactory"/></param>
        /// <param name="entityType">The type of entity to create the <see cref="IRepository"/> for</param>
        /// <param name="modelType">The type of the application model to create a new <see cref="IRepository"/> for</param>
        /// <returns>A new <see cref="IRepository"/></returns>
        public static IRepository CreateRepository(this IRepositoryFactory factory, Type entityType, ApplicationModelType modelType)
        {
            if (entityType == null)
            {
                throw new ArgumentNullException(nameof(entityType));
            }
            var keyType = entityType.GetGenericType(typeof(IIdentifiable <>)).GetGenericArguments()[0];

            return(factory.CreateRepository(entityType, keyType, modelType));
        }
        static IServiceCollection AddRepository(this IServiceCollection services, Type entityType, ServiceLifetime lifetime, ApplicationModelType modelType)
        {
            var keyType = entityType.GetGenericType(typeof(IIdentifiable <>)).GetGenericArguments()[0];
            var genericRepositoryType = typeof(IRepository <,>).MakeGenericType(entityType, keyType);

            services.Add(new(genericRepositoryType, provider => provider.GetRequiredService <IRepositoryFactory>().CreateRepository(entityType, modelType), lifetime));
            services.Add(new(typeof(IRepository <>).MakeGenericType(entityType), provider => provider.GetRequiredService(genericRepositoryType), lifetime));
            return(services);
        }