generating partial class

2 posts, 0 answers
  1. Sakar
    Sakar avatar
    3 posts
    Member since:
    Nov 2012

    Posted 05 Dec 2012 Link to this post

    How to generate partial class to override the properties before get and set takes place.

    Please kindly explain.

  2. Dimitar Tachev
    Admin
    Dimitar Tachev avatar
    67 posts

    Posted 10 Dec 2012 Link to this post

    Hello Sakar,

    In general you cannot override methods or properties from a partial class because actually that is still the same class. 

    In order to insert some code before get and set methods in your properties I suggest you to modify the code generation template that is used by OpenAccess ORM to generate the entities. 

    According to your needs I suggest you to choose one of the following options:

    1. If you need to do the same operations in all of the properties – modify your code generation template to add two partial methods and call them before each get and set method like below:

    partial void BeforeGet();
    partial void BeforeSet(object newValue); // optionally add argument for the old value
    ...
    public virtual object Something
    {
       get
       {
           this.BeforeGet();
           return this._ something;
       }
       set
       {
           this.BeforeSet(value);
           this._something = value;
       }
    }
    ...


    2. If you need to execute different operations for the properties – modify your code generation template to generate partial methods for each of the properties like below:
    partial void BeforeIdGet();
    partial void BeforeIdSet(int newValue); // optionally add argument for the old value
     
    private int _id;
    public virtual int Id
    {
       get
       {
           this.BeforeIdGet();
           return this._id;
       }
       set
       {
           this.BeforeIdSet(value);
           this._id = value;
       }
    }
    ... // the same for each property that you need

     After that add implementation for the partial method in your partial class:
    public partial class YourClass
        {
            partial void BeforeIdGet()
            {
                ... // before getting the property
            }
     
            partial void BeforeIdSet(int newValue)
            {
                ... // before setting the property
            }
            ... // the same for each property that you need
        }

     Below you could find useful information about our code generation templates and how to use them.
    Telerik TV: Customizing Code Generation Templates in OpenAccess ORM
    Best Practices for Customizing the Code Generation Templates
    How to: Implement INotifyPropertyChanging/ed Interface (C#)
    How to: Implement INotifyPropertyChanging/ed Interface (VB.NET)

    I hope this helps. I will be happy to assist you if you have further questions.

    Regards,
    Dimitar Tachev
    the Telerik team
    Telerik OpenAccess ORM Meets ASP.NET Web API. Read more.
  3. DevCraft banner
Back to Top