Upgrading to Prism 5.0 – BindableBase

I recently upgraded my TPL Samples solution to the latest Prism libraries. I’ve removed the references to the “lib” directory and added Prism as a nuget package. The first thing you’ll notice is that NotificationObject has been deprecated and replaced with Microsoft.Practices.Prism.Mvvm.BindableBase. This makes things really nice as we no longer have such horrible boilerplate code.

What used to be

private string currentState;
public string CurrentState
{
	get { return this.currentState; }
	set
	{
		if (this.currentState != value)
		{
			this.currentState = value;
			this.RaisePropertyChanged(() => this.CurrentState);
		}
	}
}

Now looks like:

private string currentState;
public string CurrentState
{
	get { return this.currentState; }
	set { SetProperty(ref this.currentState, value); }
}

You can see how much cleaner this code is. The SetProperty method in BindableBase will take care of firing any RaisePropertyChanged events for you as well as take care of any needed validation. Check out Upgrading from Prism Library 4.1, which is Microsoft’s guide on upgrading.

Thanks,
Brian

 

Update 05/15/14:

Well, after using this I’ve discovered validation still has to be implemented manually.  I’ll do a future post regarding this.

Brian

Leave a Reply