/// <summary>
        /// This registers the classes against any public interfaces (other than InterfacesToIgnore) 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 IList <AutoRegisteredResult> AsPublicImplementedInterfaces(this AutoRegisterData autoRegData,
                                                                                 ServiceLifetime lifetime = ServiceLifetime.Transient)
        {
            if (autoRegData == null)
            {
                throw new ArgumentNullException(nameof(autoRegData));
            }

            //This lists all the ignored interfaces
            var result = autoRegData.InterfacesToIgnore.Select(x =>
                                                               new AutoRegisteredResult(null, x, ServiceLifetime.Singleton))
                         .ToList();

            foreach (var classType in autoRegData.TypesToConsider)
            {
                if (classType.IsMultipleLifetime())
                {
                    throw new ArgumentException($"Class {classType.FullName} has multiple life time attributes");
                }

                var interfaces = classType.GetTypeInfo().ImplementedInterfaces;
                foreach (var infc in interfaces.Where(i =>
                                                      !autoRegData.InterfacesToIgnore.Contains(i) && //This will not register the class with this interface
                                                      i.IsPublic && !i.IsNested))
                {
                    var lifetimeForClass = classType.GetLifetimeForClass(lifetime);
                    autoRegData.Services.Add(new ServiceDescriptor(infc, classType, lifetimeForClass));
                    result.Add(new AutoRegisteredResult(classType, infc, lifetimeForClass));
                }
            }

            return(result);
        }
Exemplo n.º 2
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)))
            {
                if (classType.IsMultipleLifetime())
                {
                    throw new ArgumentException($"Class {classType.FullName} has multiple life time attributes");
                }

                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, classType.GetLifetimeForClass(lifetime)));
                }
            }

            return(autoRegData.Services);
        }
 /// <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"
 /// NOTE: You can have multiple calls to this method to apply a series off filters
 /// </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));
     }
     return(new AutoRegisterData(autoRegData.Services, autoRegData.TypesToConsider.Where(predicate)));
 }
 /// <summary>
 /// This allows you to state that the given interface will not be registered against a class.
 /// Useful if a class has a interface that you don't want registered against a class.
 /// NOTE: the <see cref="IDisposable"/> and <see cref="ISerializable"/> interfaces are automatically ignored
 /// </summary>
 /// <typeparam name="TInterface">interface to be ignored</typeparam>
 /// <param name="autoRegData"></param>
 /// <returns></returns>
 public static AutoRegisterData IgnoreThisInterface <TInterface>(this AutoRegisterData autoRegData)
 {
     if (!typeof(TInterface).IsInterface)
     {
         throw new InvalidOperationException($"The provided {typeof(TInterface).Name} mus be an interface");
     }
     autoRegData.InterfacesToIgnore.Add(typeof(TInterface));
     return(autoRegData);
 }
Exemplo n.º 5
0
        public static IServiceCollection AsSelf(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)))
            {
                autoRegData.Services.Add(new ServiceDescriptor(classType, classType, lifetime));
            }

            return(autoRegData.Services);
        }
Exemplo n.º 6
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);
        }