This article is really more on C# generics, however we will examine some of the principles and see how can we apply them on the Views hierarchy. Namely, we know that every time we declare a View (be it ViewModeControl or ViewModeUserControl) we define the host of the control as a generic type of that class. An example of the View declaration looks like this:
public class BlogsListView : ViewModeControl<BlogsView> |
However, there are cases when we don’t wish to hard code the host (or parent) of the View. Namely, as we will see in the articles on “Designing reusable Views” and “Host ambivalence”, sometimes it is opportune to develop Views which can be reused in different modules and in different places of the hierarchy. The key to this approach is not to hard code the host, but rather make it generic. Generic Content module, which is largely reused throughout Sitefinity, is a great example of such design. Let us take a look at the View declaration of one of the Generic Content module Views.
public class ContentItemEdit<THost> : ViewModeControl<THost> where THost : Control, IGenericContentHost |
public class NewsItemEdit : ContentItemEdit<NewsItemsView> |
/// <summary> |
/// Defines the members that each host in Generic Content module needs |
/// to implement |
/// </summary> |
public interface IGenericContentHost |
{ |
/// <summary> |
/// Content Manager used by the control |
/// </summary> |
ContentManager Manager { get; } |
/// <summary> |
/// Gets the mode key for the host control |
/// </summary> |
string ViewModeKey { get; } |
/// <summary> |
/// Gets parameter key for the host control |
/// </summary> |
string ParameterKey { get; } |
/// <summary> |
/// Gets the type of the view mode. |
/// </summary> |
/// <value>The type of the view mode.</value> |
Type ViewModeType { get; } |
} |
- ContentManager Manager
- string ViewModeKey
- string ParameterKey
- Type ViewModeType
This, finally, allows us to access these properties through the this.Host property from the ContentEditView, even though we don’t know what the host of the View will be. Therefore, we have created a host agnostic View, which is however, specialized for the Generic Content based modules.