Project Description

RequestModel is a simple implementation of typed access to a ASP.NET web forms request parameters (Form, Query String, etc).

The user is required to create an interface of the properties he expects, decorated with FromRequest attributes  and the RequestModel will give him an implementation of this interface filled with data from the current Request.

A page can have many different Models.

The system references ImpromptuInterface project in order to map a generated ExpandoObject to the requested interface.

The properties can be value types, nullables, strings or arrays of value types, nullables and strings.

Array types are seperated by default by “-“ but this can be changed from ArrayElementSeperator option.

If any of the request parameters declared in the interface are not valid, the Factory will return a null object.

That is:

  • a value types and any property defines as required must always have a value.
  • a nullable value type or a string without Required defined, can be null (missing or empty).

 

FromRequest attribute supports the following options:

  • RequestKey: The Request key to map the property. If omitted, the property name will be used.
  • Required: For strings and nullables, althought missing value can be declared as null, this prevents the property of having null.
  • ArrayElementRequired: Same as Required but for each element of the array.
  • ArrayElementSeperator: Seperator used to parse distinct items of array.

 

Example

    public partial class About : System.Web.UI.Page
    {
        public interface IAboutData
        {
            [FromRequest("first")]
            string FirstName {get;}

            [FromRequest(Required=true)]
            string LastName { get; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            var data = Factory.RequestModel<IAboutData>();
            if (data != null) {
                var fName = data.FirstName;
            }
        }
    }

You can follow me on twitter: https://twitter.com/GMavritsakis

View George Mavritsakis's profile on LinkedIn

Last edited Oct 19, 2012 at 6:59 PM by gmavritsakis, version 2