I repeat this property in my MVVM
project too many times to count. What would be the correct way to create a generic class or factory to replace these lines of code?
ObservableCollection<Result> _resultCollection;
public ObservableCollection<Result> ResultCollection
{
get
{
if (_resultCollection == null)
_resultCollection = new ObservableCollection<Result>();
return _resultCollection;
}
set
{
_resultCollection = value;
}
}
AFAIK it's not possible without some form of code generation. Look into T4 templates, as answered here .
Yea... to be able to bind to a field would be great, wouldn't it... ;-) This brings me to the first solution:
(1) create a converter that allows you to bind a field.
and then (2) binding the field directly from XAML:
I kinda dislike this to be honest because it doesn't really clear things up... So as the second solution, you can instantiate the property directly from the constructor:
While this does increase your memory footprint a few bytes (it changes the semantics of lazy instantiation into direct instantiation), I wouldn't be too concerned about that.
I know this doesn't exactly answer your question, however personally I prefer to use a recorded macro in Visual Studio that writes all that for me.
I prefer using this to a generic class as it keeps all the relevant code in one place, and it's easy to understand what is going on. Typically I put all my private Fields at the top of my class, and hide all the public Properties in a
#region
tag that I keep collapsed so I don't need to scroll through them.It's fairly easy to create a macro in VS: Simply go to Tools > Macros > Record Temporary Macro, then perform the changes you want using the keyboard only. Once you have the Macro working correctly, simply save it as a permanent macro. If you do it right, you can re-run the macro with any variable and it will build it out the same way.
Some useful keyboard shortcuts to remember when creating macros are:
You may also need to do some minor modifications to the VB macro code, such as a
.Replace()
to turn lowercase letters to capitals.Here's an example Macro I use to build out all my public Properties for MVVM. It uses the PRISM library, so it uses the syntax
RaisePropertyChanged(() => this.SomeProperty);
With it, I can write my private definition, such as
Hit Ctrl+R, E to build the public property definition
Then run my Macro to build out the Property Change notification
(By default, Ctrl+R, E puts the cursor at the end of the private field definition, so that is where the cursor needs to be when running this macro)
I also have another old macro that adds code that checks if the value is null, and if so sets it to a new instance of the object, but its one I never used much since I prefer to set my default definitions in the Constructor instead of in the Property definition.
If you want it, it looks like this (note the Macro name - You need to highlight the public property name prior to running this macro):
And results in code looking like this: