The table object refers to a class which implements properties and methods which relate to a specific database table. The table object must inherit from the BaseTableObject class. The BaseTableObject implements various methods, some of which allow the table object to be serialized to cache and deserialized from cache. This enables us to cache the current “active” record to memory, thus minimising unnecessary calls to the database.
A form object refers to a class which contains an instance of the table object. The form object must inherit from Btomic.Forms.BaseForm
Any web pages we want to create which pertain to our database object will inherit from the FormObject, which will enable us to access the current “active” record from the cache, or enable us to update the “active” record.
The following example will demonstrate how we create a Table and Form object for the Location database table. Figure 1 is a graphical representation of the Location table.
Figure 1
Step 1 – Creating the table object
Our first step is to create a serializable class which inherits from the BaseTableObject class. The BaseTableObject class implements the ITableObject interface. The ITableObject interface ensures that we implement methods such as Render and GetTitle, which will be discussed later.
[Serializable()]
public class LocationTable : BaseTableObject
{
Step 2 – Declaring the public properties
Next we need to define the public properties of the table object. The Key value is defined within our BaseTableObject, and this key refers to our primary key identifier value which in this case is the LocationID. The properties do not necessarily match the columns of the database table. In our example we have created a PLocationName property, which refers to the name of the parent location. The design of the Location table allow us to store the Location data in a hierarchical manner, and we are able to determine the name of the parent by joining the location table to itself by means of the PLocationID .
public int LocationID
{
get { return Convert.ToInt32(Key); }
}
public string LocationName { get; internal set; }
public bool Active { get; internal set; }
public int PLocationID { get; internal set; }
public string PLocationName { get; internal set; }
Step 3 – Assigning values to the properties
We do the binding of the data to our properties within our GetPropertyValues() method. We have used LINQ to retrieve the data from the relevant database tables, and then we assign these values to the respective properties
protected override void GetPropertyValues()
{
BtomicDB db = new BtomicDB();
var q = (from r in db.BTLocation
join p in db.BTLocation on r.PLocationID equals p.LocationID
where r.LocationID == LocationID
select new
{
r.LocationName,
r.PLocationID,
PLocationName = p.LocationName,
r.Active
}).Single();
this.LocationName = q.LocationName;
this.Active = q.Active;
this.PLocationID = q.PLocationID;
this.PLocationName = q.PLocationName;
}
We do the binding of the data to our properties within our GetPropertyValues() method. We have used LINQ to retrieve the data from the relevant database tables, and then we assign these values to the appropriate properties.
Step 4 – Creating the Render and GetTitle methods
Our final step in creating the TableObject, is to implement the Render and GetTitle methods.
public override string GetTitle()
{
return "Location Details";
}
public override void Render(System.Web.UI.HtmlTextWriter writer, System.Web.UI.Page page)
{
RenderHtmlItem(writer, page, "Location Name", LocationName);
RenderHtmlLink(writer, page, "Parent Location Name", PLocationName, string.Format("~/ui/configuration/location/details.aspx?LocationID={0}", PLocationID));
}
The Render method enables us to render data pertaining to the table object. The available HTML rendering methods are:
- RenderHtmlItem
- Renders the title and value on the web page
- RenderHtmlLink
- Renders the title and value on the web page. The value field is rendered as a hyperlink.
- RenderHtmlMail
- The value field implements the MailTo Protocol, which Opens a client’s e-mail system and begins a new email message.
In our example we have rendered the location name as a Html item, and we have rendered the Parent Location Name as an HtmlLink. Figure 2 displays the result of our GetTitle and Render methods.
Figure 2
Step 5 – Creating the form object
We now need to create our form object. The new class must inherit from Btomic.Forms.BaseForm
public class LocationForm : Btomic.Forms.BaseForm
{
Step 6 – Creating an instance of the table object
Next we create an public instance of the LocationTable object. By doing this we can allow any web pages ,which inherit from the LocationForm object, access to the instance of the LocationTable object.
public LocationTable ActiveLocation;
Step 7 – Overriding the OnPreInit event
We need to override the OnPreInit event in order to properly initialise the LocationTable object prior to the page load event. The Instance method deserializes the ActiveLocation from cache and updates the key value with the value we pass through, in this case the Request.QueryString["LocationID"] .
protected override void OnPreInit(EventArgs e)
{
base.OnPreInit(e);
ActiveLocation = LocationTable.Instance(ref ActiveLocation, Request.QueryString["LocationID"], this);
TableObjectItems.Add(ActiveLocation);
}
Step 8 – Overriding the AquireOptions method
Finally we need to override the AcquireOptions method. AquireOptions method allows us to specify the menu items within the sidebar. Figure 3 displays the sidebar for the Location form. The MenuOptionType enumeration provides us with various options for the type of menu. The type of the menu has an impact on the display and behaviour of the menu option.
protected override void AquireOptions(ref Btomic.Classes.MenuOptions MenuOptions)
{
MenuOptions.Add(MenuOptionType.Search, "Find Location", "~/ui/configuration/location/search.aspx");
MenuOptions.Add(MenuOptionType.Create, "Add Child Location", string.Format("~/ui/configuration/location/create.aspx?LocationID={0}", ActiveLocation.LocationID));
MenuOptions.Add(MenuOptionType.Overview, "View Location", string.Format("~/ui/configuration/location/details.aspx?LocationID={0}", ActiveLocation.LocationID));
}
Figure 3
Step 9 – Using the table and form object
We can now implement pages relating to our Location object. Our Location pages need to inherit from the LoctionForm.
public partial class LocationModify : Btomic.Forms.LocationForm
{
We can easily access the public properties in the location table object by using the ActiveLocation variable:
private void GetLocationDetails()
{
txtLocationName.Text = ActiveLocation.LocationName;
ddlActive.SelectedValue = ActiveLocation.Active.ToString();
}