using System.ComponentModel; using System.Runtime.CompilerServices; namespace YOUserbase { public class User : INotifyPropertyChanged { int id; string firstname; string lastname; DateTime birthDate; string details; Group workingGroup; bool isFullTime; bool isExternal; bool isRemote; int nbProjects; public int Id { get => id; set => ChangeValue(ref id, value); } public string Firstname { get => firstname; set => ChangeValue(ref firstname, value); } public string Lastname { get => lastname; set => ChangeValue(ref lastname, value); } public DateTime BirthDate { get => birthDate; set => ChangeValue(ref birthDate, value); } public string Details { get => details; set => ChangeValue(ref details, value); } public Group WorkingGroup { get => workingGroup; set => ChangeValue(ref workingGroup, value); } public bool IsFullTime { get => isFullTime; set => ChangeValue(ref isFullTime, value); } public bool IsExternal { get => isExternal; set => ChangeValue(ref isExternal, value); } public bool IsRemote { get => isRemote; set => ChangeValue(ref isRemote, value); } public int NbProjects { get => nbProjects; set => ChangeValue(ref nbProjects, value); } private static int index = 0; public User() { Id = ++index; } public User(int id) { Id = id; } // Change value so setter looks a bit nicer :) private void ChangeValue(ref T field, T value, [CallerMemberName] string propertyName = "") { if(!value.Equals(field)) { field = value; NotifyPropertyChanged(propertyName); } } // This method is called by the Set accessor of each property. // The CallerMemberName attribute that is applied to the optional propertyName // parameter causes the property name of the caller to be substituted as an argument. private void NotifyPropertyChanged([CallerMemberName] string propertyName = "") { Console.WriteLine(propertyName); if(PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } public event PropertyChangedEventHandler PropertyChanged; } }