-1

I'm building an app using Wpf Mvvm Entity Framework database-first. My model is auto generated so I can't put the NotifyPropertyChanged in the entity.. so I rewrite all the stuff in my viewModel witch is ugly..

I don't think I'm clear so a sample of code, won't post everything for more clarity.

The user control

<TextBox Name="tbClientNumber" Text="{Binding ClientNumber,
         UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"/> 

The ViewModel class:

public class ClientManagerViewModel : BaseViewModel
{
    public int ClientNumber
    {
        get { return _clientNumber; }
        set
        {
            _clientNumber= value;
            RaisePropertyChanged("ClientNumber");
        }
    }
}

Auto-generated entity:

public partial class Client
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public Client()
    {
    }

    public int idClient { get; set; }
    public int ClientNumber{ get; set; }
 }

Thank you all in advance

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
miniboom
  • 1
  • 1
  • 1
  • Hi there, what's the issue you want to avoid? Ugly is not a good description. Please write what exactly you want to achieve – Jan Nov 10 '18 at 04:44
  • You appear to want your Model to be a ViewModel. (I'm assuming you understand what the letters in [MVVM](https://en.m.wikipedia.org/wiki/Model–view–viewmodel) represent.) This may mean you haven't grasped the reason for them being separate. – Richardissimo Nov 10 '18 at 07:46
  • I thought viewmodel doesn't know anything about the view, so if i put all the property in viewModel, properties that are already existing somewhere, it sounds to me that's incorrect for the design pattern. I describe it as ugly for that reason.. – miniboom Nov 10 '18 at 19:13
  • You can put the entities and wrappers on model layer. View model will have a property `ClientWrapper` and the binding will be like `Text="{Binding ClientWrapper.ClientNumber, ... }"`. This might help you https://stackoverflow.com/questions/17143842/mvvm-viewmodel-lots-of-properties – cactuaroid Nov 11 '18 at 07:22

1 Answers1

0

If you don't want to wrap the entity classes, Fody might help you. You can implement INotifyPropertyChanged on a partial class definition. I have not tried yet though.

All classes that implement INotifyPropertyChanged will have notification code injected into property setters.

https://github.com/Fody/PropertyChanged

Actually I usually simply wrap them like you did.

cactuaroid
  • 496
  • 1
  • 4
  • 19