Creating Strongly Typed Resources from Db Resources - RickStrahl/Westwind.Globalization GitHub Wiki
The Web Resource Editor form allows you to create strongly typed resources for your database resources. The Create Strongly Typed Classes option goes through all the resources in your database table and creates strongly typed .NET classes in a file that is specified in the DbResourceProvider configuration settings by default.
<add key="StronglyTypedGlobalResource" value="~/Properties/Resources.cs" />
<add key="ResourceBaseNamespace" value="WebApplication1.Properties" />
This can be overridden by the Class Export user interface which allows specifying these values to write out resources to any location and namespace:
You specify the filename in your project and the namespace to generate it to.
The generated resources can use either the ASP.NET resource provider (which uses whatever provider is configured, directly access the DbResourceManager, or good old Resx resources which is configurable with a global flag that can be set on application startup.
Both DbResourceManager
and Resx
modes allow you use database resources in non-Web applications.
Here's what a generated resource class look like.
namespace WebApplication1.Properties
{
public static class GeneratedResourceSettings
{
// You can change the ResourceAccess Mode globally in Application_Start
// Options: AspNetResourceProvider, DbResourceManager, Resx
public static ResourceAccessMode ResourceAccessMode =
ResourceAccessMode.AspNetResourceProvider;
}
public class Resources
{
// generated for Resx Compatibility - ResourceAcessMode.Resx
public static ResourceManager ResourceManager
{
get
{
if (object.ReferenceEquals(resourceMan, null))
{
var temp = new ResourceManager("Westwind.Globalization.Sample.Properties.Resources",
typeof(Resources).Assembly);
resourceMan = temp;
}
return resourceMan;
}
}
private static ResourceManager resourceMan = null;
public static System.String NameIsRequired
{
get
{
return GeneratedResourceHelper.GetResourceString("Resources",
"NameIsRequired",
ResourceManager,
GeneratedResourceSettings.ResourceAccessMode);
}
}
public static System.String Today
{
get
{
return GeneratedResourceHelper.GetResourceString("Resources",
"Today",ResourceManager,
GeneratedResourceSettings.ResourceAccessMode);
}
}
public static System.String Yesterday
{
get
{
return GeneratedResourceHelper.GetResourceString("Resources",
"Yesterday",ResourceManager,
GeneratedResourceSettings.ResourceAccessMode);
}
}
public static System.String AddressIsRequired
{
get
{
return GeneratedResourceHelper.GetResourceString("Resources",
"AddressIsRequired",ResourceManager,
GeneratedResourceSettings.ResourceAccessMode);
}
}
public static System.Drawing.Bitmap FlagPng
{
get
{
return (System.Drawing.Bitmap)
GeneratedResourceHelper.GetResourceObject("Resources",
"FlagPng",ResourceManager,
GeneratedResourceSettings.ResourceAccessMode);
}
}
}
}
In a real application you will likely have many ResourceSets and thereform many resource classes, each class name reflecting the ResourceSet name. Since these files are generated and not meant to be modified, all the strongly typed resource classes are generated into a single file to keep project bloat to a minimum.
The strongly typed resources can then be used in any ASP.NET application:
<div class="statusbar">@Resources.Today</div>
<div class="statusbar"><%: WebApplication1.Properties.Resources.Today %></div>
string today = Resources.Today
If you use strongly typed resources from the database, you have to recreat the resources whenever you add new resources to the database, so this is an ongoing process.
Resources can be generated from the Web Resource Admin form using the Create Class option that pops up the Create Strongly Typed Classes dialog shown earlier.
Alternately you can use the StronglyTypeResources
class to generate the resources programmatically. The following is the code that is used to create the resources from the Web interface:
// Callback method used from the Web Admin Form
[CallbackMethod]
public bool CreateClass(string filename = null, string nameSpace = null)
{
var config = DbResourceConfiguration.Current;
StronglyTypedResources strongTypes =
new StronglyTypedResources(Context.Request.PhysicalApplicationPath);
if (string.IsNullOrEmpty(filename))
filename = HttpContext.Current.Server.MapPath(config.StronglyTypedGlobalResource);
if (string.IsNullOrEmpty(nameSpace))
nameSpace = config.ResourceBaseNamespace;
// create all resources into this file and namespace
strongTypes.CreateClassFromAllDatabaseResources(nameSpace, filename);
if (!string.IsNullOrEmpty(strongTypes.ErrorMessage))
throw new ApplicationException(WebUtils.GRes(STR_RESOURCESET, "StronglyTypedGlobalResourcesFailed"));
return true;
}
As with the Resx Generator in Visual Studio, if you remove or rename a resource you may break your code. So you should frequently regenerate your strongly typed classes to see compilation errors as soon as possible.