Provider model everywhere

Today I developed a small and simple web control (on1.1) that represents a navigation bar. The goal was realize this navigation bar with Microsoft CRM “look and feel” as it would to be inserted on a CRM integration project.

I started with 3 classes: NavigationBar, NavigationBarItem and NavigationBarItemCollection. NavigationBar and NavigationBarItem inherit from WebControl. I wrote all common properties and methods you usually have to implement (or to override) to manage the ViewState, the DataBinding process, ect. I think I wrote some nice code on IEnumerable, IListSource, IList and ITypedList interfaces to accept as datasource as many types as possible. But this is not what I want to talk about, may be I will do it in another post.

After that I reached the rendering phase and I realized: “I don’t want a control with a CRM look and feel. I want a control that is flexible enought for rendering its content in many ways, and sometimes in the CRM look and feel way.” And I proceeded this way:

1)I defined a new abstract class named NavigationBarRenderingProviderBase that requires an instance of NavigationBar (the control to be rendered) to be passed in the constructor, exposes this instance in a protected readonly property and defines 3 abstract methods. Here this class code:

public abstract class NavigationBarRenderingProviderBase { private NavigationBar navigationBar; protected NavigationBarRenderingProviderBase(NavigationBar navigationBar) { this.navigationBar = navigationBar; } protected NavigationBar NavigationBar { get { return navigationBar; } } public abstract void RenderNavigationBarStart(HtmlTextWriter writer); public abstract void RenderNavigationBarEnd(HtmlTextWriter writer); public abstract void RenderNavigationBarItem(HtmlTextWriter writer, NavigationBarItem navigationBarItem); }

As their names say, the 3 abstract methods will handle the begin of the rendering process, the rendering of every item in the NavigationBar and the end of the process.

2) I developed a basic specialization of this class, named NavigationBarRenderingProvider. And I wrote the code needed for the standard rendering of my control.

3) I developed a further specialization of this class, named CrmNavigationBarRenderingProvider. And I wrote the code needed for rendering my control in the CRM look and feel.

4) I defined a property in NavigationBar throught which is possible to define the rendering provider to use. This property returns the standard rendering provider (NavigationBarRenderingProvider ) if no one has been set.

public NavigationBarRenderingProviderBase RenderingProvider { get { if (m_RenderingProvider == null) m_RenderingProvider = new NavigationBarRenderingProvider(this); return m_RenderingProvider; } set { m_RenderingProvider = value; } }

5) I overrode the render method to obtain the delegation of the rendering process to the rendering provider.

protected override void Render(HtmlTextWriter writer) { this.RenderingProvider.RenderNavigationBarStart(writer); foreach (NavigationBarItem item in this.Items) { this.RenderingProvider.RenderNavigationBarItem(writer, item); } this.RenderingProvider.RenderNavigationBarEnd(writer); }

Done. I have a WebControl that: – incapsulates all the logic needed to support DataBinding and to manage then ViewState. – exposes the rendering algorithm(s) trought a standard interface to allow different behaviours.I’m now thinking about making possible to configure the default rendering provider in web.config file. This will allow to easily change the rendering behaviour of my control in the entire web application. But I’m still not sure…

But, what about this implementation? Can I think to it as a to provider model implementation even if isn’t strictly related to data management? I think so, and you?


~ by Matteo on October 22, 2005.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: