Skip to content
This repository has been archived by the owner on Aug 8, 2019. It is now read-only.

Support standard and custom ViewLocationConventions #28

Open
devuxer opened this issue Mar 2, 2014 · 8 comments
Open

Support standard and custom ViewLocationConventions #28

devuxer opened this issue Mar 2, 2014 · 8 comments

Comments

@devuxer
Copy link

devuxer commented Mar 2, 2014

I'm not sure if this is feasible, but it would be extremely cool to have support for the entire array of ViewLocationConventions configured in the Nancy bootstrapper.

For example, I was trying to organize my project by feature. This is similar to the Module Path Convention, except all the module folders are combined into a single folder called "Features".

I was able to implement this quite easily by creating the following override to ApplicationStartup:

    protected override void ApplicationStartup(TinyIoCContainer container, IPipelines pipelines)
    {
        Func<string, dynamic, ViewLocationContext, string> featureConvention = (viewName, model, context) =>
            string.IsNullOrWhiteSpace(context.ModulePath) ? null : "Features" + context.ModulePath + "/" + viewName;
        Conventions.ViewLocationConventions.Insert(0, featureConvention);
    }

This seems to work well so far, except ReSharper now thinks all my view names are wrong. If there were some way to have ReSharper actually perform the same logic as Nancy when evaluating view names, it would be a great enhancement. I believe it would also solve #4.

@hmemcpy
Copy link
Owner

hmemcpy commented Mar 2, 2014

This is a great idea, and you're right, this would solve #4 as well, however I don't immediately see how this would be possible. ReSharper's analysis depends on things that can be statically resolved (or at least, guessed), and those conventions are not resolved until runtime.

I'll try to play around with the idea. Basically, defining custom (static) conventions is easy, i.e. supporting "~/Features/ViewName.cshtml" can be done even today (just need to modify an xml file). I might have an idea how it's possible to solve this, but I'll need to spike it. Stay tuned :)

@hmemcpy
Copy link
Owner

hmemcpy commented Mar 2, 2014

Btw, just to make sure, in your convention, did you intend to concat "Features" with the module path? Or are you missing a "/" in there? (not that it matters, just making sure)

@devuxer
Copy link
Author

devuxer commented Mar 2, 2014

You're right...I was missing the "/".

@devuxer
Copy link
Author

devuxer commented Mar 2, 2014

I should mention, though, that this is still a work in progress. As of right now, I have these three conventions:

Func<string, dynamic, ViewLocationContext, string> featureModuleConvention = (viewName, model, context) =>
    "Features" + "/" + context.ModuleName + "/" + viewName;
Func<string, dynamic, ViewLocationContext, string> featureSharedConvention = (viewName, model, context) =>
    "Features" + "/" + "Shared" + "/" + viewName;
Func<string, dynamic, ViewLocationContext, string> featureConvention = (viewName, model, context) =>
    "Features" + "/" + viewName;

This allows me to completely avoid typing the word "Features" when specifying a view location.

@hmemcpy
Copy link
Owner

hmemcpy commented Mar 2, 2014

I'm thinking, that as a preliminary step, there could be some special "metadata", perhaps a custom attribute or a specially-formatted comment, so the plugin could pick it up and added to the ViewLocationFormats collection.
Eventually, after I teach the plugin to "understand" what each of the arguments are (context, viewName, etc), then perhaps it would be possible to construct those programmatically, filling in the missing pieces. But like I said, it requires some research and spiking :)

@devuxer
Copy link
Author

devuxer commented Mar 2, 2014

I don't know the first thing about writing a ReSharper plug-in, but if you can point me to the relevant code files(s), I'd like to take a look at how it works now. Maybe it will spark an idea.

@hmemcpy
Copy link
Owner

hmemcpy commented Mar 2, 2014

I wish it was that easy, I have no idea myself. I'm going to bug the guy responsible for ASP.NET MVC in R#, maybe he could hint as to how (and if) this is possible :)

@devuxer
Copy link
Author

devuxer commented Mar 2, 2014

Oh okay :) I'll keep my fingers crossed that there's a way (and that it's not too hard).

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants