Skaparate
Skaparate

Reputation: 489

How to retrieve instance based on current thread culture

The setup:

LightinjectComposition.cs

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 retrieves an instance of the same class, which has two methods:

I have two projects: one for the api (called Api) and another for the DI container configuration (called Dependency, which contains references to the other projects - NHibernate maps, services, etc. - and is referenced by the API). In the project called Dependency is where the LightinjectComposition.cs resides.

The problem is that, in NancyFX Bootstrapper, the current culture is set based on values received in the request, but the thread's culture does not change after the instance is resolved.

Is there any way to retrieve an instance using the current culture or I am doing it wrong (in regards of IoC patterns)?

In the NancyFX Bootstrapper, I set the culture like this:

pipelines.BeforeRequest += context =>
{
    Thread.CurrentThread.CurrentCulture = context.Culture;
    return null;
};

Maybe I should pass an instance of IdentityLocator to the dependant classes instead of an IIdentity?

Thanks for reading!

Upvotes: 0

Views: 245

Answers (0)

Related Questions