asp.net-core – 在运行时更改注入的对象
发布时间:2020-05-23 22:00:36 所属栏目:asp.Net 来源:互联网
导读:我想拥有IUserRepository的多个实现,每个实现都可以使用MongoDB或任何SQL数据库的数据库类型.为此,我有ITenant接口,它具有连接字符串和其他租户配置.租户已被注入IUserRepository MongoDB或任何SQLDB实现.我需要知道的是如何正确地更改注入的存储库以选择租户
|
我想拥有IUserRepository的多个实现,每个实现都可以使用MongoDB或任何SQL数据库的数据库类型.为此,我有ITenant接口,它具有连接字符串和其他租户配置.租户已被注入IUserRepository MongoDB或任何SQLDB实现.我需要知道的是如何正确地更改注入的存储库以选择租户的数据库. 接口 public interface IUserRepository
{
string Login(string username,string password);
string Logoff(Guid id);
}
public class User
{
public Guid Id { get; set; }
public string Username { get; set; }
public string Password { get; set; }
public string LastName { get; set; }
public string FirstName { get; set; }
}
public interface ITenant
{
string CompanyName { get; }
string ConnectionString { get; }
string DataBaseName { get; }
string EncriptionKey { get; }
}
重要的是要知道租户ID已通过标头请求传递给API StartUp.cs // set inject httpcontet to the tenant implemantion services.AddTransient<IHttpContextAccessor,HttpContextAccessor>(); // inject tenant services.AddTransient<ITenant,Tenant>(); // inject mongo repository but I want this to be programmatically services.AddTransient<IUserRepository,UserMongoRepository>(); 示例Mongo实现 public class UserMongoRepository : IUserRepository
{
protected ITenant Tenant
public UserMongoRepository(ITenant tenant) :
base(tenant)
{
this.Tenant = tenant;
}
public string Login(string username,string password)
{
var query = new QueryBuilder<User>().Where(x => x.Username == username);
var client = new MongoClient(this.Tenant.ConnectionString);var server = client.GetServer();
var database = client.GetServer().GetDatabase(this.Tenant.DataBaseName);
var user = database.GetCollection<User>.FindAs<User>(query).AsQueryable().FirstOrDefault();
if (user == null)
throw new Exception("invalid username or password");
if (user.Password != password)
throw new Exception("invalid username or password");
return "Sample Token";
}
public string Logoff(Guid id)
{
throw new NotImplementedException();
}
}
承租人 public class Tenant : ITenant
{
protected IHttpContextAccessor Accesor;
protected IConfiguration Configuration;
public Tenant(IHttpContextAccessor accesor,IDBConfiguration config)
{
this.Accesor = accesor;
this.Configuration = new Configuration().AddEnvironmentVariables();
if (!config.IsConfigure)
config.ConfigureDataBase();
}
private string _CompanyName;
public string CompanyName
{
get
{
if (string.IsNullOrWhiteSpace(_CompanyName))
{
_CompanyName = this.Accesor.Value.Request.Headers["Company"];
if (string.IsNullOrWhiteSpace(_CompanyName))
throw new Exception("Invalid Company");
}
return _CompanyName;
}
}
private string _ConnectionString;
public string ConnectionString
{
get
{
if (string.IsNullOrWhiteSpace(_ConnectionString))
{
_ConnectionString = this.Configuration.Get(this.CompanyName + "_" + "ConnectionString");
if (string.IsNullOrWhiteSpace(_ConnectionString))
throw new Exception("Invalid ConnectionString Setup");
}
return _ConnectionString;
}
}
private string _EncriptionKey;
public string EncriptionKey
{
get
{
if (string.IsNullOrWhiteSpace(_EncriptionKey))
{
_EncriptionKey = this.Configuration.Get(this.CompanyName + "_" + "EncriptionKey");
if (string.IsNullOrWhiteSpace(_EncriptionKey))
throw new Exception("Invalid Company Setup");
}
return _EncriptionKey;
}
}
private string _DataBaseName;
public string DataBaseName
{
get
{
if (string.IsNullOrWhiteSpace(_DataBaseName))
{
_DataBaseName = this.Configuration.Get(this.CompanyName + "_" + "DataBaseName");
if (string.IsNullOrWhiteSpace(_DataBaseName))
throw new Exception("Invalid Company Setup");
}
return _DataBaseName;
}
}
}
调节器 public class UsersController : Controller
{
protected IUserRepository DataService;
public UsersController(IUserRepository dataService)
{
this.DataService = dataService;
}
// the controller implematation
}
解决方法您可以尝试注入工厂而不是实际的存储库.工厂将负责根据当前用户身份构建正确的存储库.它可能需要更多的锅炉板代码,但它可以实现你想要的.一点点继承甚至可能使控制器代码更简单. (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc – ICommandHandler / IQueryHandler with asy
- active-directory – 你能通过ADFS获得用户列表吗?
- 通过Asp.net中Button的CommandArgument传递多个参数
- 准备ASP.Net网站进行渗透测试
- asp.net-mvc-3 – Structuremap,AutoFac或Ninject,哪一个适
- asp.net – 没有参数的WebResource.axd请求 – 这是一个无效
- asp.net – 防止页面在回发后滚动
- asp.net-mvc-3 – 如何增加会话超时MVC 3
- asp.net-mvc – asp.NET:未知长度的MVC路径
- asp.net-mvc – 追加?param =到mvc路由
推荐文章
站长推荐
- WF4 – 在asp.net中显示工作流图像,并突出显示活
- asp.net-web-api – 哪个优先级,ASP.NET Web Api
- 在ASP.Net应用程序中执行Server.MapPath的最有效
- asp.net-mvc – 官方的“如何”MVC编辑器/显示模
- asp.net-mvc – 创建一个texarea帮助器,它将视图
- asp.net – web.config文件中的特殊字符
- 在asp.net控件的style属性中使用DataBinder.Eval
- asp.net – 我可以使用一种模式来编辑MVC3应用程
- 在对ASP.NET MVC Action的AJAX请求期间有网络请求
- 我不能在ASP.NET MVC中做的事情
热点阅读
