Ant
no notes

ASP.NET MVC Cascading Resources

I like using resources for text, labels, link and button text etc. in my projects. It provides a consistency of UI, which is key when you are trying to sell a system. It also means I can translate a site to another language without separate pages and all the maintenance that would bring.

One thing that I have come up against is no fall-back or cascade, using the traditional approach, in that, I would like to have a resource which is standard, it would be used if no specific one was available.

For example...

I have a resource file located in my general assembly Antix.Web.PageResources, it has a property for Submit_Save.

<input type="submit" value="@PageResources.Submit_Save" />

If I use the code above then I get the general value for a submit button which saves data to the database "Save". But say I get a client who doesn't want the button to say "Save" and wants it to say "Commit Changes". I cannot change my general assembly as it is used in other projects.. oh no..

What I need is to be able to have a project level resource file which overrides the one in my general assembly, and if the property I am after exists in the overriding resource it is used, if not the one in the general assembly resource is used - Cascading Resources.

This is possible in MVC by virtue of the view model type, an expression target property interface, and a simple extension to retrieve the resource value.

The extension signature makes use of the model type by extending the HtmlHelper<TModel>

public static string PageResourceFor<TModel, TValue>(
    this HtmlHelper<TModel> htmlHelper,
    Expression<Func<IPageResource, TValue>> expression)

The expression is used to get the resource you want to display, targeting an interface which has a set of properties one for each resource.

public interface IPageResource {

    string SubmitSave{ get; }
}

This allows me to use the following call in my view

<input type="submit" value="@Html.PageResourceFor(p => p.SubmitSave)" />

And all the information that my extension needs to provide a cascaded resource value is available.

  1. Model Level - check for a resource which is specific to your model type
  2. Project Level - check for a resource a which is specific to your project
  3. General Assembly Level - if neither exists fall back to the resource in the general assembly

for example then...

public static string PageResourceFor<TModel, TValue>(
    this HtmlHelper<TModel> htmlHelper,
    Expression<Func<IPageResource, TValue>> expression) {

    var resourceName = ExpressionHelper.GetExpressionText(expression);
    switch(typeof(TModel).Name) {
        case "Person":

            if (PersonResources.ResourceManager.GetString(resourceName) != null)
                return PersonResources.ResourceManager.GetString(resourceName);

            break;
    }

    return SiteResources.ResourceManager.GetString(resourceName)
        ?? GeneralPageResources.ResourceManager.GetString(resourceName);
}

You may employ a more sophisticate way of getting your model resources, but this shows the cascade nicely.

Firstly I have only set the General Resource

and the page renders like this

Then I set the resource for the site to 'Commit Changes'

and the page renders like this...

Finaly I set the resource for the model to 'Commit Person Changes'

and, yay, the page renders like this...

Magic :)

Post a Note

(required)

(required never shown)

On Twitter Follow MrAntix on Twitter

15/05/2012
WindowsAzure
Announcing the MEET Windows Azure Event! Streamed online June 7th. Register at http://t.co/bObzTAuL  #MEETAzure #WindowsAzure

10/05/2012
kevinwhinnery
Comparing Titanium and PhoneGap - A common question I get asked at developer events and conferences is how... http://t.co/Zq2eND6B

09/05/2012
brianleroux
PhoneGap goals and philosophy: http://t.co/wkq8wI2T

just now
cbtacy
"Meet Windows Azure" http://t.co/cDE3s5E0 (this should be VERY interesting)

just now
jodonnel
RT @brucedkyle: Join in the ‘Meet Windows Azure’ Event Online June 7 http://t.co/kXxxSOcy #MSDEV