Пример #1
0
        /// <summary>
        /// This extension method injects all modules dependencies to <see cref="IServiceCollection"/> by dynamically module instantiation
        /// </summary>
        /// <typeparam name="TModule">Realization type of <see cref="IModule"/></typeparam>
        /// <param name="serviceCollection">Collections of DI services</param>
        /// <returns>Collections of DI services <see cref="IServiceCollection"/></returns>
        public static IServiceCollection AddModule <TModule>(this IServiceCollection serviceCollection)
            where TModule : IModule
        {
            var moduleType = typeof(TModule);

            ExceptionsHelper.ThrowIfNotRelevantConstructor(moduleType);

            var module = (IModule)Activator.CreateInstance(moduleType);

            module.Load(serviceCollection);

            return(serviceCollection);
        }
Пример #2
0
        /// <summary>
        /// This extension method scans all modules for assembly, injects its to <see cref="IServiceCollection"/> by dynamically module instantiation
        /// </summary>
        /// <param name="serviceCollection">Collections of DI services</param>
        /// <param name="scanAssembly">Assembly for scanning modules</param>
        /// <returns>Collections of DI services <see cref="IServiceCollection"/></returns>
        public static IServiceCollection AddModules(this IServiceCollection serviceCollection, Assembly scanAssembly)
        {
            if (scanAssembly == null)
            {
                throw new ArgumentNullException(nameof(scanAssembly));
            }

            var moduleTypes = scanAssembly
                              .GetTypes()
                              .Where(type => type.GetInterfaces().Any(interfaceType => interfaceType == typeof(IModule)));

            foreach (var moduleType in moduleTypes)
            {
                ExceptionsHelper.ThrowIfNotRelevantConstructor(moduleType);

                var module = (IModule)Activator.CreateInstance(moduleType);

                module.Load(serviceCollection);
            }

            return(serviceCollection);
        }