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

DataAnnotations - Replace the default by some other?

5 Answers 67 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Francois
Top achievements
Rank 1
Francois asked on 22 Oct 2013, 12:49 PM
Hi,

We're currently using the Microsoft Enterprise Library Validation for the validations of our entities and we've been trying the ORM for a while.

Currently, we're dumb because we are are creating a 'business' entity representing the 'model' entity (ORM), so let's say I have a 'TEST_CAR' table, we're creating a 'Car' entity and converting the 'TEST_CAR' to the 'Car' entity each time we want to work on it.

Now, I plan to change that but I don't want to recode everything. I saw that the model can generate de DataAnnotations attributes, which is great, but is there a way (with the templates), to tell the model to replace the 'Required' attribute by a 'NotNullValidator' (the same attribute but with the enterprise library)?

Thanks,

5 Answers, 1 is accepted

Sort by
0
Francois
Top achievements
Rank 1
answered on 23 Oct 2013, 07:00 PM
Is it possible to do this?

I was looking into this http://www.telerik.com/community/code-library/orm/general/data-annotations-code-generation-template.aspx but couldn't understand how to make it work. Is it a good path to follow?

Thanks,

--
Frank

0
Kaloyan Nikolov
Telerik team
answered on 25 Oct 2013, 12:25 PM
Hello Frank,

I can confirm you that the approach you have taken is a valid one. The article you refer targets our old code generation templates. I would suggest you to customize our new set of code generation templates. Here is an example showing the basics how to customize the templates. 

For your convenience I have prepared sample application with demo customization (see the attachment). The changes are in the file: "CustomTemplates/DefaultTemplateCS.tt", method: SetRequiredDataAnnotationAttribute. You can see there how you can set any custom attributes. The demo application removes the "Required" attribute which is generated out of the box (when the "Generate DataAnnotations" option from Model Settings/ Code generation is allowed) and generates the "NotNullValidator" attribute instead. With this approach you could generate any attribute you need.

I hope this helps. Please do not hesitate to get back to us with any further questions.

 

Regards,
Kaloyan Nikolov
Telerik
OpenAccess ORM Q3 2013 simplifies your model operations even further providing you with greater flexibility. Check out the list of new features shipped with our latest release!
0
Francois
Top achievements
Rank 1
answered on 28 Oct 2013, 04:13 PM
Hi, I've been able to make it work, thanks to you.


Now my second goal would be to make all the generated objets implement an interface, which is heavily used by our framework.

The interface is :

public interface IUniqueIdentifiable
{
    int? Id { get; set; }
}

The goal would be to generate a new property that would forward to the primary key property of the object.

So, for example, that the DatabaseLog class :

[Key()]
public virtual int DatabaseLogID
{
    get
    {
        return this._databaseLogID;
    }
    set
    {
        this._databaseLogID = value;
    }
}

would have a new property

public int? Id
{
    get
    {
        return this.DatabaseLogID;
    }
    set
    {
        this.DatabaseLogID = value;
    }
}


Is it possible to do that?

Thank you!
0
Accepted
Kaloyan Nikolov
Telerik team
answered on 31 Oct 2013, 11:50 AM
Hi Francois,

The most easy and reliable way to achieve that is to rename the primary key properties in your model so they map the name and the type expected by the interface. If you do this you will not need to generate wrapper property returning the real ID property. 

The approach with wrapper property you can find in the attached sample. Here are the most important changes in the T4 template are:
1.  "DefaultTemplateCS.tt" line 100: persists the knowledge which property corresponds to the primary key, we will need it later.
2. "Engine.ttinclude" line 70: makes all classes to implement the target interface
3. "ClassGenerator.ttinclude" line 56: generates the additional property in the classes. You will see that the property uses explicit interface implementation. I did it that way to prevent potential name collisions if you PK property is called Id already. Also by using explicit implementation of the interface those properties will not be visible when you use the entity but will be visible if you are accessing them trough the interfaces. 

NOTE: you will not be able to build the sample because of mismatch between the type of the property coming from the interface and the real PK property. If you have this case you should add the proper casts in the T4 template. 
 

Regards,
Kaloyan Nikolov
Telerik
OpenAccess ORM Q3 2013 simplifies your model operations even further providing you with greater flexibility. Check out the list of new features shipped with our latest release!
0
Francois
Top achievements
Rank 1
answered on 31 Oct 2013, 01:56 PM
Thanks, I don't know which way I'll end up using, but I like to have options!
Tags
General Discussions
Asked by
Francois
Top achievements
Rank 1
Answers by
Francois
Top achievements
Rank 1
Kaloyan Nikolov
Telerik team
Share this question
or