我对Autofac有疑问:
如何从容器中获取请求实例的类的名称?
是否有可能在创建对象时通过autofac将类“Autofac_Test”的名称传递给构造函数?
我的代码:
using System;
using System.Diagnostics;
using System.Reflection;
using Autofac;
using Xunit;
public class BootStrap
{
public IContainer Configure()
{
var builder = new ContainerBuilder();
builder.Register(b => new MyLogger(MethodBase.GetCurrentMethod().ReflectedType)).As<ILog>();
return builder.Build();
}
}
public class Autofac_Test
{
private IContainer _containter;
public Autofac_Test()
{
_containter = new BootStrap().Configure();
}
[Fact]
public void Should_create_class_with_Name_BlaBla()
{
var logger = _containter.Resolve<ILog>();
Assert.True(logger.Name == "Autofac_Test");
}
}
public class MyLogger : ILog
{
public string Name { get; private set; }
public MyLogger(Type name)
{
Name = name.FullName;
}
public void Info(string message)
{
Debug.WriteLine(string.Format("{0} {1}", Name, message));
}
}
public interface ILog
{
void Info(string message);
string Name { get; }
}
解决方法:
对于您的具体情况,获取此信息将非常困难.但正如我理解的那样,MyLogger需要知道它将被注入的类型名称以及代码示例可能不相关.
在这种情况下,您可以使用模块和准备事件:
public class LoggingModule : Autofac.Module
{
protected override void AttachToComponentRegistration(
IComponentRegistry componentRegistry, IComponentRegistration registration)
{
registration.Preparing += (sender, e) =>
{
Type limitType = e.Component.Activator.LimitType;
e.Parameters = e.Parameters.Union(new[] {
new ResolvedParameter((pi, c) => pi.ParameterType == typeof(ILogger),
(pi, c) => c.Resolve<ILogger>(new NamedParameter("name", limitType.Name))),
});
};
}
}
然后你必须像这样注册你的模块:
ContainerBuilder builder = new ContainerBuilder();
builder.RegisterModule<LoggingModule>();
builder.RegisterType<MyLogger>().As<ILogger>();
现在,每次将ILogger注入类型时,它都将具有正确的名称.
class Foo
{
public Foo(ILogger logger)
{
this._loggerName = logger.Name;
}
private readonly String _loggerName;
public String LoggerName
{
get
{
return this._loggerName;
}
}
}
LoggerName将是Foo.
您的测试现在看起来像这样:
[Fact]
public void Should_create_class_with_Name_BlaBla()
{
var foo = _containter.Resolve<Foo>();
Assert.AreEquals("foo", foo.LoggerName, "invalid loggerName");
}