I'm using ASP.NET core dependency injection in my test project, to set up the text context used by my tests (so I can't use constructor injection here). In ConfigureServices, I register the services, which works fine:
public void ConfigureServices(IServiceCollection services)
{
// Scans assemblies and adds MediatR handlers, preprocessors, and postprocessors implementations to the container.
services.AddMediatR(
typeof(Application.Logic.Queries.FindUserByEmailAddressHandler));
services.AddTransient<ILocalDb, LocalDb>(l => new LocalDb(null));
services.AddTransient<IUnitOfWork, UnitOfWork>(uow => new UnitOfWork(""));
services.AddTransient<IUserRepository, UserRepository>();
}
However, when trying to get an instance of my unit of work, I have a problem:
var localDb = serviceProvider.GetService<ILocalDb>();
var unitOfWork = serviceProvider.GetService<IUnitOfWork>(); <- need to pass constructor parameter
You see, the UnitOfWork constructor accepts a connection string, and I have to pass this connection string coming from localDb (LocalDb creates a test database on the fly).
In StructureMap I could pass a parameter to the constructor when getting an instance as follows:
x.For<IUnitOfWork>().Use<UnitOfWork>().Ctor<string>().Is(localDb.ConnectionString); });
How can I do this with ASP.NET Core dependency injection?