C# 使用autofac注册特定接口类型的不同依赖项
给定以下注册码…C# 使用autofac注册特定接口类型的不同依赖项,c#,asp.net-mvc,autofac,C#,Asp.net Mvc,Autofac,给定以下注册码… ILogger将单个字符串参数作为构造函数参数。 IJob实现都将ILogger作为构造函数参数 // register the 'default' logger. builder.RegisterType<Logger>().As<ILogger>() .WithParameter(new NamedParameter("category", "default")); //注册“默认”记录器。 builder.RegisterType()
ILogger
将单个字符串参数作为构造函数参数。IJob
实现都将ILogger
作为构造函数参数
// register the 'default' logger.
builder.RegisterType<Logger>().As<ILogger>()
.WithParameter(new NamedParameter("category", "default"));
//注册“默认”记录器。
builder.RegisterType().As()
.WithParameter(新名称参数(“类别”、“默认”));
为了简洁起见,大量注册被删掉了
// register the 'job' logger. (this is a problem, a duplicate...)
builder.RegisterType<Logger>().As<ILogger>()
.WithParameter(new NamedParameter("category", "job"));
// register some classes that need a different logger parameter.
builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly())
.Where(_ => typeof(IJob).IsAssignableFrom(_))
// how to do this...
.WithLoggerWithCategoryJob();
//注册“作业”记录器。(这是一个问题,重复…)
builder.RegisterType().As()
.WithParameter(新名称参数(“类别”、“职务”));
//注册一些需要不同记录器参数的类。
builder.RegisterAssemblyTypes(Assembly.getExecutionGassembly())
.Where(=>typeof(IJob).IsAssignableFrom(ux))
//如何做到这一点。。。
.WithLoggerWithCategoryJob();
如何将category=default
记录器用于除IJob
之外的所有类注册。我想使用
category=job
记录器处理所有这些问题。此解决方案演示了如何为记录器提供默认参数,以及如何仅为IRepository
实现覆盖日志参数
根据实现的接口,可以很容易地增强代码以提供一组覆盖
/// <summary>
/// The logging module.
/// </summary>
public class LoggingModule : Module
{
/// <summary>
/// Override to add registrations to the container.
/// </summary>
/// <remarks>
/// Note that the ContainerBuilder parameter is unique to this module.
/// </remarks>
/// <param name="builder">The builder through which components can be registered.</param>
protected override void Load(ContainerBuilder builder)
{
builder.RegisterType<NewLogger>()
.As<ILogger>()
.WithParameter(new NamedParameter("category", "Default"));
base.Load(builder);
}
/// <summary>
/// Override to attach module-specific functionality to a component registration.
/// </summary>
/// <remarks>
/// This method will be called for all existing <i>and future</i> component
/// registrations - ordering is not important.
/// </remarks>
/// <param name="componentRegistry">The component registry.</param><param name="registration">The registration to attach functionality to.</param>
protected override void AttachToComponentRegistration(IComponentRegistry componentRegistry, IComponentRegistration registration)
{
registration.Preparing += (sender, args) =>
{
var type = args.Component.Activator.LimitType;
if (type.GetInterfaces().All(_ => !_.IsGenericType || _.GetGenericTypeDefinition() != typeof(IRepository<>)))
return;
var pm = new ResolvedParameter(
(p, c) => p.ParameterType == typeof(ILogger),
(p, c) => c.Resolve<ILogger>(new NamedParameter("category", "RepositoryLogger")));
args.Parameters = args.Parameters.Union(new[] { pm });
};
base.AttachToComponentRegistration(componentRegistry, registration);
}
}
//
///日志模块。
///
公共类日志模块:模块
{
///
///覆盖以将注册添加到容器。
///
///
///请注意,ContainerBuilder参数对此模块是唯一的。
///
///可以注册组件的生成器。
受保护的覆盖无效负载(ContainerBuilder builder)
{
builder.RegisterType()
.As()
.WithParameter(新名称参数(“类别”、“默认”));
基础荷载(建筑商);
}
///
///覆盖以将特定于模块的功能附加到组件注册。
///
///
///将为所有现有和未来组件调用此方法
///注册-订购并不重要。
///
///组件注册表。要附加功能的注册。
受保护的覆盖无效AttachToComponentRegistration(IComponentRegistry ComponentRegistration,IComponentRegistration注册)
{
注册。正在准备+=(发件人,参数)=>
{
变量类型=args.Component.Activator.LimitType;
if(type.GetInterfaces().All(=>!0.IsGenericType | | | 0.GetGenericTypeDefinition()!=typeof(IRepository)))
返回;
var pm=新解析的参数(
(p,c)=>p.ParameterType==typeof(ILogger),
(p,c)=>c.Resolve(新名称参数(“类别”、“存储记录器”));
args.Parameters=args.Parameters.Union(新[]{pm});
};
基本。附件组件注册(组件注册,注册);
}
}
您是否查看过自定义IRegistrationSource?看,我将尝试一下并让您知道,它看起来可能会奏效。我已经对此进行了几个小时的黑客攻击,但不确定IRegistrationSource中是否有足够的上下文来完成上述操作。我会继续尝试。IComponentContext和InstanceLookup似乎没有被注入的主机类型。我想我需要考虑一种不同的方法,也许只是使用某种工厂。