I need to be able to pass a connection string into some of my service implementations. I am doing this in the constructor. The connection string is configurable by user will be
To pass runtime parameter not known at the start of the application you have to use the factory pattern. You have two options here
factory method
services.AddTransient>((provider) =>
{
return new Func(
(connectionString) => new NestedService(connectionString)
);
});
and inject the factory method in your service instead of INestedService
.
public class RootService : IRootService
{
public INestedService NestedService { get; set; }
public RootService(Func nestedServiceFactory)
{
NestedService = nestedServiceFactory("ConnectionStringHere");
}
public void DoSomething()
{
// implement
}
}
or resolve it per call
public class RootService : IRootService
{
public Func NestedServiceFactory { get; set; }
public RootService(Func nestedServiceFactory)
{
NestedServiceFactory = nestedServiceFactory;
}
public void DoSomething(string connectionString)
{
var nestedService = nestedServiceFactory(connectionString);
// implement
}
}
factory class
public class RootServiceFactory : IRootServiceFactory
{
// in case you need other dependencies, that can be resolved by DI
private readonly IServiceCollection services;
public RootServiceCollection(IServiceCollection services)
{
this.services = services;
}
public CreateInstance(string connectionString)
{
// instantiate service that needs runtime parameter
var nestedService = new NestedService(connectionString);
// resolve another service that doesn't need runtime parameter
var otherDependency = services.GetService()
// pass both into the RootService constructor and return it
return new RootService(otherDependency, nestedDependency);
}
}
and inject IRootServiceFactory
instead of your IRootService
.
IRootService rootService = rootServiceFactory.CreateInstance(connectionString);