当前位置: 首页>>代码示例>>C#>>正文


C# ContainerBuilder.RegisterGeneric方法代码示例

本文整理汇总了C#中ContainerBuilder.RegisterGeneric方法的典型用法代码示例。如果您正苦于以下问题:C# ContainerBuilder.RegisterGeneric方法的具体用法?C# ContainerBuilder.RegisterGeneric怎么用?C# ContainerBuilder.RegisterGeneric使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在ContainerBuilder的用法示例。


在下文中一共展示了ContainerBuilder.RegisterGeneric方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Load

        protected override void Load(ContainerBuilder builder)
        {
            builder.RegisterType<InMemoryDatabase>().As<IDatabase>().InstancePerHttpRequest();
            builder.RegisterGeneric(typeof(Repository<>)).As(typeof(IRepository<>));

            base.Load(builder);
        }
开发者ID:rootzhou,项目名称:Core,代码行数:7,代码来源:RegisterServices.cs

示例2: AsImplementedInterfaces_CanBeAppliedToOpenGenericRegistrations

        public void AsImplementedInterfaces_CanBeAppliedToOpenGenericRegistrations()
        {
            var builder = new ContainerBuilder();
            builder.RegisterGeneric(typeof(SelfComponent<>)).AsImplementedInterfaces();
            var context = builder.Build();

            context.Resolve<IImplementedInterface<object>>();
        }
开发者ID:arronchen,项目名称:Autofac,代码行数:8,代码来源:RegistrationExtensionsTests.cs

示例3: RegisterServices

        private static void RegisterServices(ContainerBuilder builder)
        {
            builder.Register(x => new AppDbContext())
               .As<DbContext>()
               .InstancePerRequest();

<<<<<<< HEAD
            builder.Register(x => new HttpCacheService())
               .As<ICacheService>()
               .InstancePerRequest();

            builder.Register(x => new IdentifierProvider())
               .As<IIdentifierProvider>()
               .InstancePerRequest();

            var servicesAssembly = Assembly.GetAssembly(typeof(IJokesService));
            builder.RegisterAssemblyTypes(servicesAssembly).AsImplementedInterfaces();

            builder.RegisterGeneric(typeof(DbRepository<>))
               .As(typeof(IDbRepository<>))
               .InstancePerRequest();

            builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly())
                .AssignableTo<BaseController>()
                .PropertiesAutowired();
=======
            builder.RegisterGeneric(typeof(DbRepository<>))
                .As(typeof(IDbRepository<>))
                .InstancePerRequest();

            // builder.Register(x => new HttpCacheService())
            //    .As<ICacheService>()
            //    .InstancePerRequest();
            // builder.Register(x => new IdentifierProvider())
            //    .As<IIdentifierProvider>()
            //    .InstancePerRequest();

            // var servicesAssembly = Assembly.GetAssembly(typeof(IJokesService));
            // builder.RegisterAssemblyTypes(servicesAssembly).AsImplementedInterfaces();



            // builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly())
            //    .AssignableTo<BaseController>().PropertiesAutowired();
>>>>>>> 6ca5c2744ff07e9ad93c0f5627d37f5deea149bf
        }
开发者ID:tddold,项目名称:ASP.NET-MVC-Projects,代码行数:46,代码来源:AutofacConfig.cs

示例4: RegisterGenericType

        private static void RegisterGenericType(ContainerBuilder builder, Type type, IEnumerable<IAutofacRegistrationAttribute> attributes)
        {
            var registrationBuilder = builder.RegisterGeneric(type);

            foreach (IAutofacRegistrationAttribute attribute in attributes)
            {
                registrationBuilder = attribute.Register(registrationBuilder);
            }
        }
开发者ID:dvabuzyarov,项目名称:Autofac.Extras.RegistrationAttributes,代码行数:9,代码来源:ContainerBuilderExtensions.cs

示例5: AggregateServiceGenericsFixture

        public AggregateServiceGenericsFixture()
        {
            var builder = new ContainerBuilder();
            builder.RegisterAggregateService<IOpenGenericAggregate>();
            builder.RegisterGeneric(typeof(OpenGenericImpl<>))
                .As(typeof(IOpenGeneric<>));

            this._container = builder.Build();
        }
开发者ID:markgould,项目名称:Autofac.Extras.AggregateService,代码行数:9,代码来源:AggregateServiceGenericsFixture.cs

示例6: BuildMediator

        private static IMediator BuildMediator()
        {
            var builder = new ContainerBuilder();
            builder.RegisterSource(new ContravariantRegistrationSource());
            builder.RegisterAssemblyTypes(typeof (IMediator).Assembly).AsImplementedInterfaces();
            builder.RegisterAssemblyTypes(typeof (Ping).Assembly).AsImplementedInterfaces();
            builder.RegisterGeneric(typeof (GenericPostRequestHandler<,>)).As(typeof (IPostRequestHandler<,>));
            builder.RegisterInstance(Console.Out).As<TextWriter>();

            var lazy = new Lazy<IServiceLocator>(() => new AutofacServiceLocator(builder.Build()));
            var serviceLocatorProvider = new ServiceLocatorProvider(() => lazy.Value);
            builder.RegisterInstance(serviceLocatorProvider);

            var mediator = serviceLocatorProvider().GetInstance<IMediator>();

            return mediator;
        }
开发者ID:ChrisMissal,项目名称:MediatR,代码行数:17,代码来源:Program.cs

示例7: GenericsAreTaggable

        public void GenericsAreTaggable()
        {
            var builder = new ContainerBuilder();
            builder.RegisterGeneric(typeof(List<>))
                .InstancePerMatchingLifetimeScope("tag")
                .As(typeof(IList<>));

            var outer = builder.Build();
            var inner = outer.BeginLifetimeScope("tag");

            var coll = inner.Resolve<IList<object>>();
            Assert.NotNull(coll);

            Assert.Throws<DependencyResolutionException>(() => outer.Resolve<IList<object>>());
        }
开发者ID:GitHuang,项目名称:Autofac,代码行数:15,代码来源:TagsFixture.cs

示例8: GenericsAreTaggable

        public void GenericsAreTaggable()
        {
            var builder = new ContainerBuilder();
            builder.RegisterGeneric(typeof(List<>))
                .InstancePerMatchingLifetimeScope("tag")
                .As(typeof(IList<>));

            var outer = builder.Build();
            var inner = outer.BeginLifetimeScope("tag");

            var coll = inner.Resolve<IList<object>>();
            Assert.NotNull(coll);

            var threw = false;
            try
            {
                outer.Resolve<IList<object>>();
            }
            catch (Exception)
            {
                threw = true;
            }

            Assert.True(threw);
        }
开发者ID:MyLobin,项目名称:Autofac,代码行数:25,代码来源:TagsFixture.cs

示例9: Load

        protected override void Load(ContainerBuilder builder)
        {
            bool enableLocalization = true;
            string absoluteFileName = HostingEnvironment.MapPath("~/Mvc.sitemap");
            TimeSpan absoluteCacheExpiration = TimeSpan.FromMinutes(5);
            bool visibilityAffectsDescendants = true;
            bool useTitleIfDescriptionNotProvided = true;

            bool securityTrimmingEnabled = false;
            string[] includeAssembliesForScan = new string[] { "QuizProjectMvc.Web" };

            var currentAssembly = this.GetType().Assembly;
            var siteMapProviderAssembly = typeof(SiteMaps).Assembly;
            var allAssemblies = new Assembly[] { currentAssembly, siteMapProviderAssembly };
            var excludeTypes = new Type[] {
            // Use this array to add types you wish to explicitly exclude from convention-based
            // auto-registration. By default all types that either match I[TypeName] = [TypeName] or
            // I[TypeName] = [TypeName]Adapter will be automatically wired up as long as they don't
            // have the [ExcludeFromAutoRegistrationAttribute].
            //
            // If you want to override a type that follows the convention, you should add the name
            // of either the implementation name or the interface that it inherits to this list and
            // add your manual registration code below. This will prevent duplicate registrations
            // of the types from occurring.

            // Example:
            // typeof(SiteMap),
            // typeof(SiteMapNodeVisibilityProviderStrategy)
            };
            var multipleImplementationTypes = new Type[] {
                typeof(ISiteMapNodeUrlResolver),
                typeof(ISiteMapNodeVisibilityProvider),
                typeof(IDynamicNodeProvider)
            };

            // Matching type name (I[TypeName] = [TypeName]) or matching type name + suffix Adapter (I[TypeName] = [TypeName]Adapter)
            // and not decorated with the [ExcludeFromAutoRegistrationAttribute].
            CommonConventions.RegisterDefaultConventions(
                (interfaceType, implementationType) => builder.RegisterType(implementationType).As(interfaceType).SingleInstance(),
                new Assembly[] { siteMapProviderAssembly },
                allAssemblies,
                excludeTypes,
                string.Empty);

            // Multiple implementations of strategy based extension points (and not decorated with [ExcludeFromAutoRegistrationAttribute]).
            CommonConventions.RegisterAllImplementationsOfInterface(
                (interfaceType, implementationType) => builder.RegisterType(implementationType).As(interfaceType).SingleInstance(),
                multipleImplementationTypes,
                allAssemblies,
                excludeTypes,
                string.Empty);

            // Registration of internal controllers
            CommonConventions.RegisterAllImplementationsOfInterface(
                (interfaceType, implementationType) => builder.RegisterType(implementationType).As(interfaceType).AsSelf().InstancePerDependency(),
                new Type[] { typeof(IController) },
                new Assembly[] { siteMapProviderAssembly },
                new Type[0],
                string.Empty);

            // Visibility Providers
            builder.RegisterType<SiteMapNodeVisibilityProviderStrategy>()
                .As<ISiteMapNodeVisibilityProviderStrategy>()
                .WithParameter("defaultProviderName", string.Empty);

            // Pass in the global controllerBuilder reference
            builder.RegisterInstance(ControllerBuilder.Current)
                   .As<ControllerBuilder>();

            builder.RegisterType<ControllerTypeResolverFactory>()
                .As<IControllerTypeResolverFactory>()
                .WithParameter("areaNamespacesToIgnore", new string[0]);

            // Configure Security
            builder.RegisterType<AuthorizeAttributeAclModule>()
                .Named<IAclModule>("authorizeAttributeAclModule");
            builder.RegisterType<XmlRolesAclModule>()
                .Named<IAclModule>("xmlRolesAclModule");
            builder.RegisterType<CompositeAclModule>()
                .As<IAclModule>()
                .WithParameter(
                    (p, c) => p.Name == "aclModules",
                    (p, c) => new[]
                        {
                            c.ResolveNamed<IAclModule>("authorizeAttributeAclModule"),
                            c.ResolveNamed<IAclModule>("xmlRolesAclModule")
                        });

            builder.RegisterInstance(System.Runtime.Caching.MemoryCache.Default)
                   .As<System.Runtime.Caching.ObjectCache>();

            builder.RegisterGeneric(typeof(RuntimeCacheProvider<>))
                .As(typeof(ICacheProvider<>));

            builder.RegisterType<RuntimeFileCacheDependency>()
                .Named<ICacheDependency>("cacheDependency1")
                .WithParameter("fileName", absoluteFileName);

            builder.RegisterType<CacheDetails>()
                .Named<ICacheDetails>("cacheDetails1")
//.........这里部分代码省略.........
开发者ID:kidroca,项目名称:project-quiz-mvc,代码行数:101,代码来源:MvcSiteMapProviderModule.cs


注:本文中的ContainerBuilder.RegisterGeneric方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。