I'm trying to find a good way of doing this. Rather than typing a whole lot, let me demonstrate what I'm trying to accomplish.
Goal
public class MyService<IMyProviderInterface>
{
private readonly IMyProviderInterface _provider;
public MyService(IMyProviderInterface provider)
{
_provider = provider;
}
public IAsynEnumerable<SomeDTO> GiveMeSomething
{
return _myprovider.INeedSomething();
}
public async Task<int> GenericCommonMethodInThisService()
{
return 1+1;
}
}
Then when I want to use this service I will inject it into another service specifying which "provider" to use:
public class SomeOtherClass
{
public SomeOtherClass (MyService<ConcreteImplOfMyInterface1> myService1)
{
// assignment to private readonly
}
}
And even allowing me to inject multiple instances
public class SomeOtherClass
{
public SomeOtherClass (MyService<ConcreteImplOfMyInterface1> myService1, MyService<ConcreteImplOfMyInterface2> myService2)
{
// assignment to private readonly
}
}
#1 Attempt
public class SomeService<T> where T : IMyProviderInterface, new()
{
public IAsynEnumerable<SomeDTO> GiveMeSomething()
{
var instance = new T();
await foreach(var item in instance.GiveMeSomething()) { // .... }
}
}
which would be used as var service = new SomeService<ConcreteImplOfMyInterface1>(); and is working fine. However I really don't like making the "instance" every time my method is called.
Is there perhaps another way of getting this done?
I'm trying to find a good way of doing this. Rather than typing a whole lot, let me demonstrate what I'm trying to accomplish.
Goal
Then when I want to use this service I will inject it into another service specifying which "provider" to use:
And even allowing me to inject multiple instances
#1 Attempt
which would be used as
var service = new SomeService<ConcreteImplOfMyInterface1>();and is working fine. However I really don't like making the "instance" every time my method is called.Is there perhaps another way of getting this done?