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

generating partial class

1 Answer 58 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.
Sakar
Top achievements
Rank 1
Sakar asked on 05 Dec 2012, 12:11 PM
How to generate partial class to override the properties before get and set takes place.

Please kindly explain.

1 Answer, 1 is accepted

Sort by
0
Dimitar Tachev
Telerik team
answered on 10 Dec 2012, 11:23 AM
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.
Tags
General Discussions
Asked by
Sakar
Top achievements
Rank 1
Answers by
Dimitar Tachev
Telerik team
Share this question
or