开发者

How do I access the nested container within a custom resolver?

I'm using a nested container to provide access to contextual items, as outlined here.

In Application_Start, I'm creating my maps in a similar manor as:

Mapper.CreateMap<MyObject, MyMappedObject>()
    .ForMember(dest => dest.Url, opt => opt.ResolveUsing<MyResolver>());


public class MyResolver<MyObject, string> {
  protected override string ResolveCore(MyObject source) {
    var urlHelper = ObjectFactory.Container.GetNestedContainer().GetInstance<UrlHelper>();
    return ur开发者_JS百科lHelper.GetMyUrl(source);
  }
}

Unfortunately, this only gets access to the container, not the nested container. Is it possible to access contextual items in a custom resolver? If so, is there a nice way of doing so with StructureMap?

As a side note, I am able to get to the nested container within the custom resolver by the HttpContext.Current.Items collection. This seems like an undesirable method to me, though.


I've always just put a wrapper around the item that was stuffed into HttpContext.Items.

public static class Current
{
  public static IContainer Container
 {     //Throw exception if the item isn't there 
   return (IContainer)HttpContext.Items[yourKey];   
 }
}

Not very pretty, but unless you go with a framework that really embraces IOC, I haven't found a nice way to do it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜