DataAnnotations - Replace the default by some other?

6 posts, 1 answers
  1. Francois
    Francois avatar
    52 posts
    Member since:
    Dec 2010

    Posted 22 Oct 2013 Link to this post

    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,
  2. Francois
    Francois avatar
    52 posts
    Member since:
    Dec 2010

    Posted 23 Oct 2013 Link to this post

    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

  3. DevCraft banner
  4. Kaloyan Nikolov
    Admin
    Kaloyan Nikolov avatar
    118 posts

    Posted 25 Oct 2013 Link to this post

    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!
  5. Francois
    Francois avatar
    52 posts
    Member since:
    Dec 2010

    Posted 28 Oct 2013 Link to this post

    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!
  6. Answer
    Kaloyan Nikolov
    Admin
    Kaloyan Nikolov avatar
    118 posts

    Posted 31 Oct 2013 Link to this post

    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!
  7. Francois
    Francois avatar
    52 posts
    Member since:
    Dec 2010

    Posted 31 Oct 2013 Link to this post

    Thanks, I don't know which way I'll end up using, but I like to have options!
Back to Top
DevCraft banner