Bv202 Bv202 - 1 month ago 13
C# Question

Adding INotifyPropertyChange in generated EF classes

So, my classes are auto-generated by EF:

//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated from a template.
//
// Manual changes to this file may cause unexpected behavior in your application.
// Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace Testje.Models
{
using System;
using System.Collections.Generic;

public partial class Ploeg
{
public Ploeg()
{
}

public int Id { get; set; }
public string Naam { get; set; }
public string Icon { get; set; }
}
}


When one of the properties change, I'd like to do a notifypropertychange. Is this possible somehow without editing this generated class?

Answer

It would be possible, but I strongly suggest to not do this! Adding INotifyPropertyChanged in your model will lead to a bad separation of concerns.

Use events to highlight that the model has changed. Have a look here to see how this works: MSDN on standard event pattern

Or even better: MSDN cites Albahari Bros on the event pattern

In your viewmodel, implement INotifyProperty changed. Let your ViewModel than listen to the events from your model, adapt the data and push it to your view through INotifyPropertyChanged