I am working on a WPF app, leveraging Prism and Unity.
I have a scenario where I have lots of similar views/viewmodel. Not exactly alike, but 90%. I have 22 dimension-type datatypes. Instead of building out 88 individual MVVM scenarios, I am doing a cut-n-paste for the 22 instance of each specific activity (CRUD/Navigation). I needed to figure out how to define a generic base viewmodel that enforced inheritance for the generic type.
BTW: This is a throw-away app with an expected <3yr lifespan. It’s not worth the time/effort in building up something truly dynamic. I need to slam it out and pass it off to off-shore support. If it’s too complicated, I won’t be able to hand it off. This falls under, ‘Just get it done,’
All of the samples I could find were ‘ClassName<T,U> where T : class where U class’ examples. That doesn’t work for what I need to do. Below is how I solved the class definition issue.
public class DimensionBase : BindableBase, IChangeTracking
{
...
}
public abstract class CollectionViewModelBase<T> : BindableBase, INavigationAware, IActiveAware where T : DimensionBase
{
...
}
public abstract class DetailViewModelBase<T> : BindableBase, INavigationAware where T : DimensionBase
{
...
}
public abstract class EditViewModelBase<T> : BindableBase, INavigationAware, IConfirmNavigationRequest where T : DimensionBase
{
...
}
What I learned is that you can tack on the where clause to the end of the class definition to enforce constraints on the ‘T’ after all of the constraints are applied to the base class itself. I had been trying to manage the ‘T’ constraints first which is following my understanding of the ‘standard’ examples of where T … where U …
The delete action is managed by the collection view model, by way of the confirmation dialog hosted in the shell. I use events to manage that process.
There one more thing I want to clarify. I am using the region manager to handle all view-based navigation. I am using the event aggregator for passing messages around the system. The biggest one is telling all of the views to refresh their data when the target environment has changed. Getting navigation/confirmation dialog to fire, leverages events because the views hosted by separate modules (24 so far) that cannot directly access the notification/clarification dialogs hosted in the shell. Nor does Prism enable hosting these dialogs in a region (as far as I can tell). So there is only one way to do it. Events.
Enjoy.