Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

override a method using a partial class

I have a class formed by two partial classes.

One created by ORM code generation and one for extensions.

In this particular instance, I need to override one of the properties generated by the partial class because I need to do some validation on it first.

Is it possible to use my extension class to kind of override the property of the code generation partial class?

like image 484
Diskdrive Avatar asked Oct 01 '10 02:10

Diskdrive


1 Answers

No, not possible. If you are the owner of the code-generation, you should put in hooks for handling that scenario. For example, sqlmetal.exe for LinqToSql produces partial classes wherein each property setter looks a bit like this:

if (this.myProperty != value) 
{
    this.OnMyPropertyChanging(value);
    this.SendPropertyChanging();
    this.myProperty = value;
    this.SendPropertyChanged("MyProperty");
    this.OnMyPropertyChanged();
}

Of course, the generator also creates those property-specific changing/change methods, but they declare those as partials:

partial void OnMyPropertyChanging(string newValue);
partial void OnMyPropertyChanged();

With this setup, it's obviously quite easy to tap into these events for the extension partial class.

like image 113
Kirk Woll Avatar answered Sep 20 '22 00:09

Kirk Woll