Why Linq-SQL generated code implements INotifyPropertyChanging

A

Andrus

I noticed that sqlmetal generated code in northwind.designer.cs implements
two interfaces:

public partial class Customer : INotifyPropertyChanging,
INotifyPropertyChanged {

public string CustomerID {
set {
....
this.SendPropertyChanging();
this._CustomerID = value;
this.SendPropertyChanged("CustomerID");
....

INotifyPropertyChanged alone allows to track changes well at property level.

Why they implemented also INotifyPropertyChanging ? INotifyPropertyChanging
allows to track changes only at entity level and does not even provide
changed property name.
Looks like junk code.

Andrus.
 
F

Frans Bouma [C# MVP]

Andrus said:
I noticed that sqlmetal generated code in northwind.designer.cs
implements two interfaces:

public partial class Customer : INotifyPropertyChanging,
INotifyPropertyChanged {

public string CustomerID {
set {
...
this.SendPropertyChanging();
this._CustomerID = value;
this.SendPropertyChanged("CustomerID");
...

INotifyPropertyChanged alone allows to track changes well at property
level.

Why they implemented also INotifyPropertyChanging ?
INotifyPropertyChanging allows to track changes only at entity level
and does not even provide changed property name. Looks like junk
code.

Try binding a list of entities to a grid or control and you'll quickly
learn that INotifyPropertyChanged is required to make them function
properly together with databinding oriented code.

FB

--
------------------------------------------------------------------------
Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
LLBLGen Pro website: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
 
A

Andrus

Frans,
Try binding a list of entities to a grid or control and you'll quickly
learn that INotifyPropertyChanged is required to make them function
properly together with databinding oriented code.

Sure.
However my question was about *INotifyPropertyChanging*
but you replied about *INotifyPropertyChanged*

Andrus.
 
P

Peter Morris

I believe that when you do SQL->LINQ you are able to write code in a partial
class and therefore hook into the part where the property is set, so that
you may implement any required logic.


A quick google should provide some examples.


Pete
 
A

Andrus

I believe that when you do SQL->LINQ you are able to write code in a
partial class and therefore hook into the part where the property is set,
so that you may implement any required logic.

Pete,

Yes, this is possible.
Any idea why MS generated code contains INotifyPropertyChanging
implementation ?
It is not needed for any purpose.

Andrus.
 
J

Jon Skeet [C# MVP]

Andrus said:
Yes, this is possible.
Any idea why MS generated code contains INotifyPropertyChanging
implementation ?
It is not needed for any purpose.

From
https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?
FeedbackID=290470

<quote>
The sole purpose of SendPropertyChanging() was to provide a lightweight
and performant way to detect when an object is changed so that a copy
can be created for keeping original values for use in optimistic
concurrency. Hence, we deliberately did not create event args to ensure
better performance. This is quite different from the traditional
databinding use of INotifyPropertyChanged where the property name is
provided.
</quote>
 
A

Andrus

Jon,
<quote>
The sole purpose of SendPropertyChanging() was to provide a lightweight
and performant way to detect when an object is changed so that a copy
can be created for keeping original values for use in optimistic
concurrency. Hence, we deliberately did not create event args to ensure
better performance. This is quite different from the traditional
databinding use of INotifyPropertyChanged where the property name is
provided.
</quote>

MS Entity framework seems to generate better code by using

ReportPropertyChanging("SocialSecurityNumber")

sample:

[EdmScalarPropertyAttribute()]
public string SocialSecurityNumber {
get { return _socialSecurityNumber; }

set {
OnSocialSecurityNumberChanging(value);
ReportPropertyChanging("SocialSecurityNumber");
_socialSecurityNumber = value;
ReportPropertyChanged("SocialSecurityNumber");
OnSocialSecurityNumberChanged();
}
}

The ReportPropertyChanging and ReportPropertyChanged methods defer to an
Entity Framework ChangeTracker object that monitors current and original
property values.

So was Microsoft changed their mind in EF ?
Why they are not consistent, implement it differently for Entity framework
!?

Andrus.
 
J

Jon Skeet [C# MVP]

So was Microsoft changed their mind in EF ?
Why they are not consistent, implement it differently for Entity framework
!?

Different teams implemented LINQ to SQL and the EF, as far as I'm
aware.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top