3.5 Propuesta metodológica para la elicitación de requisitos
3.5.3 Tarea 3: Identificar/revisar los objetivos del sistema
The SurveyDBclass (see Figure 4-16) is used to as the data layer of the application. It is essentially the main go-between for all method calls from the business tier that require access to the database. No other class or code section of the application makes data-related executions except for this SurveyDBclass.
Figure 4-16
The following table displays the accessible members of the SurveyDBclass:
Method Return Type Description
Delete n/a Deletes a survey from the database.
DeleteQuestion n/a Deletes a question from the database. Get Survey Returns in instance of the class by sending in
the survey ID.
GetCurrentSurvey DataSet Retrieves the current survey from the database.
GetQuestionIDsForSurvey Collection Retrieves a collection of survey question IDs from the database.
GetQuestionsForSurvey DataSet Retrieves a DataSet of survey questions from the database.
GetSurveyList DataSet Retrieves a DataSet of surveys from the database.
Method Return Type Description
SaveQuestion Boolean Saves a question to the survey in the database.
SaveResponses Boolean Saves a response to the question in the survey. SaveSurveyAsCurrent n/a Makes a specified survey the current one
within the database.
The next class portrays the configuration class that has been commonly used in this book.
The Config Class
The Configclass, depicted in Figure 4-17, is used as the configuration manager of the application. It is essentially the main access point for all configuration settings that any of the application tiers may require access to. No other class or code section of the application makes configuration-related calls except for this Configclass.
Figure 4-17
The following table displays the accessible members of the Configclass:
Property Return Type Description
ConnectionString String The connection string property that pulls from Web.config.
CurrentTheme String The current theme of the web site as defined in the Web.config file.
PageTitle String The HTML title value that each page displays, as defined here from the Web.config file. So you have a good idea at this point about what classes are involved in the application, and how those classes may be used. The next section explains the detailed business logic within the application and the processes or workflow that they accommodate.
Code and Code Explanation
This section explains each of the essential code files in the Wrox Survey Engine project. You look in detail at the files in the each of the different folders and learn how they interact and are used across the project.
Root Files
The root of the Wrox Survey Engine contains several important files, including the main ASPX shell- pages, and the configuration and formatting pages.
Web.config
The Web.config stores vital configuration entries used within the application. One entry, named the SqlServerConnectionString, controls the connection to the database, as shown here:
<connectionStrings>
<add name=”ConnectionString” connectionString=”Data
Source=(local)\SqlExpress;AttachDbFilename=|DataDirectory|\SurveyDB.mdf;Integrated Security=True;User Instance=True” providerName=”System.Data.SqlClient”/>
</connectionStrings>
The SqlServerConnectionStringalso contains information managing the SMTP e-mail settings for sending out e-mails:
<appSettings>
<add key=”EmailFrom” value=”[email protected]” /> <add key=”EmailTo” value=”[email protected]” />
The Web.config is also used to provide easy modification to the themes in use for the entire site. You can find more information on this in the “Themes and Skins” section earlier in the chapter.
Survey.vb
The Surveyclass is one of the most important areas of the Survey Engine application. The class contains methods and properties that allow for the storage of survey-related information and logic to implement updates to that information within the data access layer. Some of the methods provide access to the gen- eral information for surveys, whereas others provide the capability to obtain a full dataset of all surveys. In addition, the GetQuestionsmethod returns all of the questions for any given survey.
This Survey.vbclass can also be bound to an ObjectDataSourcecontrol within the user interface, thereby providing a business layer for the application. Its methods are listed as public and shared to pro- vide a more rapid development model without being required to instantiate an instance of the Survey class in order to call its methods or access its members.
By using #Regiontags in the Survey.vb class file, the Visual Studio IDE allows the page to be grouped into organized sections. Sections that are commonly used to group the code in this way include
Variables, Constructors, Methods, and Properties. This does not impact the .NET assemblies in any way, but is simply a great way to maintain organized logic. Figure 4-18 is a visual display of the regionalized code as it is displayed within the Visual Studio IDE.
Figure 4-18
One of the more important method calls of the survey is the SaveSurveymethod. The code for this is as follows:
Public Shared Sub SaveSurvey(ByVal Name As String, ByVal Description As String, ByVal ID As Integer)
Dim mSurvey As New Survey mSurvey.ID = ID
mSurvey.Name = Name
mSurvey.Description = Description SurveyDB.Save(mSurvey)
End Sub
This method provides the means by which to hand off a Surveyclass object to the data tier for processing.
Config.vb
The Configclass is used as an available object with three static members. Its members are listed as properties in order to abstract the location in which these values are stored. Currently, the three proper- ties are ConnectionString, CurrentTheme, and PageTitle. The values for the three properties are stored in the Web.config file, with a Configclass to retrieve them when they are needed:
Imports Microsoft.VisualBasic
Public Class Config ‘’’ <summary>
‘’’ The connection string property that pulls from the web.config ‘’’ </summary>
Public Shared ReadOnly Property ConnectionString() As String Get
Return ConfigurationManager.ConnectionStrings(“ConnectionString”) .ConnectionString
End Property ‘’’ <summary>
‘’’ The current theme of the website as defined in the web.config file ‘’’ </summary>
Public Shared ReadOnly Property CurrentTheme() As String Get
Return ConfigurationManager.AppSettings(“CurrentTheme”).ToString() End Get
End Property ‘’’ <summary>
‘’’ The HTML title value that each page displays, as defined here from the web.config file
‘’’ </summary>
Public Shared ReadOnly Property PageTitle() As String Get
Return ConfigurationManager.AppSettings(“PageTitle”).ToString() End Get
End Property End Class
As the preceding Configclass displays, the properties ConnectionString, CurrentTheme, and PageTitleare marked as Public Shared ReadOnly, which allows them to be accessed from anywhere in the project by the config-dot notation. An example of this would be config.ConnectionString(). This would return the connection string from the Configclass, without instantiating a Configclass object first.
SurveyDB.vb
This class is essentially the data layer for the application. It provides method calls in order to retrieve information from the database and insert or update data within the database as well. This class serves as the only file or object that will have access to the database files. In this way, you isolate data-specific operations outside of the business logic layer. In so doing, you can see that it protects a developer from writing duplicate data access code because it is organized in nature and located in the same place. This also allows for the application to be logically separated into tiers, or layers, with the deliberate feasibility of migrating and expanding the application onto separate servers at any point in time.
In line with the documented function call from the Surveyclass, the surveyDBclass contains a Save method, as displayed here:
Public Shared Function Save(ByVal mSurvey As Survey) As Integer
Using mConnection As New SqlConnection(Config.ConnectionString)
Dim mNewSurveyID As Integer Dim mCommand As SqlCommand = New
SqlCommand(“sprocSurveyInsertUpdateItem”, mConnection) mCommand.CommandType = CommandType.StoredProcedure If mSurvey.ID > 0 Then mCommand.Parameters.AddWithValue(“@id”, mSurvey.ID) Else mCommand.Parameters.AddWithValue(“@id”, DBNull.Value) End If mCommand.Parameters.AddWithValue(“@name”, mSurvey.Name)
mCommand.Parameters.AddWithValue(“@description”, mSurvey.Description) If mSurvey.IsCurrentSurvey = False Then
mCommand.Parameters.AddWithValue(“@iscurrentsurvey”, 0) Else mCommand.Parameters.AddWithValue(“@iscurrentsurvey”, 1) End If mConnection.Open() mNewSurveyID = mCommand.ExecuteScalar() mConnection.Close() Return mNewSurveyID End Using End Function
This accepts a parameter of the type surveyand accesses the members to save values into the database. Another method of interest is the GetCurrentSurvey()method, returning a DataSet of the currently selected survey in the system. The following is a code excerpt for this method:
‘’’ <summary>
‘’’ Retrieves the ‘current’ survey from the database ‘’’ </summary>
Public Shared Function GetCurrentSurvey() As DataSet Dim dsSurveys As DataSet = New DataSet()
Try
Using mConnection As New SqlConnection(Config.ConnectionString)
Dim mCommand As SqlCommand = New SqlCommand (“sprocSurveySelectSingleItemWhereCurrent”, mConnection)
mCommand.CommandType = CommandType.StoredProcedure
Dim myDataAdapter As SqlDataAdapter = New SqlDataAdapter() myDataAdapter.SelectCommand = mCommand myDataAdapter.Fill(dsSurveys) mConnection.Close() Return dsSurveys End Using Catch ex As Exception
‘When you call the “Throw” statement, you are raising the error to the global.asax file, which will use the default error handling page to process/display the custom error to the user
Throw End Try End Function
The preceding page logic performs the following steps:
1.
Creates a new SqlCommandobject, passing in the stored procedure name and the connection.2.
Sets the command type to be stored procedure.3.
Creates a new DataAdapter.5.
Calls the data adapter’s Fillmethod, passing in the DataSet to be filled with data.6.
Closes the connection.7.
Returns the DataSet to the caller.It is worth mentioning that the CurrentSurveyweb user control is the way that you currently display a survey to the user. By extending the application, you could offer a list of surveys to choose from, and provide dynamic page logic to pull the right survey for the user to complete.
WebForms
The WebForms are standard ASPX pages that contain the client-side graphical user interface of the applica- tion. A few WebForms are of particular importance within the project, as noted in the following sections.
Default.aspx
The Default.aspx file is of course used as the first page that loads when the site is accessed. Within this page is an instance of the user control currentsurvey.ascx, which provides visibility to the title and description of the survey that is marked as current in the database. In this way, the web site viewers will be able to see a survey they can click to complete and view the results.
Login.aspx
The Login page contains a Logincontrol and a PasswordRecoverycontrol. As mentioned in other chapters, these are brand new to the .NET environment. This Login.aspx WebForm is located at the root of the site and is not using a master page. The login controls contain HTML markup that defines the spe- cific values for the destination page and text values of the controls:
<fieldset style=”height: 128px; width: 270px;”>
<asp:Login ID=”Login1” runat=”server” DestinationPageUrl= “~/Management/ Admin.aspx”>
</asp:Login> </fieldset>
<fieldset style=”height: 118px; width: 270px;”>
<asp:PasswordRecovery ID=”PasswordRecovery1” runat=”server”> </asp:PasswordRecovery>
</fieldset>
This HTML markup contains the control definitions for the Loginand PasswordRecoverycontrols and their properties.
TakeSurvey.aspx
The TakeSurvey.aspx WebForm is used to provide a survey from the database to complete with inserts into the response table. The basic controls on the WebForm are an ObjectDataSourcecontrol, SqlDataSourcecontrol, DataListcontrol, and a set of fields within the DataListcontrol that are bound to the object properties. The following excerpt is the defined values of an ObjectDataSource control as it is used to bind to values from the SelectMethodof its designated Surveybusiness object. The GetQuestionsmethod is used to retrieve the survey table records in the form of a DataSet, for this ObjectDataSourcecontrol to bind to:
<asp:ObjectDataSource ID=”odsSurveyQuestions” runat=”server” SelectMethod=”GetQuestions” TypeName=”Survey”>
<SelectParameters>
<asp:QueryStringParameter Name=”id” QueryStringField=”surveyID” Type=”Int32” /> </SelectParameters>
</asp:ObjectDataSource>
Just below this section in the form is the DataListcontrol that this ObjectDataSourcecontrol binds to. The fields and settings of this control exist solely within the HTML markup, as displayed here:
<asp:DataList ID=”DataList1” runat=”server” DataSourceID=”odsSurveyQuestions”> <ItemTemplate>
<%=GetQuestionNum()%>. <%#Server.HtmlEncode(Eval(“Text”).ToString())%> <br />
<input name=”Q<%#Eval(“ID”)%>” type=”radio” value=”A”>A.
<%#Server.HtmlEncode(Eval(“OptionA”).ToString())%></option><br /> <input name=”Q<%#Eval(“ID”)%>” type=”radio” value=”B”>B.
<%#Server.HtmlEncode(Eval(“OptionB”).ToString())%></option><br /> <input name=”Q<%#Eval(“ID”)%>” type=”radio” value=”C”>C.
<%#Server.HtmlEncode(Eval(“OptionC”).ToString())%></option><br /> <input name=”Q<%#Eval(“ID”)%>” type=”radio” value=”D”>D.
<%#Server.HtmlEncode(Eval(“OptionD”).ToString())%></option><br /> <br /><br />
</ItemTemplate> </asp:DataList>
This code specifies the object properties that the DataListbinds to by the use of the <%#Eval(“ID”)%> tags. This provides the connectivity and bindings to the object properties for the repeating data values in the DataListcontrol.
User Controls
Some specific user controls in the site assist with the navigation and content display for multiple pages. Because web user controls promote a practice of creating and using reusable code, they were made to be applicable within multiple pages of the site, depending on the nature of the controls.
header.ascx
The headeruser control is used to provide the top area of each page with meaningful content. If any- thing needs to reside at or near the top of a web page, you would add it to the headercontrol so it would be visible through all of the pages.
The following code represents entire header.ascx source:
<%@ Control Language=”VB” AutoEventWireup=”false” CodeFile=”header.ascx.vb” Inherits=”Controls_header” %>
<div style=”text-align: center”> <table><tr> <td><img src=”../Images/headerlogo.gif” /></td> <td><h1><% Response.Write(Page.Title) %></h1> </td> </tr></table> </div>
Notice that the <%Response.Write(Page.Title)%>tags are used to write back to the response stream a title of the web site on the top of each page, which originated from the Web.config file.
footer.ascx
The footeruser control is used as the bottom section of the site, for each page that uses the master page. That is, the footercontrol, among others, is a referenced control within the master page. In this way, it is propagated to all pages in the same exact manner.
The content of the footercontrol is displayed here:
<%@ Control Language=”VB” AutoEventWireup=”false” CodeFile=”footer.ascx.vb” Inherits=”Controls_footer” %>
<a href=”http://wrox.com” target=”_blank”>© 2005 Wrox Press</a> <asp:LoginStatus ID=”LoginStatus1” runat=”server”
LogoutAction=”RedirectToLoginPage” LogoutPageUrl=”~/Login.aspx” />
This excerpt includes a reference to a LoginStatuscontrol, brand new in the ASP.NET 2.0 control set. The new control displays a changing link-button for providing login and logout functionality. When the user is logged in to the site, the LoginStatuscontrol displays a Logout link-button. Clicking the Logout link-button logs users out of the site, and directs them to the Login page. When the user is logged out of the site, the LoginStatuscontrol displays a Login link-button. Clicking the Login link-button directs users to the Login page, where they are able to log in.
navigation.ascx
The navigationuser control is used to provide the reusable menu that each page includes within itself in structure of the site. The menu itself is a brand new ASP.NET 2.0 control that binds to a SiteMapDataSourcecontrol, also new in version 2.0 of the .NET Framework. The SiteMapDataSource control is used to bind to an XML file, wherein the site files are listed as entries in the XML file.
The following excerpt is the HTML markup of the navigationcontrol:
<%@ Control Language=”VB” AutoEventWireup=”false” CodeFile=”navigation.ascx.vb” Inherits=”Controls_Navigation” %>
<asp:Menu ID=”Menu1” runat=”server” DataSourceID=”SiteMapDataSource1” Orientation=”Horizontal”
StaticDisplayLevels=”2”></asp:Menu>
<asp:SiteMapDataSource ID=”SiteMapDataSource1” runat=”server” />
The XML file of the SiteMapDataSourcecontrol is displayed here: <?xml version=”1.0” encoding=”utf-8” ?>
<siteMap xmlns=”http://schemas.microsoft.com/AspNet/SiteMap-File-1.0” > <siteMapNode url=”ContentFiles/default.aspx” title=”Home” description=””>
<siteMapNode url=”ContentFiles/about.aspx” title=”About” description=”” /> <siteMapNode url=”ContentFiles/contact.aspx” title=”Contact Us”
description=”” />
<siteMapNode url=”Management/admin.aspx” title=”Admin” description=”” /> </siteMapNode>
To add a page to the menu of the web site, you must simply copy and paste (with the necessary modifi- cations) an entry of the preceding XML file. In this way, the master page (which contains the only refer- ence to the navigationcontrol) provides visibility to the menu of the site on each page.
surveyresults.ascx
The SurveyResults control displays the results of the survey that is specified via the surveyed QueryString value. It is referenced within the SurveyResults.aspx WebForm page in the ContentFiles folder, and the MgtSurveyResults.aspx page in the Management folder. The SurveyResults control provides visibility to the percentage results of each of the survey questions of the specified survey.
The following HTML markup is from the SurveyResults.aspx page, displaying a SqlDataSourcecontrol. Note the use of the QueryString surveyID as an entry in the <SelectParameters>section of the file:
<asp:SqlDataSource ID=”SqlSurveyResults” runat=”server” ConnectionString= ”<%$ ConnectionStrings:ConnectionString %>” SelectCommand=”SELECT * FROM
[viewAnswerPercentByQuestion] WHERE ([SurveyID] = @SurveyID)”> <SelectParameters>
<asp:QueryStringParameter Name=”SurveyID” QueryStringField=”surveyID” Type=”Int32” />
</SelectParameters> </asp:SqlDataSource>
The next section of the file contains the data-bound DataListcontrol, with all the applicable data- bound fields listed within server-side tags, as shown here:
<asp:DataList ID=”DataList1” runat=”server” DataSourceID=”SqlSurveyResults”> <ItemTemplate>