I am working on .NET 6.0 application.
I have an Interface IDestinationFileNaming
that is implemented by multiple classes. How I Can choose which class to call based on they implementing same interface. Does Iterface where T :Class plays role here?
public interface IDestinationFileNaming
{
string Generate();
}
ClassA
That Implements above interface
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
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
services.AddScoped<IDestinationFileNaming, ClassA>();
services.AddScoped<IDestinationFileNaming, ClassB>();
ClassC
I want to run ClassA
here...
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?
}
}