0

I have WPF application where I use some basic class. I also use Fody PropertyChanged and EF 6.1.3. I am trying to change property SaveNeeded whenever any of property is changed. So I write this class:

 public class Foo : INotifyPropertyChanged
{
    public Foo()
    {
        PropertyChanged += Foo_PropertyChanged;
    }

    public event PropertyChangedEventHandler PropertyChanged;

    public string GivenNames { get; set; }

    public int Id { get; set; }

    [NotMapped]
    public bool SaveNeeded { get; set; }

    private void Foo_PropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        SaveNeeded = true;
    }
}

When I create new Foo and then change some property it works like I think it would. But when I am trying to get Foo from database with EF, PropertyChanged event is fired. Any suggestions how to get Foo from database and not fire PropertyChanged?

Thank You!!

Pavol
  • 552
  • 8
  • 19
  • 1
    When you get object from database EF creates that object for you and then *set* it's properties. That's why event is fired. – 3615 Jun 16 '17 at 06:57
  • Yes, I figured it out.. but is there some workaround? – Pavol Jun 16 '17 at 07:13
  • Not one I'm aware of. INPC is usually used for ViewModels, but EF returns domain entities. You should change the approach. Instead of adding INPC to entities, you should create ViewModels that represent entities and add INPC there. And to convert entities to ViewModels you could use something like automapper. – 3615 Jun 16 '17 at 07:19
  • Automapper use same technique like EF .. creates object and then set it's properties – Pavol Jun 16 '17 at 07:56
  • Yeah, you are right. You can still create ViewModels passing params via constructor, without automapper, but it's quite tedious... – 3615 Jun 16 '17 at 08:07
  • As stated before, I think you should have viewmodel classes and your db model classes(which won't implement INPC). Other solution is to use your own or 'standard' implementation of INPC where you can modify behavior of your method which is firing INPC so when let's say some flag is set like 'isReadingDataFromDB' then you won't rise INPC but only assigned field behind this property. After loading all data You will probably have to fire some INPC on your own to update UI but in will be done only once. – Lukasz Cokot Jun 16 '17 at 08:14
  • if you want to use and auto code injector then your stuck with its behaviour fody allows you to decorate with DoNotNotifyAttribute if there is a property you want to never update but otherwise as @3615 says your not following best practice which is fine if that works for you but means you have to accept the side effects, i've trieds auto injectors and i find that the number of times they don't work is so large that they are more hassel than they are worth – MikeT Jun 16 '17 at 09:13

1 Answers1

0

Ok thanks for suggestions I did it like this:

  public class Foo : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    public string GivenNames { get; set; }

    public int Id { get; set; }
}

 public class FooViewModel : Foo
{

    public bool SaveNeeded { get; set; }

    public void NoticeChanges()
    {
        PropertyChanged += FooViewModel_PropertyChanged;
    }

    private void FooViewModel_PropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        SaveNeeded = true;
    }
}

So I take Foo from database, map properties with Automapper and then I call NoticeChanges.

Pavol
  • 552
  • 8
  • 19