我正在开发一个Web API,它基于appsettings.json
密钥连接到不同的存储库.这些实现在不同的项目中定义,并共享相同的接口
我使用这个代码在它们之间切换
switch (connector)
{
case "MSSQL":
services.AddScoped<IDocumentRepository, MSSQL.Repositories.DocumentRepository>();
break;
case "MySQL"
services.AddScoped<IDocumentRepository, MySQL.Repositories.DocumentRepository>();
break;
}
我正在寻找一个不是基于switch
或if
声明的解决方案.
会是这样的
services.AddScoped<IDocumentRepository, $"{namespace}.DocumentRepository">();
这个是可能的吗?也许是使用反射?