Knowledge is power. We love to share it.

News related to Mono products, services and latest developments in our community.


MonoX declarative parameter binding

04/06/2010Categories: MonoX
Declarative parameter binding is one of those small gems that will save you a lot of time that was usually spent of writing QueryString parameter retrieval code. We started for an idea that was described a long ago by a guy named Piers Williams on CodeProject. His article discusses a reflection/attribute based strategy for automatic population of instance fields and properties on an ASP.NET page from parameters supplied via properties of the Request object - GET-based Request.QueryString or POST-based Request.Form.
As a result, you are able to populate instance fields and properties using attrubutes which specify the QueryString or Form parameter to bind to. No need for repetitive chunks of code that parses the Request.QueryString["whatever"], checking for nulls, casting, error handling, etc. New MonoX syntax is clean and elegant:

public string PrivateName;

[QueryParameter("MyName", DefaultValue="default")]
public string MyName{
      get{ return (string)ViewState["MyName"]; }
      set{ ViewState["MyName"]=value; }

[QueryParameter("IntParam", DefaultValue="0")]
public int MyIntParameter;

Note that there we were striving for a minimalistic solution that could be used with existing portal projects. There are alternative approaches, and PageMethods by metaSapiens is probably known to most of our users. Although it provides additional benefits (like strongly-typed navigation, another useful technique that will be described here soon), it also adds some overhead. An additional file - PageList.xml - is automatically generated in a process that is not really compatible with the portal page navigation generation. It is available as a VS add-in only and introduces problems in the command-line build process that we use on our contionous integration server.
This content has not been rated yet.