MVVM involves a lot of writing boilerplate code, as mentioned in multiple posts. A lot of shortcuts seem to provide runtime evaluation, but I was wondering is there a free utility which uses reflection to generate the c# code ie:
Class A
{
int I {get;set;}
ICollection C {get; set;}
}
will give me a file or clipboard containing something like
Class ViewModelA : INotifyPropertyChanged
{
readonly A _a;
private ObservableCollection _c;
ViewModelA(ClassA a)
{
_a=a;
_c=new ObservableCollection(a.C);
}
int I
{
get {return _a.I;}
set
{
(if _a.I != value)
{
_a.I = value;
NotifyPropertyChanged("I");
}
}
}
}
Thanks
PropertyChanged.Fody is what I use to deal with this.
It already recognizes base classes from most of the MVVM frameworks out there and calls the appropriate method for doing notification. Otherwise it’ll find any class that implements INPC and convert it for you.
The reason why I prefer Fody over PostSharp is that Fody adds the IL into your assembly, and then cleans itself up, so you have no dependency on Fody or any other assemblies, and your code is self contained. PostSharp just adds interceptors, which then call out to the PostSharp library, then back into any interceptor you’ve added. This is slower at runtime, and you need a dependency on the PostSharp library.
Plus Fody is free and open source.