0
votes

I'm currently working on a pretty basic application that uses Linq-to-SQL classes. I've been adding new methods and properties to the auto-generated dataclasses by leveraging the fact that they are "partial." However, now I would like to actually modify the code contained in one the class methods - namely, SendPropertyChanged().

The simplest way I can see to do this is by modifying the code in the auto-generated .designer.cs file itself, but I can see how this is quite dangerous (because the code may be overwritten by the code generator). Is there an alternative way for me to safely modify the code?

EDIT: Why I wish to do this: I want to add a property "isDirty" that is set whenever any of the fields are changed. Since whenever a field is changed, it calls the SendPropertyChanged method, I figured I would just stick "isDirty = true" in there (with appropriate checks).

2
This sounds like a bad idea, each time you update your dbml you'll lose your changes. Why do you want to replace it? Can't you use one of the partial methods? - Liath
I want to add an isDirty property to the class, which is set whenever any of the fields are updated. I [i]could[/i] use the onFieldChanged() events (and in failing to find a suitable alternative method, I probably will) and add those to the partial class, but it seems too inelegant. - John Go-Soco
It's also a bad idea to use LINQ to SQL. If you have the option, I'd recommend switching to a more mature, supported ORM like Entity Framework or nHibernate. L2S isn't being developed anymore... it's dead. - Daniel Mann
@DanielMann I wasn't going to go that far but +1 for NH - Liath
Take a look at this question - stackoverflow.com/questions/1117207/… - Liath

2 Answers

0
votes

It is not recommended to change auto-generated files, because of the obvious reason is that they will be replaced each time something is changed and the IDE generates it.

If you really want to modify them, and since they are partial classes you can redefine them as partials or better, if you could subclass those.

There is one other option that you can explore, is using Extension Methods, this is a very easy way to add functionalities to classes without modifying them

0
votes

The idea here is that you add your code to the On*Changing and On*Changed methods, for example:

partial void OnNameChanged() {
    // my code here
}

You could also subscribe to PropertyChanging / PropertyChanged, but that is generally bad practice (subscribing to your own events), and will have performance overheads.