实体库5does有能力做到这一点。我猜你的监听器类上有一个 [ConfigurationElementType(typeof(CustomTraceListenerData)] ,对吗?
Entlib 5 被设计为独立于容器。因此,我们不能依赖任何类型的自动装配语义,因为每个容器的做法都不同。因此,您需要告诉 Entlib 要调用哪个构造函数,以及应该注入哪些依赖项。这是通过您的配置数据类完成的。
我举了一个简单的例子。这是跟踪侦听器 - 没什么特别的:
[ConfigurationElementType(typeof(GuiTraceListenerData))]
public class GuiTraceListener : TraceListener
{
private readonly ILogFormatter formatter;
private readonly IGuiController guiController;
public GuiTraceListener()
: this(string.Empty, null, null)
{
}
public GuiTraceListener(string name)
: this(name, null, null)
{
}
public GuiTraceListener(string name, ILogFormatter formatter, IGuiController guiController) : base(name)
{
this.formatter = formatter;
this.guiController = guiController;
}
public override void TraceData(TraceEventCache eventCache, string source, TraceEventType eventType, int id,
object data)
{
if ((Filter == null) || Filter.ShouldTrace(eventCache, source, eventType, id, null, null, data, null))
{
if (data is LogEntry)
{
if (formatter != null)
{
Write(formatter.Format(data as LogEntry));
}
else
{
base.TraceData(eventCache, source, eventType, id, data);
}
}
else
{
base.TraceData(eventCache, source, eventType, id, data);
}
}
}
public override void Write(string message)
{
guiController.Log(message);
}
public override void WriteLine(string message)
{
guiController.Log(message);
}
}
有趣的部分在 GuiTraceListenerData 类中:
public class GuiTraceListenerData : TraceListenerData
{
private const string formatterNameProperty = "formatter";
public GuiTraceListenerData()
: this("unnamed", null, TraceOptions.None)
{
}
public GuiTraceListenerData(string name)
: this(name, null, TraceOptions.None)
{
}
public GuiTraceListenerData(string name, string formatterName)
: this(name, formatterName, TraceOptions.None)
{
}
protected GuiTraceListenerData(string name, string formatterName, TraceOptions traceOutputOptions)
: base(name, typeof (GuiTraceListener), traceOutputOptions, SourceLevels.All)
{
ListenerDataType = typeof (GuiTraceListenerData);
Formatter = formatterName;
}
[ConfigurationProperty(formatterNameProperty, IsRequired = false)]
[Reference(typeof(NameTypeConfigurationElementCollection<FormatterData, CustomFormatterData>), typeof(FormatterData))]
public string Formatter
{
get { return (string) base[formatterNameProperty]; }
set { base[formatterNameProperty] = value; }
}
protected override Expression<Func<TraceListener>> GetCreationExpression()
{
return () =>
new GuiTraceListener(Name,
Container.ResolvedIfNotNull<ILogFormatter>(Formatter),
Container.Resolved<IGuiController>());
}
}
特别是查看 GetCreationExpression 方法。这告诉 entlib 要创建此配置 new 表示的对象,调用该构造函数,并解析格式化程序(如果指定了)和 IGuiController。
然后,在我的测试应用程序中(我使用 Winforms 只是为了快速)我初始化了我的容器和应用程序,如下所示:
static void Main()
{
var container = new UnityContainer()
.AddNewExtension<EnterpriseLibraryCoreExtension>()
.RegisterType<IGuiController, MessageBoxGuiController>();
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(container.Resolve<Form1>());
}
我的 Form1 类采用 LogWriter 作为构造函数参数。
Entlib 5 构建方式的好处是,您在执行此操作时几乎可以获得自动配置工具支持 - 无需编写单独的配置节点。您的运行时配置元素就是您所需要的 - 只需将 DLL 复制到配置工具的同一目录中,它就会拾取它。
不管怎样,从这里开始就可以了。
希望这可以帮助。如果您想了解更多详细信息,请给我写信,我可以向您发送整个工作项目。
-Chris