Patrick Patrick - 1 year ago 138
C# Question

Nancy, TinyIoC - Stuck in a Box

I've been using Nancy for a while now and I think I've worked myself into a box and cannot think outside of it enough to see my mistakes in my use of Nancy. The option I do see I don't like very much. If there is some functionality I missed that someone can help me with, or alternative methods that I don't list well any help would be greatly appreciated.

Problem Setup

I am creating a small web application where I have the normal fixtures. Layout Pages and Content Pages and even a few partial views floating around.

I want to create a dynamic menu system that'll change its menu options and general layout based on the user logged in. So a user whose primary job is Accounting might see more billing features, people who Schedule might see more scheduling features, etc.

Problem Itself

Problem is, I'm having difficulty getting a Dynamic Menu to Fire using Nancy and TinyIoC.

Originally I thought, "I'll just setup a Menu module, and RenderAction, or equivalent, and I'll be good to go", except Nancy doesn't like this. That's fine because my alternative is... Talk to TinyIoC on the Layout page in a code snippet that'll Resolve my IMenuManager and I can call its GenerateMenu function there with a passed in user Id or what have you. I'll take the returned object and pass it to an @Html.Partial('path', model) and I'm golden

Except you really can't talk to TinyIoC in this fashion. You don't get easy access to Resolve an Interface to an implementation unless it is an extension of a Nancy Module itself.

Other Options I considered

  1. I gave thought to using JavaScript / jQuery to do an AJAX call to a MenuModule and get the information that way, but this feels dirty and I am not a fan of this.

  2. Another other option I gave some thought to was wrap all Page Models going down with a PageModel.MenuModel, and PageModel.RealPageModel. This also feel really dirty, and I'm not a fan.

Is there some other out option that exists that I am just not seeing? Am I going to have to ditch TinyIoC and re-implement the whole thing another way?

Answer Source

I guess you can take 1 of 2 approaches, as you've mentioned.

I tend to use a front-end framework to manage these sorts of things, for me it's either riot.js or vue.js, but same thing would apply regardless if you were using angular/react, etc.

Basically you would make a call to an endpoint to return a json result, this would look at the user and call a factory to get a menu service based on the user information and then the menu service would be responsible for creating the model of the resulting menu. The client would then build its own view based on the response.

If you're pushing the menu down with every request you could do something similar using RenderPartial, the approach is the same where your model contains a Menu and a MenuView based on the result of the service.

Use the factory, create/resolve the correct service, generate menu, attach the menu to the response model along with everything else, then render your partial:

@Html.Partial(Model.MenuView, Model.Menu)

I use this approach for a personal project of mine.

Let me know if you need me to explain further.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download