How to generate partial class to override the properties before get and set takes place.
Please kindly explain.
1 Answer, 1 is accepted
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: