WebForms/Sample project 1

From eBabel wiki
Jump to: navigation, search

Compare and contrast the two controls below, where one uses a SqlDataSource and the other a BusinessLogic model from my own library to be populated. The SqlDataSource has a lot more markup in the aspx view and none in code-behind. My custom model has a lot less markup (and less functionality) and a bit of code-behind that calls upon my BusinessLogic custom class library.

The aspx view

GridView with a DataSource

The combination of GridView and DataSource provides a lot of functionality but there is little separation of concerns there. Too much code in the view, none in code-behind (save the protected property declarations that are automatically generated). I don't think it's a good idea to use this approach, unless the business requirement exactly fits the default GridView functionality which is not likely.

<syntaxhighlight lang="xml" line>

   <asp:GridView runat="server" ID="CityBreaksWithDataControlGridView" 
       AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" 
       DataKeyNames="StateProvinceID" DataSourceID="CityBreaksDataSource" >
       <Columns>
           <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
           <asp:BoundField DataField="StateProvinceCode" HeaderText="Province" 
               SortExpression="StateProvinceCode" />
           <asp:BoundField DataField="CountryRegionCode" HeaderText="Country" 
               SortExpression="CountryRegionCode" />
           <asp:BoundField DataField="TerritoryID" HeaderText="Territory" 
               SortExpression="TerritoryID" />
           <asp:CheckBoxField DataField="IsOnlyStateProvinceFlag" 
               HeaderText="Whole country?" SortExpression="IsOnlyStateProvinceFlag" />
       </Columns>
   </asp:GridView>
   <asp:SqlDataSource ID="CityBreaksDataSource" runat="server" 
       ConnectionString="<%$ ConnectionStrings:ApplicationServices %>" 
       SelectCommand="SELECT StateProvinceID, StateProvinceCode, CountryRegionCode, IsOnlyStateProvinceFlag, Name, TerritoryID, rowguid, ModifiedDate FROM Person.StateProvince">
   </asp:SqlDataSource>

</syntaxhighlight>

ListBox without DataSource

<syntaxhighlight lang="xml" line>

   <asp:ListBox runat="server" ID="CityBreakListBox" EnableViewState="False" Rows="1" SelectionMode="Single" />

</syntaxhighlight>

GridView without DataSource

Columns are hand picked from available columns returned by the model

<syntaxhighlight lang="xml" line>

   <asp:GridView runat="server" ID="CityBreaksGridView" EnableViewState="False" AutoGenerateColumns="False">
       <Columns>
           <asp:BoundField DataField="Description" HeaderText="Destinations" />
       </Columns>
   </asp:GridView>

</syntaxhighlight>

GridView without DataSource

Columns generated from the model

<syntaxhighlight lang="xml" line>

   <asp:GridView runat="server" ID="CityBreaksGridView2" EnableViewState="False" />

</syntaxhighlight>

The code behind

<syntaxhighlight lang="csharp" line>

using System;
using AdventureWorks1.ViewModels;
using BusinessLogic;

namespace AdventureWorks1
{
   public partial class Default : BasePage 
   {
       protected void Page_Load(object sender, EventArgs e)
       {
           var model = new CityBreaksModel();

           CityBreakListBox.DataSource = model.Destinations;
           CityBreakListBox.DataValueField = "DestinationId";
           CityBreakListBox.DataTextField = "Description";
           CityBreakListBox.DataBind();

           CityBreaksGridView.DataSource = model.Destinations;
           CityBreaksGridView.DataBind();

           CityBreaksGridView2.DataSource = model.Destinations;
           CityBreaksGridView2.DataBind();

           model.Dispose();
       }
   }
}

</syntaxhighlight>

The classes from the BusinessLogic library

Destination class

<syntaxhighlight lang="csharp" line>

namespace BusinessLogic
{
   public class Destination
   {
       public int DestinationId { get; set; }
       public string Description { get; set; }
   }
}

</syntaxhighlight>

CityBreaksModel class

This business logic class CityBreaksModel accesses the ORM generated by Microsoft Linq to SQL.

<syntaxhighlight lang="csharp" line>

using System.Collections.Generic;
using System.Linq;
using DataAccess;
using System;

namespace BusinessLogic
{
   public class CityBreaksModel : IDisposable
   {
       public List<Destination> Destinations { get; set; }

       public CityBreaksModel()
       {

           Destinations = new List<Destination>();

           var context = new AdventureOrmDataContext();

           var result = (from item in context.StateProvinces select item).ToList();

           foreach (var stateProvince in result)
               Destinations.Add(
                   new Destination
                       {
                           DestinationId = stateProvince.StateProvinceID,
                           Description = stateProvince.Name
                       });

           context.Dispose();
       }

       public void Dispose()
       {
           Destinations = null;
       }
   }
}

</syntaxhighlight>