1
Vote

Create an AssignTypeConverter

description

Just like done with filters will be nice to have an AssignTypeConverter to make POCOs use templates without need to edit them.

comments

kazimanzurrashid wrote Sep 13, 2010 at 3:51 PM

Could you please elaborate it a bit more.

luizbon wrote Sep 22, 2010 at 3:54 PM

When we use the HTML helper extensions EditorFor and DisplayFor it checks if the type can be converted from string. In this case if I have an custom type in my ViewModel or POCO I need to decorate it with a TypeConverter to override this and the scafolding works.
What I have done is to workarround this and don't decorate my POCOs that are used directly in my views is to create an bootstrapper that decorate my classes the same way that you do with filters already.
My sugestion is to integrate this to MvcExtensions just like the filters works and we will be able to use scafolding to our POCOs or ViewModels without the need to decorate them and use display and editor templates for them.
Below I put two files for this sample. I have two types that are parameters to my report system and my view simply needs to use DisplayFor and all my parameters are printed with corresponding templates.
Here is the code I made for this:

EntityConverter.cs

namespace Infrastructure.Web {
public class EntityConverter: TypeConverter {
    public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) {
        return sourceType == typeof(string) ? true : base.CanConvertFrom(context, sourceType);
    }

    public static void AssignTypeConverter<TType>() {
        TypeDescriptor.AddAttributes(typeof(TType), new TypeConverterAttribute(typeof(EntityConverter)));
    } 
}

}

ConfigureTypeConverter.cs

namespace RXAnalysis.Models.Parameters {
public class ConfigureTypeConverter: BootstrapperTask {
    public override TaskContinuation Execute() {
        EntityConverter.AssignTypeConverter<Data>();

        EntityConverter.AssignTypeConverter<TextBox>();

        return TaskContinuation.Continue;
    }
}
}