You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
}
}
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?
The text was updated successfully, but these errors were encountered:
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?
The text was updated successfully, but these errors were encountered: