The content you're reading is getting on in years
This post is on the older side and its content may be out of date.
Be sure to visit our blogs homepage for our latest news, updates and information.
Throughout the documentation of the new Sitefinity 3.6 backend architecture, many times it has been said that Views should be self contained units of functionality that could function in any given place in the hierarchy. While this claim sounds reasonable, once we start developing real world modules, one may find it not so simple to achieve. Yet, by following some simple rules, we can achieve this host ambivalence and design Views that can function anywhere in the hierarchy of Views.
public class ContactsItemsView : ViewModeControl<ContactsControlPanel> |
public class ContactsItemsView<THost> : ViewModeControl<THost> where THost : Control |
Now, that we have defined the View this way, we will not be able to add the View as we used to:
AddView<ContactsItemsView>(null, "AllContacts", "CommandPanel_ContactsItems_Desc", "all", Messages.ResourceManager); |
AddView<ContactsItemsView<ContactsControlPanel>>(null, "AllContacts", "CommandPanel_ContactsItems_Desc", "all", Messages.ResourceManager); |
AddView<ContactsItemsView<CrmContactsView>>(null, "AllContacts", "CommandPanel_ContactsItems_Desc", "all", Messages.ResourceManager); |
Seemingly, we have to choose between two evils: having crude Views which can only work in a predefined place of the hierarchy or having no access to the members of the hierarchy. Fortunately, we can use the generic constraints to overcome these obstacles and get the best of the both world.
Let us suppose that we want to have a View which can be placed anywhere in the hierarchy, but at the same time we wish that whichever View hosts our View, must provide us with the access to the instance of ContactsManager class, so that we can always call this.Host.ContactsManager.
Instead of hardcoding the host, we can do following:
Create an interface and call it IContactsView - an interface to be implemented by all contact views. The implementation would look like this:
namespace Sample.Contacts.WebControls.Admin |
{ |
public interface IContactsView |
{ |
ContactsManager ContactsManager { get; } |
} |
} |
public class ContactsItemsView<THost> : ViewModeControl<THost> where THost : Control, IContactsView |
View all posts from The Progress Team on the Progress blog. Connect with us about all things application development and deployment, data integration and digital business.
Let our experts teach you how to use Sitefinity's best-in-class features to deliver compelling digital experiences.
Learn MoreSubscribe to get all the news, info and tutorials you need to build better business apps and sites
Progress collects the Personal Information set out in our Privacy Policy and the Supplemental Privacy notice for residents of California and other US States and uses it for the purposes stated in that policy.
You can also ask us not to share your Personal Information to third parties here: Do Not Sell or Share My Info
We see that you have already chosen to receive marketing materials from us. If you wish to change this at any time you may do so by clicking here.
Thank you for your continued interest in Progress. Based on either your previous activity on our websites or our ongoing relationship, we will keep you updated on our products, solutions, services, company news and events. If you decide that you want to be removed from our mailing lists at any time, you can change your contact preferences by clicking here.