Tuesday, August 27, 2013

Binding enum arrays with MVC models

The simplicity of model binding in the MVC framework makes it a powerful and pleasant framework to send models up and down to views. But a little short coming prevents the usage of enum arrays and raising maintainability in my opinion. MVC 4 cannot convert / materialize enum values to an array when used in a model. To put it correctly, there is no standard type converter for the job.

This issue manifested when I was working on a checkbox list of statuses that could be selected as a filter. This would then be posted as a comma separated parameter value to be set on the model object. Which is finally used in a LINQ query.

Monday, August 26, 2013

Convert all the (C#) types!

Well, almost all. So here is an incredible handy conversion class that I always carry around in all my projects. It has never failed me, never let me down... you get the idea. Although I cannot take full credit for it, thank Steven for the initial setup, I have done some minor tweaks in the past.

Usage is pretty straight forward. Call the (generic) method, put in a string in the correct format, converted and casted type comes out.

var value = Conversion.AsValue<int>("123"); 
//// Returns 123 as int

var value = Conversion.AsValue<int?>("123"); 
//// Returns 123 as nullable int

var value = Conversion.AsValue<int?>(""); 
//// Returns null (handy for webrequest parameters)

Monday, April 8, 2013

Improved compiletime supported validators

After a the initial post on the compile-time supporting webform validators, a few itches started to surface. Mainly the extendability and cleanliness. With some slight changes it was possible to categorize the validators (thus enforcing more of the Single Responsibility Principle).

Usage


The following code demonstrates the usage of this API. Note that there are breaking changes if you are replacing the previous version.
protected override void OnInit(EventArgs e)
{
    base.OnInit(e);

    this.UsernameField.Validation().RequiredField("username");
    this.PasswordField.Validation().RequiredField("password");
}
Read the original post for implementation details.

Thursday, July 21, 2011

Property based template parsing

The usage of string.Format is widely known and appreciated. It provides a simple and clean way to build parametrized strings. However, this method can become somewhat obscure when there is a large string (e.g. template) to be formatted. Sometimes lot of parameters are needed and all the parameter numbers make not much sense on a page sized string. In this case named parameters or template variable names would be desired.

Thursday, January 6, 2011

ASP.NET Compile Time Supported Validators

UPDATED VERSION

Click here for the latest blog.

With.NET 3.5 extension methods were introduced, these methods provide a great way built features into 'closed' objects or doing a way of IOC.

Either way, the following example shows the usages a 'simple' validation framework. It provides extensions to validate web controls with compile time support. Which is quiet nice if don't have to write any markup code anymore.

Tuesday, November 30, 2010

Serialize to XML made a little easier

COVERAGE: XML Serialization, C#.
While digging, breaking and smashing around some code (aka refactoring), I found some redundant code that serialized and deserialized objects to files at various places. With a little effort a centralized helper class was created.

Wednesday, November 10, 2010

Creating compile time supported URLs

COVERAGE: ASP.Net C#, Microsoft AntiXss, LINQ, CuttingEdge Conditions.

When it comes to ASP.Net and linking to other pages it's a very lose system. Which is strange when you think about it. We're working in assemblies here, pages are classes and classes can have 'relations' with other classes and be supported by the compiler. Yet there are a lot of NavigateUrl's being filled with strings from either code or markup. Brrr.