Ejemplo n.º 1
0
        /// <summary>
        /// This finds all the public, non-generic, non-nested classes in an assembly
        /// in the provided assemblies
        /// </summary>
        /// <param name="services">the NET Core dependency injection service</param>
        /// <param name="assemblies">Each assembly you want scanned </param>
        /// <returns></returns>
        public static AutoRegisterData RegisterAssemblyPublicNonGenericClasses(this IServiceCollection services, params Assembly[] assemblies)
        {
            var allPublicTypes = assemblies.SelectMany(x => x.GetExportedTypes()
                                                       .Where(y => y.IsClass && !y.IsAbstract && !y.IsGenericType && !y.IsNested));
            var registerAssemblyPublicNonGenericClasses = new AutoRegisterData(services, allPublicTypes);

            return(registerAssemblyPublicNonGenericClasses);
        }
Ejemplo n.º 2
0
 /// <summary>
 /// This allows you to filter the classes in some way.
 /// For instance <code>Where(c =\> c.Name.EndsWith("Service")</code> would only register classes who's name ended in "Service"
 /// </summary>
 /// <param name="autoRegData"></param>
 /// <param name="predicate">A function that will take a type and return true if that type should be included</param>
 /// <returns></returns>
 public static AutoRegisterData Where(this AutoRegisterData autoRegData, Func <Type, bool> predicate)
 {
     if (autoRegData == null)
     {
         throw new ArgumentNullException(nameof(autoRegData));
     }
     autoRegData.TypeFilter = predicate;
     return(new AutoRegisterData(autoRegData.Services, autoRegData.TypesToConsider.Where(predicate)));
 }
Ejemplo n.º 3
0
        /// <summary>
        /// This registers the classes against any public interfaces (other than IDisposable) implemented by the class
        /// </summary>
        /// <param name="autoRegData">AutoRegister data produced by <see cref="RegisterAssemblyPublicNonGenericClasses"/></param> method
        /// <param name="lifetime">Allows you to define the lifetime of the service - defaults to ServiceLifetime.Transient</param>
        /// <returns></returns>
        public static IServiceCollection AsPublicImplementedInterfaces(this AutoRegisterData autoRegData,
                                                                       ServiceLifetime lifetime = ServiceLifetime.Transient)
        {
            if (autoRegData == null)
            {
                throw new ArgumentNullException(nameof(autoRegData));
            }
            foreach (var classType in (autoRegData.TypeFilter == null
                ? autoRegData.TypesToConsider
                : autoRegData.TypesToConsider.Where(autoRegData.TypeFilter)))
            {
                var interfaces = classType.GetTypeInfo().ImplementedInterfaces
                                 .Where(i => i != typeof(IDisposable) && (i.IsPublic));
                foreach (var infc in interfaces)
                {
                    autoRegData.Services.Add(new ServiceDescriptor(infc, classType, lifetime));
                }
            }

            return(autoRegData.Services);
        }