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

1.8k views Asked by At

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?

3

There are 3 answers

0
BFree On BEST ANSWER

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));
4
Wiktor Zychla On
container.RegisterInstance<IIdentity>(...);
0
neontapir On

I believe a static factory extension would do it. I'm rusty on Unity. Seeman's Dependency Injection in .NET is a good resource for situations like this.