views:

21

answers:

1

Basically what i'd like to do is injecting what's in HttpContext.Current.User everytime I ask for an IPrincipal. This is the code in StructureMap:

ForRequestedType<IPrincipal>()
  .CacheBy(InstanceScope.Hybrid)
  .TheDefault.Is.ConstructedBy(ctx => HttpContext.Current.User)

How do I achieve the same in Unity?.

+1  A: 

this can be done using InjectionFactory in Unity 2.0 or StaticFactoryExtension in previous version of Unity.

 var container = new UnityContainer(); // unity 2.0
 container.RegisterType<IPrincipal>(new InjectionFactory(_ => HttpContext.Current.User));
desco