简体   繁体   English

C#如何根据使用相同接口实现的多个类来决定在依赖注入中运行哪个类

[英]C# How to decide which class to run in Dependency Injection based on multiple classes implement using same interface

I am working on .NET 6.0 application.我正在开发 .NET 6.0 应用程序。

I have an Interface IDestinationFileNaming that is implemented by multiple classes.我有一个由多个类实现的接口IDestinationFileNaming How I Can choose which class to call based on they implementing same interface.我如何根据它们实现相同的接口来选择调用哪个类。 Does Iterface where T :Class plays role here? Iterface where T :Class 在这里起作用吗?

public interface IDestinationFileNaming
{
    string Generate();
}

ClassA That Implements above interface实现上述接口的ClassA

public class ClassA : BaseFileNaming, IDestinationFileNaming
{
    
    public ClassA()
        : base() { }
 

    public override string Generate()
    {
        string fileName = string.Empty;

        try
        {
            fileName = BaseName + "AIM_UBW";
        }
        catch (Exception ex)
        { }

        return fileName;
    }

I ClassB That Implements above interface实现上述接口的ClassB

 public class ClassB : BaseFileNaming, IDestinationFileNaming
{
    public ClassB()
       : base() { }

    public override string Generate()
    {
        string fileName = string.Empty;

        try
        {
            fileName = BaseName + "DDS_UBW";
        }
        catch (Exception ex)
        { }

        return fileName;
    }
}

I have register dependencies in DI Container as我在 DI Container 中注册依赖项为

services.AddScoped<IDestinationFileNaming, ClassA>();
services.AddScoped<IDestinationFileNaming, ClassB>();

ClassC

I want to run ClassA here...我想在这里运行ClassA ...

public class ClassC{

   private readonly IDestinationFileNaming _destinationFileNaming;
   
   public ClassC(IDestinationFileNaming destinationFileNaming)         
        :base()
    {
        this._destinationFileNaming = destinationFileNaming;
    }

    public void Run(){
       // Hot to call ClassA from using above Interface?
    }
} 

I have found two approaches我找到了两种方法

approach 1

register dependencies注册依赖项

 services.AddScoped<IDestinationFileNaming, ClassA>();
 services.AddScoped<IDestinationFileNaming, ClassB>();

Class C

public class ClassC{公共类 ClassC{

private readonly IEnumerable _destinationFileNaming;私有只读 IEnumerable _destinationFileNaming;

public ClassC(IEnumerable destinationFileNaming) public ClassC(IEnumerable destinationFileNaming)
:base() { this._destinationFileNaming = destinationFileNaming; :base() { this._destinationFileNaming = destinationFileNaming; } }

public void Run(){
   
}

private dynamic ResolveObject()
    {
        var x = _destinationFileNamings.SingleOrDefault(_ => _.GetType() == typeof(ClassA));

        var y = x.Generate();

        var a = _destinationFileNamings.SingleOrDefault(_ => _.GetType() == typeof(ClassB));

        var b = a.Generate();

        return x;
    }
} 

option 2

interface

public interface IDestinationFileNaming<T> where T : class
{
    string Generate();
}

register in DI在 DI 注册

services.AddScoped<IDestinationFileNaming<ClassA>, ClassA>();
services.AddScoped<IDestinationFileNaming<ClassB>, ClassB>();

Class C

 public class ClassC{

 private readonly IDestinationFileNaming<ClassA> _destinationFileNaming;

 public ClassC(IDestinationFileNaming<ClassA> destinationFileNaming)         
     :base()
 {
     this._destinationFileNaming = destinationFileNaming;
 }

 public void Run(){
    x = _destinationFileNaming.Generate();
 }  
} 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用带有C#的依赖注入的通用类 - Using Generic Classes with Dependency Injection with C# 如何区分()列出包含实现接口的类的列表? C# - How to Distinct() List which contains classes that implement interface? C# C# 依赖注入 - 注入采用相同接口作为参数的接口 - C# Dependency Injection - injecting interface that takes the same interface as a parameter 在C#中使用依赖注入时,为什么调用接口方法会自动调用实现类的方法? - When using dependency injection in C#, why does calling an interface method automatically call the implemented class' method? 具有继承自相同接口 Console .Net Core 的类的依赖注入 - Dependency Injection with classes which inherit from the same interface Console .Net Core C#依赖注入多个相同泛型类型的实现 - C# dependency injection multiple implementation of the same Generic Type c#接口依赖注入错误 - c# dependency injection error with interface 接口问题c#,以及依赖注入 - Interface problem c#, and dependency injection 如何在 C# .net 6 应用程序中使用依赖注入来传递具有相同接口的相同 object 的不同实例? - How do I use Dependency Injection in C# .net 6 application to pass in different instances of the same object with same interface? 如何从单个类中的静态运行非静态方法并使用依赖注入 C# - How to run non static method from static inside single class and use dependency injection C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM