Useful Expression Builders

Expression builders are a cool subject... they work with no-compile pages and provide a lot of declarative power!

How many times have you had the need to display values that are mostly equal, but slightly different than resources included in you .resx file?

For example, supose you have resource entry with key "User" which maps to value "User", and you also want to be able to display "Users". Normally, you would have to add another entry to the .resx file for the "Users" value. Enter the ConcatExpressionBuilder! See this example:

<%$ Concat:SomeGlobalResourceFile.ResourceKey,'SomeText',SomeOtherGlobalResourceFile.SomeOtherKey,'SomeOtherText',SomeLocalResourceKey %>

As you can guess, it mimics the behavior of the String.Concat method, but it can accept both literal strings (included in '') as well as global and local resource keys.

But what if you wanted to format a long value from a resource key? Enter the FormatExpressionBuilder:

<%$ Format:'Number is {0:2}',SomeGlobalResourceFile.LongResourceKey %>

The first argument before the first ',' is the format expression, and the others are arguments to it. The format expression can also come from a resource key:

<%$ Format:SomeLocalResourceKey,SomeGlobalResourceFile.SomeResourceKey,'SomeText' %>

Basically, it implements the String.Format method.

What about execute some code without code regions? Here is the CodeExpressionBuilder:

<%$ Code:DateTime.Now.Date %>

Here is my full list of general-purpose expression builders:

ApplicationExpressionBuilder Returns a value stored in the application
CodeExpressionBuilder Executes code
ConcatExpressionBuilder Concatenates a number of expressions, which may come from resource files
CookieExpressionBuilder Returns a value stored in a cookie
FormatExpressionBuilder Formats an expression with any number of arguments, which may come from resource files
InRoleExpressionBuilder Checks if the current user belongs to a role
ProfileExpressionBuilder Returns a profile value
QueryStringExpressionBuilder Returns a value from the query string
RequestExpressionBuilder Returns a value from the request
SessionExpressionBuilder Returns a value stored in the session

In the attached file, you can see all of these expression builders, together with their editors. Enjoy, and let me know if you like them or if you find any problems!



Comments have been disabled for this content.