Пример #1
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)));
 }
Пример #2
0
        /// <summary>
        /// This registers the classes against any public class (other than IDisposable) implemented by the class as a Singleton instance
        /// </summary>
        /// <param name="autoRegData">AutoRegister data produced by <see cref="RegisterAssemblyPublicNonGenericClasses"/></param> method
        /// <returns></returns>
        public static IServiceCollection AsSingletonPublicImplementedClasses(this AutoRegisterData autoRegData)
        {
            if (autoRegData == null)
            {
                throw new ArgumentNullException(nameof(autoRegData));
            }
            foreach (var classType in (autoRegData.TypeFilter == null
                ? autoRegData.TypesToConsider
                : autoRegData.TypesToConsider.Where(autoRegData.TypeFilter)))
            {
                autoRegData.Services.AddSingleton(classType);
            }

            return(autoRegData.Services);
        }
Пример #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;
                foreach (var infc in interfaces.Where(i => i != typeof(IDisposable) && i.IsPublic && !i.IsNested))
                {
                    autoRegData.Services.Add(new ServiceDescriptor(infc, classType, lifetime));
                }
            }

            return(autoRegData.Services);
        }