如何使用物业注射AutoFac? [英] How to use Property Injection with AutoFac?

查看:93
本文介绍了如何使用物业注射AutoFac?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个控制台应用程序,我使用log4net的和主要方法,我得到的Logger对象。现在,我想通过让所有的类都从具有ILog的财产,应该由物业注射来设置,而不是构造注射BaseClass的继承,使在我所有的类可用此日志对象。

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

什么是实现这一目标的最佳/最简单的方法是什么?

有什么办法来自动解决类型?

下面是我的测试应用程序:

 命名空间的ConsoleApplication1
{
    类节目
    {
        静态的ILog登录;
        静态的IContainer容器;        静态无效的主要(字串[] args)
        {
           InitializeLogger();           InitializeAutoFac();            //下面的作品,但它可以自动完成(不指定每个类的名称)?
           Product.Log = Container.Resolve<&ILog的GT;();           //下面的尝试,但并未注入的ILog对象到产品
           Container.Resolve<产品及GT;();           的runTest();            到Console.ReadLine();
        }        私有静态无效的runTest()
        {
            变种产品=新产品();
            product.Do();
        }        私有静态无效InitializeAutoFac()
        {
            VAR建设者=新ContainerBuilder();            builder.Register(C =>日志)。作为<&ILog的GT;();            builder.RegisterType<产品及GT;()PropertiesAutowired()。            容器= builder.Build();
        }        私有静态无效InitializeLogger()
        {
            log4net.Config.XmlConfigurator.Configure();            日志= LogManager.GetLogger(LoggerName);
        }
    }    公共类产品
    {
        公共静态的ILog登录{搞定;组; }        公共无效DO()
        {
            //,这将引发异常,因为日志未设置
            Log.Debug(一些调试);
        }
    }
}


解决方案

使用地产注入

  builder.Register(C => LogManager.GetLogger(LoggerName))
       。至于<&ILog的GT;();builder.RegisterType< CustomClass>()
       .PropertiesAutowired();

In a Console application, I'm using Log4Net and in the Main method I'm getting the logger object. Now, I'd like to make this log object available in all my classes by letting all the classes inherit from a BaseClass which has a ILog property and is supposed to be set by Property Injection rather than Constructor Injection.

I'm using AutoFac IoC container, how to inject my log Object to the Log property of my every class?

What's the best/easiest way to achieve this?

Is there any way to automatically resolve types?

Below is my test application:

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");  
        }
    }
}

解决方案

Use Property Injection:

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

builder.RegisterType<CustomClass>()
       .PropertiesAutowired();

这篇关于如何使用物业注射AutoFac?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆