Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unity equivalent for Ninject's Bind.ToMethod of IPrincipal,IIdentity

I'm trying to replicate the following Ninject syntax in Unity, but not having any luck:

Bind<IIdentity>().ToMethod(c => HttpContext.Current.User.Identity);

I'm thinking it ought to look something like:

IUnityContainer container;
...
container.RegisterType<IIdentity>(HttpContext.Current.User.Identity);

How should it be?

like image 602
Alfred Avatar asked Nov 23 '11 17:11

Alfred


2 Answers

While neontapir's answer could work, that extension method is Obsolete. The correct way to do this now would be to use an InjectionFactory:

container.RegisterType<IIdentity>(new InjectionFactory(u => HttpContext.Current.User.Identity));
like image 92
BFree Avatar answered Nov 16 '22 20:11

BFree


container.RegisterInstance<IIdentity>(...);
like image 24
Wiktor Zychla Avatar answered Nov 16 '22 21:11

Wiktor Zychla