如何根据当前线程文化检索实例

时间:2016-01-14 15:52:31

标签: c# nancy light-inject

设置:

LightinjectComposition.cs

to_s(2)

IdentityLocator检索同一个类的实例,该实例有两个方法:

  • Get():根据Thread.CurrentThread.CurrentCulture检索内部类的实例。
  • Get(CultureInfo):根据CultureInfo检索内部类的实例。

我有两个项目:一个用于api(称为Api),另一个用于DI容器配置(称为Dependency,其中包含对其他项目的引用 - NHibernate映射,服务等 - 并由API引用) 。在名为Dependency的项目中,LightinjectComposition.cs所在的位置。

问题是,在NancyFX Bootstrapper中,当前文化是根据请求中收到的值设置的,但是在解析实例后线程的文化不会改变。

有没有办法使用当前文化检索实例,或者我做错了(关于IoC模式)?

在NancyFX Bootstrapper中,我设置了这样的文化:

serviceRegistry.Register<IdentityLocator>(f => IdentityLocator.GetInstance());
serviceRegistry.Register<IIdentity>(f => f.GetInstance<IdentityLocator>().Get());

// I could use this too:
// serviceRegistry.Register<IIdentity>(f => f.GetInstance<IdentityLocator>().Get(Thread.CurrentThread.CurrentCulture));

// There are N classes that have a dependency to IIdentity
serviceRegistry.Register<InterfaceN, ClassN>();

也许我应该将IdentityLocator的实例传递给依赖类而不是IIdentity?

感谢阅读!

0 个答案:

没有答案