首页 > 其他分享 >Autofac中的AsImplementedInterfaces()

Autofac中的AsImplementedInterfaces()

时间:2023-01-31 11:14:03浏览次数:33  
标签:Autofac string get ITelemetryConfiguration IConfiguration AsImplementedInterface

在项目开发中,遇到一个问题,是这样的,我们有一个接口IConfiguration

public interface IConfiguration 
{        
        string DefaultValue { get; }

        int Order { get; }
}

另外有一个接口ITelemetryConfiguration继承它

 public interface ITelemetryConfiguration : IConfiguration
 {
         string TelemetryValue {get; }
 }

现在有2个类,一个是LogLevel类,它继承IConfiguration接口. 另一个是TelemetrySupport类,它继承ITelemetryConfiguration接口。 如下

public class LogLevel : IConfiguration
{
   private string logvalue;

   public string DefaultValue 
   {
       get {return logvalue;}
       set {logvalue = value;}    
   }
   

   public int Order
   {
       get {return 1;}
   }

}
public class TelemetrySupport : ITelemetryConfiguration
{
     public string DefaultValue
     {
            get { return "Enable"; }
      }

      
     public string TelemetryValue
        {
            get; set;
        }

        public int LoadOrder
        {
            get
            {
                return 2;
            }
        }


}

 

在系统启动时用Autofac写的依赖注入中,我们原来是这样写的

using Autofac;
using Module = Autofac.Module;

public class IoCModule : Module
{
    
    protected override void Load(ContainerBuilder builder)
    {
          
         builder.RegisterType<LogLevel>().As<IConfiguration>().SingleInstance(); 
         builder.RegisterType<TelemetrySupport>().As<IConfiguration>().SingleInstance();


    }



}

 

 

 


标签:Autofac,string,get,ITelemetryConfiguration,IConfiguration,AsImplementedInterface
From: https://www.cnblogs.com/wphl-27/p/17078314.html

相关文章

  • 数据访问层服务自动注册类封装和使用源码-AutoFac
    项目使用三层结构RepositoryIocFactoryusingSystem;usingSystem.Reflection;usingAutofac;namespaceCommonHelper.AutoInject.Repository{publicclassRe......
  • .net core (.net 6) IOC容器注入 -- autofac
    注:接口代码、类库代码参考:.netcore(.net6)IOC容器注入--内置容器 Autofac容器优点:灵活(属性注入、多种生命周期、AOP扩展)、比较流行(技术门槛低)1、引入NuGet包Auto......
  • automapper、autofack、子组件(props)、base64、请求头以及JWT和Session和Cookie 的区
    1、automapper将一个对象的字段的值映射到另一个对象相应的字段中使用:1.引用NuGet:AutoMappert、AutoMapper.Extensions.Microsoft.DependencyInjection2.新建一个......
  • AutoFac
    AutoFac的配置使用一.什么是AutoFac第三方IOC容器二.优点它是C#语言联系很紧密,也就是说C#里的很多编程方式都可以为Autofac使用,例如可以用Lambda表达式注册组件较......
  • .net core 6 console app use autofac
    Program.cs//Seehttps://aka.ms/new-console-templateformoreinformationusingAutofac;usingAutofac.Extensions.DependencyInjection;usingConsoleApp;using......
  • AutoFac
    AutoFac的配置使用一.什么是AutoFac第三方IOC容器二.优点它是C#语言联系很紧密,也就是说C#里的很多编程方式都可以为Autofac使用,例如可以用Lambda表达式注册组件较......
  • c#中的IOC容器之Autofac容器
    IOC(inversionofcontrol)控制反转以前我们把框架分为三层架构:UI层,BLL层,DAL层。在三层架构中我们,我们是一层一层的调用对象里面的方法,需要实例化对象:类名 对象名=new......
  • WebApi Autofac依赖注入配置
    publicclassAutofacConfig{///<summary>///IOC注册///</summary>publicstaticvoidRegister(){......
  • .Net Core WebApi AutoFac用法
    1.安装Autofac.Extensions.DependencyInjection管理包UI层安装 2.在Program里面配置服务提供工厂  3.在Startup里面添加一个配置容器的方法使用基于扫描程序集......
  • .NET CORE IOC容器和AutoFace 的用法
    一IOC默认的IOC的三种注入方式  通过构造函数获取到实例          二IOC默认的IOC的三种注入方式......