tags:

views:

54

answers:

2

You can refer to this post.

I am getting these Extensibility methods from VS2008 in my Linq to sql Entity.

What are these auto generated extensibility methods good for?

#region Extensibility Method Definitions
    partial void OnCreated();
    partial void InsertPerson(Person instance);
    partial void UpdatePerson(Person instance);
    partial void DeletePerson(Person instance);
    #endregion

#region Extensibility Method Definitions
    partial void OnLoaded();
    partial void OnValidate(System.Data.Linq.ChangeAction action);
    partial void OnCreated();
    partial void OnIDChanging(int value);
    partial void OnIDChanged();
    partial void OnIDRoleChanging(System.Nullable<int> value);
    partial void OnIDRoleChanged();
    partial void OnLastNameChanging(string value);
    partial void OnLastNameChanged();
    partial void OnFirstNameChanging(string value);
    partial void OnFirstNameChanged();
    #endregion

And these events?

public event PropertyChangingEventHandler PropertyChanging;

public event PropertyChangedEventHandler PropertyChanged;
+4  A: 

These partial methods are extension points you can use - if you need and wish to do so:

partial void InsertPerson(Person instance);

This means, you can - if you wish - implement a function that gets called each time a Person object is being inserted in your data context.

A partial function will be removed by the linker if it's not been implemented in any way - so there's no performance penalty of any kind.

However, if you implement it, it will be called and can be used to tweak the way the system behaves.

partial void OnLastNameChanging(string value);
partial void OnLastNameChanged();

For each property on your entities, you'll get two partial methods that allow you to hook into a time just before your property gets changed (OnLastNameChanging), or just after the change has been made (OnLastNameChanged). You'd use the first method typically for validation - check the new validation and throw an exception if you don't like it for some reason.

You'd use the second method typically to do additional housekeeping or updating other properties, once your "lastname" has been changed.

Partial methods are a new C# 3.0 (.NET 3.5) feature - read more about them here and here.

Marc

marc_s
And what are the two events good for?
JMSA
What are the use of PropertyChanging; PropertyChanged; events????
JMSA
+1  A: 

They are there so you easily can add behaviour to the different stages of a linq-to-sql entity.

For example the OnValidate method is incredibly useful if you want to hook in custom validation for certain properties of a class. Say for example you do not want FirstNames that contains the letter A to be allowed. You could simply add a partial version of the OnValidate method that sets the object as invalid like so:

 partial void OnValidate(System.Data.Linq.ChangeAction action) {
    if(FirstName.Contains('a') {
       //Do some custom code that prevents saving to the database and notifies the user.
    }
 }

In addition to explain the two events. They are there so you can add custom logic whenever a property is changing or has changed. For example you might wanna log to database everytime a user changes his FirstName (for what reason I know not) then you could hook into the PropertyChanging event and add behaviour for that.

Something like this might explain it better:

 this.PropertyChanging += new PropertyChangingEventHandler(User_PropertyChanging);

and then a method to handle the event:

void User_PropertyChanging(object sender, PropertyChangingEventArgs e)
    {
        //Add some code to log the change to database...
    }
Robban
you could also do this kind of validation in the OnFirstNameChanging partial method.......
marc_s
You're absolutely correct about that... a bad example perhaps, the onvalidate should be better used for validation of all properties instead of simply one...
Robban
What are the use ofPropertyChanging;PropertyChanged;events????
JMSA
Added some code to the post for clarity.
Robban