我有一个NHibernate.Cfg.Configuration
提供商。
ConfigurationProvider
public class ConfigurationProvider : Provider<Configuration> {
public class ConfigurationProvider(string connectionString
, Assembly mappings) {
ConnectionString = connectionString;
Mappings = mappings;
}
protected override Configuration CreateInstance(IContext context) {
var c = new Configuration().Configure();
c.AddAssembly(Mappings);
c.Properties["connection.connection_string"] = ConnectionString;
return c;
}
private readonly string ConnectionString;
private readonly Assembly Mappings;
}
其连接字符串来自Configurationservice.BuildConnectionString()
我的应用程序。
ConfigurationService
public class ConfigurationService {
public ConfigurationService(ICurrentUser user) { CurrentUser = user; }
public string BuildConnectionString() {
return string.Format(DefaultConnectionString
, CurrentUser.DatabaseInstanceName
, CurrentUser.Login
, CurrentUser.Password);
}
private readonly ICurrentUser CurrentUser;
}
这反过来又依赖于CurrentUser
来冒充。
当前用户
public class CurrentUser : ICurrentUser {
public CurrentUser(string dbinstance, string login, string password) {
DatabaseInstanceName = dbinstance;
Login = login;
Passwword = password;
}
public readonly string DatabaseInstanceName;
public readonly string Login;
public readonly string Password;
}
所以手动进我的依赖将如下所示:
string dbInstance = authenticationDialog.DatabaseInstanceName;
string login = authenticationDialog.Login;
string password = authenticationDialog.Password;
var nHibernateConfigurationProvider =
new ConfigurationProvider(
new ConfigurationService(
new CurrentUser(dbInstance, login, password)).BuildConnectionString()
, Assembly.GetExecutingAssembly());
问题是,我不知道如何绑定,使用Ninject,该ConfigurationService.BuildConnectionString()
方法来解决string
中使用构造函数的参数ConfigurationProvider.ctor()
。
推进一步,我怎么告诉Ninject到实际结合这些类型只有当用户通过验证?
这里是我到目前为止
// Wait until user is authenticated before continuing to bind???
Bind<string>().ToMethod(ctx => ctx.Request.???.BuildConnectionString()).WhenInjectedInto<ConfigurationProvider>().InSingletonScope();
Bind<Assembly>().ToConstant<Assembly>(Assembly.GetExecutingAssembly().WhenInjectedInto<ConfigurationProvider>();