コード例 #1
0
        /// <summary>
        /// Registers modules found in an assembly.
        /// </summary>
        /// <param name="registrar">The module registrar that will make the registrations into the container.</param>
        /// <param name="moduleType">The <see cref="Type"/> of the module to add.</param>
        /// <param name="assemblies">The assemblies from which to register modules.</param>
        /// <exception cref="ArgumentNullException">
        /// Thrown if <paramref name="registrar"/> or <paramref name="moduleType"/> is <see langword="null"/>.
        /// </exception>
        /// <returns>
        /// The <see cref="IModuleRegistrar"/> to allow
        /// additional chained module registrations.
        /// </returns>
        public static IModuleRegistrar RegisterAssemblyModules(this IModuleRegistrar registrar, Type moduleType, params Assembly[] assemblies)
        {
            if (registrar == null)
            {
                throw new ArgumentNullException(nameof(registrar));
            }

            if (moduleType == null)
            {
                throw new ArgumentNullException(nameof(moduleType));
            }

            var moduleFinder = new ContainerBuilder();

            moduleFinder.RegisterAssemblyTypes(assemblies)
            .Where(t => moduleType.IsAssignableFrom(t))
            .As <IModule>();

            using (var moduleContainer = moduleFinder.Build())
            {
                foreach (var module in moduleContainer.Resolve <IEnumerable <IModule> >())
                {
                    registrar.RegisterModule(module);
                }
            }

            return(registrar);
        }
コード例 #2
0
ファイル: PackagesRegistrar.cs プロジェクト: 5l1v3r1/Maze-1
        public void Configure(ContainerBuilder builder, IEnumerable <PackageCarrier> packages)
        {
            //https://github.com/autofac/Autofac/blob/41044d7d1a4fa277c628021537d5a12016137c3b/src/Autofac/ModuleRegistrationExtensions.cs#L156
            var moduleFinder = new ContainerBuilder();

            moduleFinder.RegisterInstance(_configurationProvider.ConfigurationRoot);

            moduleFinder.RegisterAssemblyTypes(packages.Select(x => x.Assembly).ToArray())
            .Where(t => typeof(IModule).IsAssignableFrom(t)).As <IModule>();

            IModuleRegistrar registrar = null;

            using (var moduleContainer = moduleFinder.Build())
            {
                foreach (var module in moduleContainer.Resolve <IEnumerable <IModule> >())
                {
                    if (registrar == null)
                    {
                        registrar = builder.RegisterModule(module);
                    }
                    else
                    {
                        registrar.RegisterModule(module);
                    }
                }
            }
        }
コード例 #3
0
        /// <summary>
        /// Add a module to the container.
        /// </summary>
        /// <param name="registrar">The module registrar that will make the registration into the container.</param>
        /// <typeparam name="TModule">The module to add.</typeparam>
        /// <exception cref="ArgumentNullException">
        /// Thrown if <paramref name="registrar"/> is <see langword="null"/>.
        /// </exception>
        /// <returns>
        /// The <see cref="IModuleRegistrar"/> to allow
        /// additional chained module registrations.
        /// </returns>
        public static IModuleRegistrar RegisterModule <TModule>(this IModuleRegistrar registrar)
            where TModule : IModule, new()
        {
            if (registrar == null)
            {
                throw new ArgumentNullException(nameof(registrar));
            }

            return(registrar.RegisterModule(new TModule()));
        }
コード例 #4
0
        public static IModuleRegistrar RegisterEventstreamModule(
            this IModuleRegistrar builder,
            IConfiguration configuration)
        {
            var connectionString = configuration.GetConnectionString("Events");

            if (string.IsNullOrWhiteSpace(connectionString))
            {
                throw new ApplicationException("Missing 'Events' connectionstring.");
            }

            return(builder
                   .RegisterModule(new SqlStreamStoreModule(connectionString, Schema.Default))
                   .RegisterModule(new TraceSqlStreamStoreModule(configuration["DataDog:ServiceName"])));
        }
コード例 #5
0
        /// <summary>Add a module to the container.</summary>
        /// <param name="registrar">The module registrar that will make the registration into the container.</param>
        /// <typeparam name="TModule">The module to add.</typeparam>
        /// <exception cref="T:System.ArgumentNullException">
        /// Thrown if <paramref name="registrar" /> is <see langword="null" />.
        /// </exception>
        /// <returns>
        /// The <see cref="T:Autofac.Core.Registration.IModuleRegistrar" /> to allow
        /// additional chained module registrations.
        /// </returns>
        public static IModuleRegistrar SafeRegisterModule <TModule>(this IModuleRegistrar registrar)
            where TModule : IModule, new()
        {
            if (registrar == null)
            {
                throw new ArgumentNullException(nameof(registrar));
            }

            IModuleRegistrar ret;
            Type             type = typeof(TModule);

            lock (SyncLock)
            {
                // Verifica que el módulo no este registrado.
                if (!Cache.TryGetValue(type, out ret))
                {
                    ret = registrar.RegisterModule <TModule>();
                    Cache.Add(type, ret);
                }
            }

            return(ret);
        }