0

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).

John Go-Soco
  • 886
  • 1
  • 9
  • 20
  • 1
    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 Jun 13 '14 at 13:00
  • 1
    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 Jun 13 '14 at 13:05
  • 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 Jun 13 '14 at 13:07
  • @DanielMann I wasn't going to go that far but +1 for NH – Liath Jun 13 '14 at 13:17
  • Take a look at this question - http://stackoverflow.com/questions/1117207/how-can-i-add-an-isdirty-property-to-a-linq-to-sql-entity – Liath Jun 13 '14 at 13:17
  • Thanks for the info, guys. I will definitely investigate using Entity Framework or nHibernate on my next application. I uses LINQ to SQL in this instance because I figured it would be quicker and easier to implement. – John Go-Soco Jun 13 '14 at 13:28

2 Answers2

0

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

brainless coder
  • 6,310
  • 1
  • 20
  • 36
  • The significance of the `SendPropertyChanging` etc is that the generated properties invoke these automatically; adding an extension method won't cause this to happen – Marc Gravell Jun 13 '14 at 13:06
0

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.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • Indeed, the On*Changed methods would be my last resort. I just considered it a bit inelegant add one line of code to each method call (for each database field), when there already was another method (SendPropertyChanged) that is also already called. – John Go-Soco Jun 13 '14 at 13:40