• No results found

Design of the Wrox CMS

In document Instant Results pdf (Page 159-162)

Most of the pages in the Wrox CMS rely on SqlDataSourcecontrols to get the data in and out of the database. These new data source controls, in combination with the GridViewand FormView, allow you to create database-driven pages in no time, with very little to no code at all. However, these controls suffer from a few problems. First of all, they are best suited for rather simple scenarios. For example, the page in the Management section that allows you to create or change categories is well suited for the SqlDataSourcein combination with a GridView(for the list of categories) and a FormView(to insert new items) because the underlying data structure is quite simple. However, with more complex pages, like the AddEditContent.aspx page that has two drop-downs that are bound to each other, things become a bit more difficult. To make the SqlDataSourceand FormViewcontrols work in these scenarios you

have to jump through all kinds of hoops, resulting in bloated markup and far from straightforward code in the code-behind of your pages.

The second problem with the SqlDataSourcecontrols is the fact that they usually embed SQL state- ments directly in the markup of your pages. This breaks about every rule of good multi-tiered design because this forces you to update possibly many pages in your site whenever you make even a little change to the structure of your database.

Despite these disadvantages, using the SqlDataSourcecontrol can be a great way to rapidly develop relatively small web sites that require little to no changes in the database structure. To show you how they work and how to use them, they are used for most of the data access in the Wrox CMS. The only exception is the AddEditContent.aspx page. Instead of working with a SqlDataSourcecontrol, the Wrox CMS uses a few custom classes and methods to get information from and in the database. To minimize the impact of the SQL statements all over the page, stored procedures are used in all of the SqlDataSourcecontrols in the Wrox CMS. Instead of storing the entire INSERTor UPDATEstatement in the ASPX portion of the page, you now only store the name of a procedure in the database. Whenever a change is made to either the database structure or the queries, all that needs to be updated are a few stored procedures.

In later chapters — including Chapter 9 and Chapter 12 — you use ObjectDataSourcecontrols to enforce a three-tiered architecture.

The decision to use the SqlDataSourcecontrols in your pages results in very slim business and data access layers. The next section discusses the only class in the business layer. The section following that describes the database and the classes in the data access layer.

The Business Layer

As stated earlier, no SqlDataSourcecontrols are used to create and update content items in the Content table. The two drop-downs with the content types and categories that are related to each other result in bloated code that is very hard to understand and maintain. Instead, a simple, straightforward class — called Content— was designed that represents a content item in the database. The class exposes a num- ber of properties like its Title, IntroText, and its CategoryIdand has two methods to get the content item in and out of the database. You can find the definition of the Contentclass in the file Content.vb in the App_Code\BusinessLogic folder of the web site. Figure 5-5 shows the design of the Contentclass.

The following table lists each of the seven public properties the class exposes:

Property Data Type Description

BodyText String The BodyTextproperty holds the full text for the content item and is displayed on the detail page only. CategoryId Integer Indicates to which category the content item belongs. ContentTypeId Integer Indicates to which content type the content item belongs. Id Integer This is the unique ID of the content item and is

assigned by the database automatically whenever a new item is inserted.

IntroText String This property contains the introduction text for the content item. This intro text is displayed, possibly with a different formatting, on the content list page and at the top of the content detail page.

Title String This is the title of the content item as it appears on the content list and detail pages.

Visible Boolean Determines whether the item is visible in the public area of the web site.

In addition to these seven properties, the Contentclass has a total of four methods: the two construc- tors, a Savemethod, and a GetItemmethod, each of which is discussed in the following table:

Method Return Type Description

Public Sub n/a The default constructor for the Contentclass. Initializes New() a new instance with all of its properties set to their

default values.

Public Sub n/a This overloaded constructor initializes a new instance New (ByVal id of the Contentclass with all of its properties set to As Integer) their default values except for the Idthat is filled with

the ID passed to constructor. This overload is used to re-create existing items when updating them in the management section.

Public Sub n/a Saves a new or an existing content item in the database Save() by calling the Savemethod in the ContentDBclass,

which is discussed later.

Public Shared An instance of Gets an existing content item from the database by Function the Content calling GetItemin the ContentDBclass.

GetItem class or (ByVal id As Nothingwhen Integer) As the item could Content not be found.

The Data Access Layer

Because most of the pages in the site use SqlDataSourcecontrols for their data access, you need only one class in the data access layer: the ContentDBclass, shown in Figure 5-6, which is responsible for retrieving and saving a single content item from the database.

Figure 5-6

Because the ContentDBclass exposes only shared members, it has no public constructor. It also has no public properties, but it does have two public methods, outlined in the following table:

Method Return Type Description

Public Shared Sub Save n/a Saves a new or an existing

(ByVal contentItem As content item in the database.

Content)

Public Shared Function An instance of the Content Gets an existing content item GetItem (ByVal id As class or Nothingwhen the from the database.

Integer) As Content item could not be found.

In addition to this single class, the data access layer contains the database that is discussed next.

In document Instant Results pdf (Page 159-162)