本文整理汇总了C#中Autofac.ContainerBuilder.RegisterGeneric方法的典型用法代码示例。如果您正苦于以下问题:C# ContainerBuilder.RegisterGeneric方法的具体用法?C# ContainerBuilder.RegisterGeneric怎么用?C# ContainerBuilder.RegisterGeneric使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Autofac.ContainerBuilder
的用法示例。
在下文中一共展示了ContainerBuilder.RegisterGeneric方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Load
protected override void Load(ContainerBuilder builder)
{
builder.RegisterGeneric(typeof (BasicRavenRepositoryWithGuid<>))
.As(typeof (IRepository<>))
.As(typeof (IRepositoryWithGuid<>))
.AsImplementedInterfaces();
builder.RegisterGeneric(typeof (Refrence<>))
.PropertiesAutowired()
.AsSelf();
}
示例2: SetAutofacContainer
private static void SetAutofacContainer()
{
var builder = new ContainerBuilder();
builder.RegisterControllers(Assembly.GetExecutingAssembly());
builder.RegisterGeneric(typeof (UnitOfWork<>)).As(typeof (IUnitOfWork<>));
builder.RegisterGeneric(typeof (RepositoryBase<>)).As(typeof (IRepository<>));
builder.RegisterType(typeof (CategoryService)).As(typeof (ICategoryService)).InstancePerDependency();
builder.RegisterType(typeof (DepartmentService)).As(typeof (IDepartmentService)).InstancePerDependency();
IContainer container = builder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
}
示例3: RegisterCommonComponents
private void RegisterCommonComponents(ContainerBuilder builder) {
var mapper = Mapper ?? new MemoizingMappingManager(new AttributesMappingManager());
builder.RegisterInstance(mapper).As<IReadOnlyMappingManager>();
// builder.RegisterType<HttpRuntimeCache>().As<ISolrCache>();
builder.RegisterType<DefaultDocumentVisitor>().As<ISolrDocumentPropertyVisitor>();
builder.RegisterType<DefaultFieldParser>().As<ISolrFieldParser>();
builder.RegisterGeneric(typeof (SolrDocumentActivator<>)).As(typeof (ISolrDocumentActivator<>));
builder.RegisterGeneric(typeof (SolrDocumentResponseParser<>)).As(typeof (ISolrDocumentResponseParser<>));
builder.RegisterType<DefaultFieldSerializer>().As<ISolrFieldSerializer>();
builder.RegisterType<DefaultQuerySerializer>().As<ISolrQuerySerializer>();
builder.RegisterType<DefaultFacetQuerySerializer>().As<ISolrFacetQuerySerializer>();
builder.RegisterGeneric(typeof(DefaultResponseParser<>)).As(typeof(ISolrAbstractResponseParser<>));
builder.RegisterType<HeaderResponseParser<string>>().As<ISolrHeaderResponseParser>();
builder.RegisterType<ExtractResponseParser>().As<ISolrExtractResponseParser>();
foreach (var p in new[] {
typeof (MappedPropertiesIsInSolrSchemaRule),
typeof (RequiredFieldsAreMappedRule),
typeof (UniqueKeyMatchesMappingRule),
typeof(MultivaluedMappedToCollectionRule),
})
builder.RegisterType(p).As<IValidationRule>();
builder.RegisterType<SolrSchemaParser>().As<ISolrSchemaParser>();
builder.RegisterGeneric(typeof(SolrMoreLikeThisHandlerQueryResultsParser<>)).As(typeof(ISolrMoreLikeThisHandlerQueryResultsParser<>));
builder.RegisterGeneric(typeof(SolrQueryExecuter<>)).As(typeof(ISolrQueryExecuter<>));
builder.RegisterGeneric(typeof (SolrDocumentSerializer<>)).As(typeof (ISolrDocumentSerializer<>));
builder.RegisterType<SolrDIHStatusParser>().As<ISolrDIHStatusParser>();
builder.RegisterType<MappingValidator>().As<IMappingValidator>();
builder.RegisterType<SolrStatusResponseParser>().As<ISolrStatusResponseParser>();
builder.RegisterType<SolrCoreAdmin>().As<ISolrCoreAdmin>();
builder.RegisterType<SolrDictionarySerializer>().As<ISolrDocumentSerializer<Dictionary<string, object>>>();
builder.RegisterType<SolrDictionaryDocumentResponseParser>().As<ISolrDocumentResponseParser<Dictionary<string, object>>>();
}
示例4: Load
protected override void Load(ContainerBuilder builder)
{
//config
builder.RegisterType<NHBoxConfig>().As<INHPropertiesProvider>().SingleInstance();
builder.RegisterType<RepositoryFinder>()
.PropertiesAutowired()
.InstancePerDependency();
//transformations
builder.RegisterGeneric(typeof (FileBoxTransformation<>)).InstancePerDependency();
builder.RegisterGeneric(typeof (NHibernateBoxTransformation<,>)).InstancePerDependency();
builder.RegisterType<BackupsResolver>().InstancePerDependency();
//initialize evaluator
builder.RegisterType<NHibernateBoxQueryEvaluator>()
.PropertiesAutowired()
.SingleInstance();
builder.RegisterGeneric(typeof(BoxQuery<>))
.OnActivated(x =>
{
if (BoxedQueryRemoteExtensions.DefaultEvaluator == null)
BoxedQueryRemoteExtensions.DefaultEvaluator = x.Context.Resolve<NHibernateBoxQueryEvaluator>().Evaluate;
});
base.Load(builder);
}
示例5: ConfigureContainer
public static void ConfigureContainer()
{
var builder = new ContainerBuilder();
builder.RegisterControllers(Assembly.GetAssembly(typeof(AutofacConfig)));
builder.Register(context => NhibernateConfig.GetSessionFactory()).As<ISessionFactory>().SingleInstance();
// Use a session per web request.
builder.Register(context => context.Resolve<ISessionFactory>().OpenSession())
.As<ISession>()
.InstancePerHttpRequest();
builder.RegisterGeneric(typeof(UserAccountService<>)).AsSelf();
builder.RegisterType(typeof(SamAuthenticationService<NhUserAccount>))
.AsSelf()
.As(typeof(AuthenticationService<NhUserAccount>));
builder.RegisterGeneric(typeof(MembershipRebootConfiguration<>)).AsSelf();
builder.RegisterGeneric(typeof(NhRepository<>)).As(typeof(IRepository<>));
builder.RegisterGeneric(typeof(NhUserAccountRepository<>)).As(typeof(IUserAccountRepository<>));
builder.RegisterGeneric(typeof(NhGroupRepository<>))
.As(typeof(IGroupRepository<>));
builder.RegisterType(typeof(NhGroupRepository<NhGroup>))
.As(typeof(IGroupQuery));
builder.Register(
context => new GroupService<NhGroup>("default", context.Resolve<IGroupRepository<NhGroup>>())).AsSelf();
var container = builder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
}
示例6: Register
public void Register(ContainerBuilder builder, ITypeFinder typeFinder)
{
var zergDataProvider = new ZergDataProvider();
zergDataProvider.InitDatabase();
var dataSettingsManager = new DataSettingsManager();
var dataProviderSettings = dataSettingsManager.LoadSettings();
//不同数据库对应不同的仓库
if (dataProviderSettings != null && dataProviderSettings.IsValid())
{
builder.Register<IDbContext>(c => new CMSDbContext(dataProviderSettings.RawDataSettings["CMSConnection"])).Named<IDbContext>("CMS").InstancePerRequest();
builder.Register<IDbContext>(c => new CRMDbContext(dataProviderSettings.RawDataSettings["CRMConnection"])).Named<IDbContext>("CRM").InstancePerRequest();
builder.Register<IDbContext>(c => new TradingDbContext(dataProviderSettings.RawDataSettings["TradingConnection"])).Named<IDbContext>("Trading").InstancePerRequest();
builder.Register<IDbContext>(c => new EventDbContext(dataProviderSettings.RawDataSettings["EventConnection"])).Named<IDbContext>("Event").InstancePerRequest();
builder.Register<IDbContext>(c => new CommunityDbContext(dataProviderSettings.RawDataSettings["CommunityConnection"])).Named<IDbContext>("Community").InstancePerRequest();
}
#region 不同的数据库对应不同仓库
var cmsParameter = new ResolvedParameter((pi, ctx) => pi.Name == "context",
(pi, ctx) => ctx.ResolveNamed<IDbContext>("CMS"));
var crmParameter = new ResolvedParameter((pi, ctx) => pi.Name == "context",
(pi, ctx) => ctx.ResolveNamed<IDbContext>("CRM"));
var tradingParameter = new ResolvedParameter((pi, ctx) => pi.Name == "context",
(pi, ctx) => ctx.ResolveNamed<IDbContext>("Trading"));
var eventPrameter = new ResolvedParameter((pi, ctx) => pi.Name == "context",
(pi, ctx) => ctx.ResolveNamed<IDbContext>("Event"));
var communityPrameter = new ResolvedParameter((pi, ctx) => pi.Name == "context",
(pi, ctx) => ctx.ResolveNamed<IDbContext>("Community"));
builder.RegisterGeneric(typeof(CMSRepository<>))
.WithParameter(cmsParameter)
.As(typeof(ICMSRepository<>))
.InstancePerRequest();
builder.RegisterGeneric(typeof(CRMRepository<>))
.WithParameter(crmParameter)
.As(typeof(ICRMRepository<>))
.InstancePerRequest();
builder.RegisterGeneric(typeof(TradingRepository<>))
.WithParameter(tradingParameter)
.As(typeof(ITradingRepository<>))
.InstancePerRequest();
builder.RegisterGeneric(typeof(EventRepository<>))
.WithParameter(eventPrameter)
.As(typeof(IEventRepository<>))
.InstancePerRequest();
builder.RegisterGeneric(typeof(CommunityRepository<>))
.WithParameter(communityPrameter)
.As(typeof(ICommunityRepository<>))
.InstancePerRequest();
#endregion
// builder.RegisterAssemblyTypes(typeFinder.GetAssemblies().ToArray())
// .Where(t =>!String.IsNullOrEmpty(t.Namespace)&& t.Namespace.StartsWith("CMS") && t.Name.Contains("Service"))
// .WithParameter(repositeryParameter);
}
示例7: RegisterServices
private static void RegisterServices(ContainerBuilder builder)
{
//var context = ;
builder.Register(x => new ApplicationDbContext())
.As<DbContext>()
.InstancePerRequest();
builder.Register(x => new HttpCacheService())
.As<ICacheService>()
.InstancePerRequest();
/*builder.Register(x => context)
.As<IdentityDbContext<ApplicationUser>>()
.InstancePerRequest();*/
builder.Register(x => new IdentifierProvider())
.As<IIdentifierProvider>()
.InstancePerRequest();
var servicesAssembly = Assembly.GetAssembly(typeof(ITestsService));
builder.RegisterAssemblyTypes(servicesAssembly).AsImplementedInterfaces();
builder.RegisterGeneric(typeof(DbRepository<>))
.As(typeof(IDbRepository<>))
.InstancePerRequest();
builder.RegisterGeneric(typeof(GenericRepository<,>))
.As(typeof(IDbGenericRepository<,>))
.InstancePerRequest();
builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly())
.AssignableTo<BaseController>().PropertiesAutowired();
}
示例8: Register
public virtual void Register(ContainerBuilder builder, ITypeFinder typeFinder)
{
Singleton<IDependencyResolver>.Instance = new DependencyResolver();
string collectionString = "Data Source=.;Initial Catalog=demo;User ID=sa;Password=sa;Trusted_Connection=False;";
builder.Register<IDbContext>(c => new WaterMoonContext(collectionString)).PerLifeStyle(ComponentLifeStyle.LifetimeScope);
builder.RegisterGeneric(typeof(EfRepository<>)).As(typeof(IRepository<>)).PerLifeStyle(ComponentLifeStyle.LifetimeScope);
builder.RegisterGeneric(typeof(PagedList<>)).As(typeof(IPagedList<>)).PerLifeStyle(ComponentLifeStyle.LifetimeScope);
builder.RegisterType<DefualtMapping>().As<IMapping>().SingleInstance();
//注册所有实现IConsumer<>类型的对象
var consumers = typeFinder.FindClassesOfType(typeof(IConsumer<>)).ToList();
foreach (var consumer in consumers)
{
builder.RegisterType(consumer)
.As(consumer.FindInterfaces((type, criteria) =>
{
var isMatch = type.IsGenericType && ((Type)criteria).IsAssignableFrom(type.GetGenericTypeDefinition());
return isMatch;
}, typeof(IConsumer<>)))
.PerLifeStyle(ComponentLifeStyle.LifetimeScope);
}
builder.RegisterType<EventPublisher>().As<IEventPublisher>().SingleInstance();
builder.RegisterType<SubscriptionService>().As<ISubscriptionService>().SingleInstance();
builder.RegisterType<DemoService>().As<IDemoService>().PerLifeStyle(ComponentLifeStyle.LifetimeScope);
}
示例9: RegisterServices
private static void RegisterServices(ContainerBuilder builder)
{
builder.Register(x => new ApplicationDbContext())
.As<DbContext>()
.InstancePerRequest();
builder.Register(x => new HttpCacheService())
.As<ICacheService>()
.InstancePerRequest();
builder.Register(x => new HtmlSanitizerAdapter())
.As<ISanitizer>()
.InstancePerRequest();
builder.Register(x => new IdentifierProvider())
.As<IIdentifierProvider>()
.InstancePerRequest();
var servicesAssembly = Assembly.GetAssembly(typeof(IPostsService));
builder.RegisterAssemblyTypes(servicesAssembly).AsImplementedInterfaces();
builder.RegisterGeneric(typeof(DbRepository<>)).As(typeof(IDbRepository<>)).InstancePerRequest();
builder.RegisterGeneric(typeof(UserDbRepository<>)).As(typeof(IUserDbRepository<>)).InstancePerRequest();
builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly())
.AssignableTo<BaseController>().PropertiesAutowired();
}
示例10: Register
public virtual void Register(ContainerBuilder builder, ITypeFinder typeFinder, NopConfig config)
{
var asm = AppDomain.CurrentDomain.GetAssemblies();
builder.RegisterGeneric(typeof(BaseEntityService<>)).As(typeof(IBaseEntityService<>)).InstancePerLifetimeScope();
builder.RegisterGeneric(typeof(BaseEntityWithPictureService<,>)).As(typeof(IBaseEntityWithPictureService<,>)).InstancePerLifetimeScope();
builder.RegisterType<MobPageHeadBuilder>().As<MobPageHeadBuilder>().InstancePerRequest();
//register all the implemented services in various mob plugins
builder.RegisterAssemblyTypes(asm).AsClosedTypesOf(typeof(BaseEntityService<>))
.AsImplementedInterfaces()
.InstancePerDependency();
builder.RegisterAssemblyTypes(asm).AsClosedTypesOf(typeof(BaseEntityWithPictureService<,>))
.AsImplementedInterfaces()
.InstancePerDependency();
//register all the repositories
builder.RegisterGeneric(typeof(MobRepository<>)).As(typeof(IMobRepository<>))
.WithParameter(ResolvedParameter.ForNamed<IDbContext>(ContextName))
.InstancePerLifetimeScope();
}
示例11: Register
public void Register(ContainerBuilder builder, SmartConfig config)
{
builder.RegisterType<Context.SampleDbContext>().As<DbContext>().InstancePerLifetimeScope();
builder.RegisterType<Context.OtherDbContext>().As<Context.IOtherDbContext>().InstancePerLifetimeScope();
builder.RegisterGeneric(typeof(EFRepository<>)).As(typeof(IRepository<>)).InstancePerLifetimeScope();
builder.RegisterGeneric(typeof(EFRepository<,>)).As(typeof(IRepository<,>)).InstancePerLifetimeScope();
}
示例12: Register
public static void Register()
{
var builder = new ContainerBuilder();
//cache manager
builder.RegisterType<MemoryCacheManager>().As<ICacheManager>().Named<ICacheManager>("static_cache_manager").SingleInstance();
//builder.RegisterType<PerRequestCacheManager>().As<ICacheManager>().Named<ICacheManager>("per_request_cache_manager").InstancePerLifetimeScope();
// Since WCF requests does not have an http context we can not use InstancePerHttpRequest(). Instead we can use InstancePerLifetimeScope()
// which is resolvable for both WCF and http requests. Autofac Wiki says :
// The default ASP.NET and WCF integrations are set up so that InstancePerLifetimeScope() will attach a component to the current web request or service method call.
// Register WCF services
builder.RegisterType<UserService>().InstancePerLifetimeScope();
builder.RegisterType<GenericCharacteristicService>().InstancePerLifetimeScope();
builder.RegisterType<DataInfoService>().InstancePerLifetimeScope();
builder.RegisterType<Config>().InstancePerLifetimeScope();
builder.RegisterType<WebsiteSearcher>().As<ISearcher>().InstancePerLifetimeScope();
builder.RegisterType<DebugHelper>().As<IDebugHelper>().SingleInstance();
// database register
var dbSettingsManager = new DatabaseSettingsManager();
var databaseSettings = dbSettingsManager.LoadSettings();
builder.RegisterType<DatabaseSettingsManager>().InstancePerDependency();
builder.Register(context => context.Resolve<DatabaseSettingsManager>().LoadSettings()).As<DatabaseSettings>().InstancePerLifetimeScope();
builder.Register(context => new EfDataProviderManager(context.Resolve<DatabaseSettings>())).As<IDataProviderManager>().InstancePerDependency();
// register for two types
builder.Register(context => (IEfDataProvider)context.Resolve<IDataProviderManager>().DataProvider()).As<IDataProvider>().InstancePerDependency();
builder.Register(context => (IEfDataProvider)context.Resolve<IDataProviderManager>().DataProvider()).As<IEfDataProvider>().InstancePerDependency();
if (databaseSettings != null && databaseSettings.IsValid())
{
//var efDataProviderManager = new EfDataProviderManager(dbSettingsManager.LoadSettings());
//var dataProvider = (IEfDataProvider)efDataProviderManager.DataProvider();
//dataProvider.Init();
builder.Register<IDatabase>(context => new CommerceDbContext(databaseSettings.DataConnectionString)).InstancePerLifetimeScope();
}
else
{
builder.Register<IDatabase>(context => new CommerceDbContext(dbSettingsManager.LoadSettings().DataConnectionString)).InstancePerLifetimeScope();
}
if (!dbSettingsManager.IsFaked)
{
builder.RegisterGeneric(typeof(EfRepository<>)).As(typeof(IRepository<>)).InstancePerLifetimeScope();
}
else
{
builder.RegisterGeneric(typeof(FaskeRepository<>)).As(typeof(IRepository<>)).InstancePerLifetimeScope();
}
//host.EnableDiscovery();
var container = builder.Build();
// make AutofacHostFactory.Container equal to EngineContext.Current
// after it, should not set new container to AutofacHostFactory.Container or EngineContext.Current
AutofacHostFactory.Container = container;
EngineContext.Initialize(new AutofacContainerManager(container), new ContainerConfig(), false);
}
示例13: Load
protected override void Load(ContainerBuilder builder)
{
builder.RegisterType<NHibernateQuery>();
builder.RegisterType<EntityDuplicateChecker>().As<IEntityDuplicateChecker>();
builder.RegisterGeneric(typeof(NHibernateRepository<>)).As(typeof(INHibernateRepository<>));
builder.RegisterGeneric(typeof(NHibernateRepositoryWithTypedId<,>)).As(typeof(INHibernateRepositoryWithTypedId<,>));
builder.RegisterType<DefaultSessionFactoryKeyProvider>().As<ISessionFactoryKeyProvider>();
}
示例14: Load
protected override void Load(ContainerBuilder builder)
{
builder.RegisterGeneric(typeof (BasicNHibernateRepository<>))
.As(typeof (IRepository<>));
builder.RegisterGeneric(typeof(BasicNHibernateRepositoryWithGuid<>))
.As(typeof(IRepositoryWithGuid<>));
}
示例15: SetGenerator
private static void SetGenerator(ContainerBuilder container)
{
container.RegisterType<Generator.Database.LiturgieDatabase>().As<ILiturgieDatabase.ILiturgieDatabase>();
container.RegisterType<Generator.LiturgieOplosser.LiturgieOplosser>().As<ILiturgieDatabase.ILiturgieLosOp>().WithParameter("defaultSetNameEmpty", Properties.Settings.Default.SetNameEmpty);
container.RegisterGeneric(typeof(Generator.Database.FileSystem.FileEngine<>)).As(typeof(IDatabase.Engine.IEngine<>));
container.RegisterGeneric(typeof(Database.EngineManager<>)).As(typeof(IDatabase.IEngineManager<>));
SetMsPowerpointBuilder(container);
}