如何使用物业注射AutoFac?

编程入门 行业动态 更新时间:2024-10-27 12:25:04
本文介绍了如何使用物业注射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?

本文发布于:2023-11-05 21:14:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1561878.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   物业   AutoFac

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!