Is it possible to change the implementation of a interface for a specific scope? What I would like to have is a default implementation of a "ILogService" which will log data to disk. But for the task scheduler I use "IServiceScopeFactory.CreateScope()" to resolve the implementation of the tasks, but in this case I would like to use a different implementation for logging so the data will end up in my Database.
interface ILogService { void Write(string text); }
This has a default implementation
class LogDisk : ILogService { void Write(string text) { ... } }
But when I do a GetService() in a scope where class x is using ILogService I would like to use this
class LogTask : ILogService { void Write(string text) { ... } }
Is it possible to change the implementation of a interface for one specific scope?
Example
public class TaskFactory : ITaskFactory
{
private IServiceScopeFactory _serviceScopeFactory;
public TaskFactory(IServiceScopeFactory serviceScopeFactory)
{
this._serviceScopeFactory = serviceScopeFactory;
}
public ITaskDefinition GetDefinition(ETaskType taskType)
{
using (var scope = this._serviceScopeFactory.CreateScope())
{
var provider = scope.ServiceProvider.GetService<ITaskX>();
return null;
}
}
}
Within the tasks, I don't want to look at how this should be implemented if I should use implementation A or B. Each task has its own dependencies, that is what I would like the dependency injection to take care of. But the only implementation I would like to change is the one for ILogService.