This project has moved. For the latest updates, please go here.

 

AppCan/Views and ViewModels

AppCan works with views and view models like prism, except that it expects you to derive them from specific interfaces.  Prism best practice says create a specific interface for practically everything including your view and view model.  AppCan instead recomends using a standard interface IView or IViewModel.  While its not absolutely required without doing this you won't get many of the benefits. 

 

Using standard interfaces allows developers that what to create composites of your view or view model to do so more easily, they don't need to reference your specific interface for your class and instead could just create their own and just derive from the standard interfaces.  This can sometimes prevent the need for creating a shared library between app a modules to contain interfaces, and prevents tying to specific types in some cases.

 

Windows are typically derived from IViewContainer

Views (i.e. UserControls) are derived from IView

ViewModels are derived from IViewModel

ViewModel extensions are derived from IViewModelExtension

 

 

View Model Extensions

You may notice on the IViewModel interface and extensions property that has a dictionary that maps a string to an IViewModelExtension.  The benefit of this is for easier compositing.  If you wanted to use an existing window/view the way it was except perhaps by injecting another control, you could bind that control to an additional view model (IViewModelExtension) that you add, so that you can leave the existing view model in place.  This allows you to provide additive behaviour without having to completely rewrite a view model interface.

An example of a binding in a xaml file, this example is if you gave your view model extension the name "test":

Text="{Binding ViewModelExtensions[test].MyViewModelProperty}"

 

 

IView interface:

public interface IView
    {
        IContext Context { get; set; }
        IViewModel ViewModel { get; set; }
        Object DataContext { get; set; }

    }

 

 

IViewModel interface:

public interface IViewModel
    {
        IModelContainer ModelContainer { get; set; }
        IModel Model { get; set; }
        IView View { get; set; }
        IRegionManager LocalRegionManager { get; }      

        /// <summary>
        /// View model extensions.  This will support adding additional view models
        /// They can be accessed from a property like this Text="{Binding ViewModelExtensions[test].MyViewModelProperty}"
        /// </summary>
        Dictionary<string, IViewModelExtension> Extensions { get; set; }
        

    }
}

 

IViewContainer interface:

/// <summary>
    /// IViewContainer is used for Window classes to indicate that the window can be used as a shell
    /// </summary>
    public interface IViewContainer : IView
    {
        /// <summary>
        /// The View Model
        /// </summary>
        IViewModel ViewModel { get; set; }

        /// <summary>
        /// The context
        /// </summary>
        IContext Context { get; set; }

        //The data context (often points to the same thing as the ViewModel
        Object DataContext { get; set; }
    }

 

IViewModelExtension interface:

/// <summary>
    /// Must be used by any View Model extensions.  The name will be added to the dictionary so it should be unique.
    /// </summary>
    public interface IViewModelExtension
    {
        string ExtensionName { get; }
    }

Last edited Mar 17, 2013 at 4:32 AM by davesspacebar, version 2

Comments

No comments yet.