Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I inject service dependencies into MVC sitemap DynamicNodeProviderBase using StructureMap

Consider the following code:

    public class InboxMenuItemDynamicProvider : DynamicNodeProviderBase
    {
      private IMyService _myService { get; set; }

      public InboxMenuItemDynamicProvider(IActionService actionService)
      {
         _myService = myService;
      }

      public override IEnumerable<DynamicNode> GetDynamicNodeCollection()
      { 
        // use _myService here....e.g. db access 
      }
    }
}

How do I go about injecting a service dependency into a Mvc Sitemap DynamicNodeProvider using StructureMap and MVC3? I'm using the MVC3 dependency resolver.

However, on running the app, I get a 'no default constructor' error. I need to somehow inject service dependencies into the provider, but I'm at a total loss as to where/how I can inject them. I don't even know if its possible as Mvc Site Map might be outside of the depency resolver.

like image 969
jaffa Avatar asked Jan 22 '26 10:01

jaffa


1 Answers

See this link: http://mvcsitemap.codeplex.com/discussions/263971

I think you could just use:

private IMyService _myService 
{ 
    get 
    { 
        return DependencyResolver.Current.GetService<IMyService>(); 
    } 
}

and do nothing specific in your constructor

like image 98
thekip Avatar answered Jan 25 '26 17:01

thekip



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!