如何在AutoFac中使用属性注入

本文关键字:属性 注入 AutoFac | 更新日期: 2023-09-27 18:05:50

在控制台应用程序中,我使用Log4Net,并在Main方法中获取记录器对象。现在,我想让这个日志对象在我所有的类中可用,让所有的类从一个BaseClass继承,它有一个ILog属性,应该是由属性注入而不是构造函数注入设置。

我使用AutoFac IoC容器,如何注入我的日志对象到我的每一个类的日志属性?

最好/最简单的方法是什么?

是否有自动解析类型的方法?

下面是我的测试应用程序:
namespace ConsoleApplication1
{
    class Program
    {
        static ILog Log;
        static IContainer Container;
        static void Main(string[] args)
        {                
           InitializeLogger();
           InitializeAutoFac();
            // the below works but could it be done automatically (without specifying the name of each class)?
           Product.Log = Container.Resolve<ILog>();
           // tried below but didn't inject ILog object into the Product
           Container.Resolve<Product>();
           RunTest();
            Console.ReadLine();
        }
        private static void RunTest()
        {
            var product = new Product();
            product.Do();
        }
        private static void InitializeAutoFac()
        {
            var builder = new ContainerBuilder();
            builder.Register(c => Log).As<ILog>();
            builder.RegisterType<Product>().PropertiesAutowired();
            Container = builder.Build();            
        }
        private static void InitializeLogger()
        {
            log4net.Config.XmlConfigurator.Configure();
            Log = LogManager.GetLogger("LoggerName");
        }
    }
    public class Product
    {
        public static ILog Log { get; set; }
        public void Do()
        {
            // this throws exception because Log is not set   
            Log.Debug("some Debug");  
        }
    }
}

如何在AutoFac中使用属性注入

在我看来,Ninject创建的解决方案比Autofac中的propertyinjection要好得多。因此,我创建了一个自定义属性,它是一个后置方面,它会自动注入我的类:

[AutofacResolve]
public IStorageManager StorageManager { get; set; }

我的方面:

[Serializable]
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
public class AutofacResolveAttribute : LocationInterceptionAspect
{
    public override void OnGetValue(LocationInterceptionArgs args)
    {
        args.ProceedGetValue();
        if (!args.Location.LocationType.IsInterface) return;
        if ( args.Value != null )
        {
           args.Value = DependencyResolver.Current.GetService(args.Location.LocationType);
           args.ProceedSetValue();
        }
    }
}

我知道这个问题的答案已经给出了,但我认为这是解决Autofac中自动属性注入的一个非常简洁的方法。也许将来会对别人有用。

使用属性注入:

builder.Register(c => LogManager.GetLogger("LoggerName"))
       .As<ILog>();
builder.RegisterType<CustomClass>()
       .PropertiesAutowired();

属性注入适用于属性,而不适用于字段。在你的类中,Log是一个字段而不是一个属性,因此它永远不会被Autofac解析。

使用属性注入(除了@cuongle answer)。

选项1:

builder.Register(c => LogManager.GetLogger("LoggerName")).As<ILog>();
builder.RegisterType<Product>()
        .WithProperty("Log", LogManager.GetLogger("LoggerName"));

选项2:

或者您可以将SetLog方法添加到Product类:

public class Product
{
    public static ILog Log { get; set; }
    public SetLog(Log log)
    {
        Log = log;
    }
}

这样您就不必调用LogManager.GetLogger("LoggerName")两次,而是使用构建器的上下文来解析Log

builder.Register(c => LogManager.GetLogger("LoggerName")).As<ILog>();
builder.Register(c => 
    var product = new Product();
    product.SetLog(c.Resolve<Log>());
    return product;
);

选项3:

使用onactivated:

OnActivated事件在组件完全构造完成后引发。在这里,可以执行依赖于的应用程序级任务组件被完全构造——这些应该是罕见的。

builder.RegisterType<Product>()
    .OnActivated((IActivatedEventArgs<Log> e) =>
    {
        var product = e.Context.Resolve<Parent>();
        e.Instance.SetParent(product);
    });

这些选项提供了更多的控制,你不必担心@steven comment:

PropertiesAutowired的可怕之处在于它确实如此隐式属性注入,这意味着任何不可解析的依赖项将被跳过。这很容易忽略配置错误,并可能导致应用程序在运行时失败

我不想使用postsharp,所以我做了一个快速的解决方案,但它不会自动注入。我是Autofac的新手,应该可以在这个解决方案的基础上构建。

[Serializable]
[AttributeUsage(AttributeTargets.Property)]
public class AutofacResolveAttribute : Attribute
{
}
public class AutofactResolver
{
    /// <summary>
    /// Injecting objects into properties marked with "AutofacResolve"
    /// </summary>
    /// <param name="obj">Source object</param>
    public static void InjectProperties(object obj)
    {
        var propertiesToInject = obj.GetType().GetProperties()
             .Where(x => x.CustomAttributes.Any(y => y.AttributeType.Name == nameof(AutofacResolveAttribute))).ToList();
        foreach (var property in propertiesToInject)
        {
            var objectToInject = Autofact.SharedContainer.Resolve(property.PropertyType);
            property.SetValue(obj, objectToInject, null);
        }
    }
}

与下面的调用一起使用:

AutofactResolver.InjectProperties(sourceObject);

有一个接口IPropertySelector,你可以通过.PropertiesAutowired(new MyPropertySelector())实现和传递实现。这将允许你实现任何你想要的逻辑。