This is a migrated thread and some comments may be shown as answers.

TypeConverterAttribute<T1, T2>

2 Answers 29 Views
Feature Requests
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Pål
Top achievements
Rank 1
Pål asked on 09 Nov 2011, 09:17 PM
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 {
 
    [TypeToStringConverter]
    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.

Thanks

Pål

2 Answers, 1 is accepted

Sort by
0
Ady
Telerik team
answered on 14 Nov 2011, 03:10 PM
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" />
    </extension>
 </field>

You can find an example here.

Do get back in case you need further assistance.

Greetings,
Ady
the Telerik team

NEW and UPDATED OpenAccess ORM Resources. Check them out!

0
Pål
Top achievements
Rank 1
answered on 14 Nov 2011, 03:53 PM
Hi.

That's great news.

Can't wait to try this out.

Thanks

Pål
Tags
Feature Requests
Asked by
Pål
Top achievements
Rank 1
Answers by
Ady
Telerik team
Pål
Top achievements
Rank 1
Share this question
or