예제 #1
0
        /// <summary>
        /// This registers the classes against any public interfaces (other than IDisposable) implemented by the class
        /// </summary>
        /// <param name="autoRegisterData">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 autoRegisterData,
            ServiceLifetime lifetime = ServiceLifetime.Transient)
        {
            if (autoRegisterData == null)
            {
                throw new ArgumentNullException(nameof(autoRegisterData));
            }

            var classTypes = autoRegisterData.TypeFilter == null
                ? autoRegisterData.TypesToConsider
                : autoRegisterData.TypesToConsider.Where(autoRegisterData.TypeFilter);

            foreach (var classType in classTypes)
            {
                var interfaces = classType.GetTypeInfo().ImplementedInterfaces
                                 .Where(i => i != typeof(IDisposable) && i.IsPublic);

                foreach (var interInterface in interfaces)
                {
                    autoRegisterData.Services.Add(new ServiceDescriptor(interInterface, classType, lifetime));
                }
            }

            return(autoRegisterData.Services);
        }
예제 #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="autoRegisterData"></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 autoRegisterData, Func <Type, bool> predicate)
        {
            if (autoRegisterData == null)
            {
                throw new ArgumentNullException(nameof(autoRegisterData));
            }
            autoRegisterData.TypeFilter = predicate;

            return(new AutoRegisterData(autoRegisterData.Services, autoRegisterData.TypesToConsider.Where(predicate)));
        }