I have a service called MyRepository, I was to write my own custom provider for MyRepository. How can I pass an argument to the constructor of MyRepository using the provider?
This is my code:
public class MyRepository : IMyRepository
{
private string _path;
public MyRepository(string path)
{
_path = path;
}
// more code...
}
public class MyRepositotyProvider : Provider<IMyRepositoty>
{
public static IMyRepositoty CreateInstance()
{
return new MyRepository(/*how to pass an argument?*/);
}
protected override IMyRepositoty CreateInstance(IContext context)
{
// I need to pass path argument?
return CreateInstance();
}
}
// I need to pass the path argument to the provider
var instance = OrganisationReportPersisterProvider.CreateInstance(/*pass path arg*/);
Based on your comment you could consider using an abstraction that can be passed to lower layers
and can then be resolved via the context in the provider
The implementation will live in a higher layer and allows for decoupling.
Ideally the repository could have been refactored to depend on the abstraction
and avoid the need to have a provider to begin with.