D.R.
D.R.

Reputation: 21194

ASP.NET MVC dependency injection in application events

I've a follow up question to: ASP.NET MVC, set culture after authorization

Our dependency injection composition root is ASP.NET MVC's MvcApplication.Application_Start method. We're also registering for ASP.NET's PostAuthenticateRequest application event in MvcApplication.Init.

We now need to inject an instance of IUserSettingsRepository into our OnPostAuthenticateRequest event handler. How to do that?

Note: we don't want to put a static field into our MvcApplication class, as this is a code smell.

Upvotes: 0

Views: 311

Answers (1)

haim770
haim770

Reputation: 49095

Assuming your Dependency Injection solution is well integrated with ASP.NET MVC, you could simply refer to it using DependencyResolver.Current. for example:

DependencyResolver.Current.GetService<IUserSettingsRepository>();

(Strictly speaking, it's not really 'injecting an instance' but rather resolving the dependency using 'Service Locator' approach)

Upvotes: 1

Related Questions