Autofac:注册组件并根据解析父级进行解析

时间:2012-07-27 16:06:30

标签: c# dependency-injection autofac

我想要注册一个组件,以根据它可能解析的类来解析参数。 (这听起来有点令人困惑,所以我会举一个例子)。

这是一个使用记录器的对象:

class MyObject : IMyObject
{
    public ILogger Logger;
    public MyObject(ILogger logger)
    {
        Logger = logger;
    }
}

现在,在COULD中传递的记录器在类与类之间是不同的。所以我对如何做到这一点有一个相当修补的想法:

class MyLogger : ILogger
{
    public string Name{get; protected set;}

    public static ILogger GetLogger(string className)
    {
        Name = className;
        MyLogger logger;
        // Do something to choose a logger for that specific class
        return logger;
    }
}

因此,当我注册Logger时,我希望能够告诉它className。我希望有一种方法可以做到这一点:

ContainerBuilder builder = new ContainerBuilder();

builder.Register<MyLogger>(ctx =>
    {
        string className = //Get resolving class name somehow;
        return MyLogger.GetLogger(className);
    }).As<ILogger>();

builder.Register<MyObject>().As<IMyObject>();

var container = builder.Build();

IMyObject myObj = container.Resolve<IMyObject>();

//myObject.Logger.Name should now == "MyObject"

我想这样做的原因是为了避免在代码中使用带有autofac的记录器注册我实现的每个类。我希望能够在xml中注册所有对象,并且只需要一个LoggerModule来添加此注册。

提前致谢!

1 个答案:

答案 0 :(得分:14)

这就是我所做的(ILog只是我自己的log4net包装器):

public class LoggingModule : Module
{
    protected override void Load(ContainerBuilder builder)
    {
        builder.Register((c, p) => GetLogger(p.TypedAs<Type>()));
    }

    protected override void AttachToComponentRegistration(
        IComponentRegistry registry, IComponentRegistration registration)
    {
        registration.Preparing +=
            (sender, args) =>
            {
                var forType = args.Component.Activator.LimitType;

                var logParameter = new ResolvedParameter(
                    (p, c) => p.ParameterType == typeof (ILog),
                    (p, c) => c.Resolve<ILog>(TypedParameter.From(forType)));

                args.Parameters = args.Parameters.Union(new[] {logParameter});
            };
    }

    public static ILog GetLogger(Type type)
    {
        return new Log4NetLogger(type);
    }
}

public interface ILog
{
    void Debug(string format, params object[] args);
    void Info(string format, params object[] args);
    void Warn(string format, params object[] args);

    void Error(string format, params object[] args);
    void Error(Exception ex);
    void Error(Exception ex, string format, params object[] args);

    void Fatal(Exception ex, string format, params object[] args);
}

public class Log4NetLogger : ILog
{
    private readonly log4net.ILog _log;

    static Log4NetLogger()
    {
        XmlConfigurator.Configure();
    }

    public Log4NetLogger(Type type)
    {
        _log = LogManager.GetLogger(type);
    }

    public void Debug(string format, params object[] args)
    {
        _log.DebugFormat(format, args);
    }

    public void Info(string format, params object[] args)
    {
        _log.InfoFormat(format, args);
    }

    public void Warn(string format, params object[] args)
    {
        _log.WarnFormat(format, args);
    }

    public void Error(string format, params object[] args)
    {
        _log.ErrorFormat(format, args);
    }

    public void Error(Exception ex)
    {
        _log.Error("", ex);
    }

    public void Error(Exception ex, string format, params object[] args)
    {
        _log.Error(string.Format(format, args), ex);
    }

    public void Fatal(Exception ex, string format, params object[] args)
    {
        _log.Fatal(string.Format(format, args), ex);
    }
}