English 中文(简体)
统一防弹和构造方法的替代办法
原标题:Alternative to TheDefault and ConstructedBy methods in Unity

基本上,我要说的是HttpContext的内容。 现任。 用户 我请我发言。 这是结构法典:

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

我如何在团结中实现同样的目标?

最佳回答

可以用团结2.0的投射法或前几版团结法的静态扩展来做到这一点。

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

暂无回答




相关问题
Validate interface using IoC

I have a domain model that uses IoC with Microsoft Unity. For the validation I use VAB and I decorate the interface, not the entity. The code the following: interface IJob : IValidable { [...

Injecting Subsonic SimpleRepository class to controller

I m tryingot get started with IoC, I have an MVC project in which is use subsonic, I m trying to inject subsonic simplerepository to my controllers but I m getting this error: StructureMap Exception ...

Unity view Registered Types during debug

Possibly a stupid question, but during debug I simply want to see the types that have been registered with my Unity container. I have tried going through the container in the watch window, but can t ...

ASP.NET MVP Injecting Service Dependency

I have an ASP.NET page that implements my view and creates the presenter in the page constuctor. Phil Haack s post providing was used as the starting point, and I ll just the examples from the post ...

Unity Container Disposing and XML Web Service

I am registering some wrapers over un-managed objects in container. How can I dispose of them at the end of the container s lifetime? Please bear in mind I have an XML Web service.

热门标签