One of the most important aspects of Object-Oriented Development is interfaces. By now, most developers would have touched on them from one time or another, but strangely enough, not many use them that often on a daily basis. So why is that? is it a lack of understanding how interfaces work? or is it simply because it does give you additional work, at least early on, that we don't bother with them?
It is a shame, because it's a very powerful way of allowing one object to be accessed by another. It also gives you the option (shouldn't really say option because i think it's almost a requirement to doing well-managed and segregated code) of seperating your
UI logic from your
Business Logic.
Here i'll show an example on how to utilise interfaces. mind you, for prosperity's sake it wont be a complete solution, but it should give you an idea on how to implement them.
This example, which is very simple, displays the content of an XML file, bound to a DataGrid - but it does show how to create a re-usable control.
I've create a solution with the following projects in it:
- Interfaces (simple aspx web site)
Books.xml
Default.aspx
- Controls (Class Library)
DataGridPanelControl.cs
IDataGridPanelControl.cs
First, we build our Control, which is a simple class that contains a Panel and a DataGrid
Code: DataGridPanelControl.cs
using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
namespace Controls
{
public class DataGridPanelControl : IDataGridPanelControl
{
#region Private member variables and objects
private Panel m_gridContainer;
private DataGrid m_dataGrid;
#endregion
public void Initialise(XmlDataSource xmlDataSource)
{
// instantiate all objects required
m_gridContainer = new Panel();
m_dataGrid = new DataGrid();
// create datagrid columns
this.CreateDataGridColumns();
// lets not auto-generate columns for the sake of argument
m_dataGrid.AutoGenerateColumns = false;
m_dataGrid.DataSource = xmlDataSource;
// add GridView control to the panels
// control collection.
m_gridContainer.Controls.Add( m_dataGrid );
}
public Control GridPanel
{
get { return m_gridContainer; }
}
public void BindDataGridView()
{
m_dataGrid.DataBind();
}
private void CreateDataGridColumns()
{
BoundColumn businessColumn = new BoundColumn();
businessColumn.DataField = "genre";
businessColumn.HeaderText = "Genre";
m_dataGrid.Columns.Add(businessColumn);
BoundColumn isbnColumn = new BoundColumn();
isbnColumn.DataField = "ISBN";
isbnColumn.HeaderText = "ISBN";
m_dataGrid.Columns.Add(isbnColumn);
BoundColumn titleColumn = new BoundColumn();
titleColumn.DataField = "Title";
titleColumn.HeaderText = "Title";
m_dataGrid.Columns.Add(titleColumn);
BoundColumn priceColumn = new BoundColumn();
priceColumn.DataField = "Price";
priceColumn.HeaderText = "Price";
m_dataGrid.Columns.Add(priceColumn);
}
}
}
Secondly, we need our interface.
Code: IDataGridPanelControl.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web.UI;
using System.Xml;
using System.Web.UI.WebControls;
namespace Controls
{
public interface IDataGridPanelControl
{
void Initialise(XmlDataSource xmlDataSource);
void BindDataGridView();
Control GridPanel { get;}
}
}
And to finish it all off, we just need to call the interface in our aspx.cs page. In this page you'll see a reference to a controlPlaceHolder which is a control i added in Design View - a Placeholder control. and the xmlBooks - a XmlDataSource control is also added in the Design View of the Default.aspx page.
Code: IDataGridPanelControl.cs
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Controls;
public partial class _Default : System.Web.UI.Page
{
#region Private member objects and variables
private IDataGridPanelControl m_dataGridPanelControl;
#endregion
protected void Page_Load(object sender, EventArgs e)
{
// initialise all objects
m_dataGridPanelControl = new DataGridPanelControl();
m_dataGridPanelControl.Initialise(this.xmlBooks);
controlPlaceHolder.Controls.Add(m_dataGridPanelControl.GridPanel);
m_dataGridPanelControl.BindDataGridView();
}
}
That's how simple it is. a re-usable (well, pretty much useless datagrid - but it gets the point across) control..
and the XML file (Books.xml) contains this.
<?xml version="1.0" encoding="utf-8" ?>
<Books>
<book genre="Business" ISBN="BU1032" Title="The Busy Executive's Database Guide" Price="19.99" />
<book genre="Business" ISBN="BU1031" Title="The Busy Executive's KPI Reports Guide" Price="19.99" />
<book genre="History" ISBN="HI001" Title="The Rise and Fall of the VW Beetle" Price="25.00" />
<book genre="SCI-FI" ISBN="SF3263" Title="Monsters From Below" Price="9.95" />
<book genre="Kids" ISBN="KD7567" Title="Sally The Apple Girl" Price="5.85" />
<book genre="IT" ISBN="IT0001" Title="How To Waste Time - And Get Paid For It" Price="7.95" />
</Books>

Posted
Feb 13 2007, 10:57 PM
by
Brian Madsen