Dan*_*Dan 4 c# dependency-injection unity-container
我们将现有的日志记录库包装在我们自己的C#应用程序中的日志记录服务中,以便使用预定义的方法为特定的日志记录情
public class LoggingBlockLoggingService : ILoggingService
{
private LogWriter writer;
public LoggingBlockLoggingService(LogWriter writer)
{
this.writer = writer;
}
....//logging convenience methods, LogWarning(), etc...
}
Run Code Online (Sandbox Code Playgroud)
我想修改这个实现,以便它接受实例化它的类的类型(底层记录器,在这种情况下,LogWriter将是一个单例).所以要么使这个实现(和接口ILoggingService)通用:
public class LoggingBlockLoggingService<T> : ILoggingService<T>
{
...
private string typeName = typeof(T).FulName;
...
Run Code Online (Sandbox Code Playgroud)
或者添加一个额外的构造函数参数:
public class LoggingBlockLoggingService : ILoggingService
{
private LogWriter writer;
private string typeName;
public LoggingBlockLoggingService(LogWriter writer, Type type)
{
this.writer = writer;
this.typeName = type.FullName;
}
....//Include the typeName in the logs so we know the class that is logging.
}
Run Code Online (Sandbox Code Playgroud)
在注册我们的类型时,有没有办法在Unity中配置一次?我想避免为每个想要记录的类添加一个条目.理想情况下,如果有人想在我们的项目中向类中添加日志记录,他们只需将ILoggingService添加到他们正在使用的类的构造函数中,而不是在我们的unity配置中添加另一行来注册他们正在处理的每个类. .
我们使用的是运行时/代码配置,而不是XML
Dav*_*ead 12
是的,你可以使用:
container.RegisterType(typeof(IMyGenericInterface<>), typeof(MyConcreteGenericClass<>));
| 归档时间: |
|
| 查看次数: |
4497 次 |
| 最近记录: |