TypeConverterAttribute<T1, T2>

3 posts, 0 answers
  1. Pål
    Pål avatar
    53 posts
    Member since:
    Sep 2012

    Posted 09 Nov 2011 Link to this post

    This is something I constantly get reminded that I need.

    After the introduction of sql type datetime2 support, not so much, but I still need to hack around to achieve desired result.

    Basically, this is an attribute that can be applied to fields/properties to specify a different CLR Type than database type where no 1:1 mapping exist, but a simple (or complex for that matter) conversion can be performed.

    This is best explained with a simple example:
    [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
    public abstract class TypeConverterAttribute<T1, T2> : Attribute {
        public abstract T2 ConvertTo(T1 item);
        public abstract T1 ConvertFrom(T2 item);
    public class TypeToStringConverterAttribute : TypeConverterAttribute<Type, String> {
        public override String ConvertTo(Type item) {
            return item.FullName;
        public override Type ConvertFrom(String item) {
            return Type.GetType(item);
    public class MyPersistentClass {
        public Type Type { get; set; }

    The base class could be provided as part as OpenAcces and descendant Attribute classes implemented by developers to automatically convert values on load/persist/query operations.

    The big advantage here is the ability to use LINQ on the market properties as the actual type:
    var query = from m in new List<MyPersistentClass>().AsQueryable()
            where m.Type == typeof(Exception)
            select m;

    Please let me know if none of this make sense.


  2. Ady
    Ady avatar
    589 posts

    Posted 14 Nov 2011 Link to this post

    Hi Pål,

     There already exists similar functionality where a type converter can be specified for a particuar field/property. A 'Telerik.OpenAccess.Data.AdoTypeConverter' implementation is used for each column/field in order to do transfer the values to and fro. At the moment the UI to specify this is not available via the DSL surface but this can be done by using the following code in a static constructor of the context class-

    var fldsWithCnvtr = from pt in container.PersistentTypes
                                    from member in pt.Members
                                    where (/*condition to filter the member*/)
                                    select member;
    foreach (var item in fldsWithCnvtr)
        MetaPrimitiveMember pm = item as MetaPrimitiveMember;
        if (pm != null)
             pm.Column.Converter = /*Assembly qualified name of class that implements AdoTypeConverter*/;

    If you are using the 'classic' approach you can specify the type converter in the mapping in the app.config file as follows -
    <field name="somefield">
        <extension key="db-column">
           <extension key="db-converter" value="Assembly qualified type name of class that implements AdoTypeConverter" />

    You can find an example here.

    Do get back in case you need further assistance.

    the Telerik team

    NEW and UPDATED OpenAccess ORM Resources. Check them out!

  3. Pål
    Pål avatar
    53 posts
    Member since:
    Sep 2012

    Posted 14 Nov 2011 Link to this post


    That's great news.

    Can't wait to try this out.


Back to Top