依赖注入和用户特定数据
我有使用 ORM 的应用程序(Nhibernate 但事实并非如此)。
要创建 NH Session,我们需要传递一些信息:用户名、数据库名称等。所以我实现了:
public interface ISettingsManager
{
Settings MySettings {get;set;}
}
public class Settings
{
public string DbUser{get;set;}
public string DbAddress {get;set;}
public string DbPassword{get;set;}
//...
}
public class SessionProvider
{
[Inject]
public ISettingsManager MySettings {get;set;}
public Session CreateSession
{
//Create Session object using settings passed do MySettings via IoC.
}
}
public static Main()
{
// very beggining of my application, bootstrap the DI container
Bind<ISettingsManager>().To<SettingsManagerImpl>();
// Application run
}
我所有的 NHibernate Session Providers 都通过 DI (Ninject) 注入了 ISettingsManager
,这样我就可以简单地使用它。它的工作方式就像是一种伤害,但现在我需要在我的应用程序中支持许多用户,并且问题进入了场景。当应用程序启动时,我无法绑定我的 ISettingsManager,因为我现在不知道哪个用户将登录。
所以问题是,如何在不使用服务位置的情况下以最佳方式实现传递当前登录的用户设置?
I have application that uses ORM (Nhibernate but it's not the case).
To create NH Session we need to pass somewhere: username, database name, etc. So I have implemented:
public interface ISettingsManager
{
Settings MySettings {get;set;}
}
public class Settings
{
public string DbUser{get;set;}
public string DbAddress {get;set;}
public string DbPassword{get;set;}
//...
}
public class SessionProvider
{
[Inject]
public ISettingsManager MySettings {get;set;}
public Session CreateSession
{
//Create Session object using settings passed do MySettings via IoC.
}
}
public static Main()
{
// very beggining of my application, bootstrap the DI container
Bind<ISettingsManager>().To<SettingsManagerImpl>();
// Application run
}
All my NHibernate Session Providers have the ISettingsManager
injected to it via DI (Ninject) so I can simple use it. It works like a harm, but now I need to support many users in my application and the problem goes into the scene. I cant bind my ISettingsManager when applications starts, becauese I dont now wchich user will be logged in.
So the question is, how to implement passing current logged user settings in the best way, without using Service Location?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设我正确理解了这个问题,像单例模式这样的东西可能会有所帮助(参见 http://en.wikipedia.org /wiki/Singleton_pattern)。
一个单例将存储该信息并在请求时提供该信息。其他组件只需要知道如何访问单例,而不需要知道更具体的信息。
为了从单例中获取单个用户设置,将键(如用户 ID)映射到值的字典类型的数据结构可能会很好。
Assuming I understood the problem correctly, something like the singleton pattern could help (see http://en.wikipedia.org/wiki/Singleton_pattern).
One singleton would store the information and give it when requested. Other components would need to know only how to access the singleton, not anything more specific.
For getting individual user settings out the singleton, a dictionary type of data structure that maps keys (like user id) to values could be good.
我将使用单独的数据库进行所有身份验证,然后从该数据库中提取特定于用户的数据库设置。
使用检索到的用户特定设置,我将创建要注入到 ORM 连接进程中的类,并使用构造函数或属性注入来注入它。
I would use a separate DB for all authentications, then pull out the user-specific DB settings from that DB.
Using the user specific setting retrieved, I would create the class you want to inject into your ORM's connection process and inject it using either constructor or property injection.