Ejemplo n.º 1
0
        /// <summary>
        /// Add repositories for each model class in <paramref name="assembly"/> using <paramref name="map"/> and register any IRepositoryEvents&lt;&gt; from <paramref name="assembly"/>.
        /// </summary>
        /// <param name="services"></param>
        /// <param name="assembly"></param>
        /// <param name="map"></param>
        /// <returns></returns>
        public static IServiceCollection AddRepositoriesForAssembly(this IServiceCollection services, Assembly assembly, string inNamespace, Func <ModelRepositoryMapRequest, Type> map)
        {
            services.AddModelConverter();

            var types = assembly.GetExportedTypes();

            foreach (var modelType in types)
            {
                if (!modelType.IsClass || modelType.IsAbstract)
                {
                    continue;
                }

                // Restrict by the namespace (if its been specified).
                if (!String.IsNullOrEmpty(inNamespace) && modelType.Namespace != inNamespace)
                {
                    continue;
                }

                // See if the type implements IRepositoryEvent<>.
                // If it does we add it as an IRepositoryEvent<> for use by repositories of the right model type, rather than trying to register
                // a repository treating the type like a model type.
                var eventInterfaces = modelType.GetInterfaces().Where(it => it.IsGenericType && it.GetGenericTypeDefinition() == typeof(IRepositoryEvents <>));
                if (eventInterfaces.Any())
                {
                    foreach (var eventInterface in eventInterfaces)
                    {
                        services.AddScoped(eventInterface, modelType);
                    }
                    continue;
                }

                // If we get here we are going to treat the type as model type, but if the type has no public propeties then ignore it
                // as it can't be a useful model.
                if (!modelType.GetProperties().Any())
                {
                    continue;
                }

                // Prepare a request to pass on to the map function and let our caller map the model type to a repository as a class.
                var request = new ModelRepositoryMapRequest
                {
                    Services  = services,
                    ModelType = modelType,
                };

                // Let the caller perform the mapping.
                var repositoryType = map(request);

                // If we got a repository to map to, add it to the services collection.
                if (repositoryType != null)
                {
                    AddAllRepositoryInterfaces(services, modelType, repositoryType);
                }
            }

            return(services);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Add a repository for <typeparamref name="Model"/> using <paramref name="map"/>.
        /// </summary>
        /// <typeparam name="Model"></typeparam>
        /// <param name="services"></param>
        /// <param name="map"></param>
        /// <returns></returns>
        public static IServiceCollection AddRepository <Model>(this IServiceCollection services, Func <ModelRepositoryMapRequest, Type> map)
            where Model : class, new()
        {
            services.AddModelConverter();

            var request = new ModelRepositoryMapRequest
            {
                Services  = services,
                ModelType = typeof(Model),
            };

            var repositoryType = map(request);

            if (repositoryType != null)
            {
                AddAllRepositoryInterfaces(services, typeof(Model), repositoryType);
            }

            return(services);
        }