ManuelAbadia.com
ExtendedObjectDataSource samples
Samples



Product Information

Generics Support

The ObjectDataSource does not have any kind of design time support to use generic types and it does't support generic methods.

The CompatObjectDataSource and the ExtendedObjectDataSource have full support for generic types and generic methods to not restrict the developer's potential.

The syntax to specify a generic type is the same supported by the .NET Framework GetType method. For example, the generic type:

System.Collections.Generic`1[System.Int32]

represents the class System.Collections.Generic<int>.

For non framwork types the assembly name is also required so the syntax can be quite complex:

Manu.GenericTests.GenericDAO`1[[Manu.Business.Product, Business]], __code

represents the class Manu.GenericTests.GenericDAO<Manu.BusinessProduct>, where the Product class is in an assembly called Business and the GenericDAO class is in the App_Code folder.

For generic methods, a similar syntax is available. For example:

GetObjectList`2[System.String, [Manu.GenericTests.Country, __code]]

is used to represent the GetObjectList<string, Manu.GenericTests.Country> method.

To hide the complexity of generics, the CompatObjectDataSource and the ExtendedObjectDataSource have full design time support, so the developer does not need to know the underlying details.

The following example will use a generic method to get some data stored in a registry. When the application starts a registry is created and some data is stored in it. Among those data there is a list of available countries. The registry has the following methods:

public class Registry

{

    public T GetObject<K, T>(K key);

    public IList<T> GetObjectList<K, T>(K key);

    public void RegisterObject<K, T>(K key, T obj);

    public void RegisterObjectList<K, T>(K key, IList<T> objects);

}

The example use a CompatObjectDataSource to populate a DropDownList with the country list from the registry using the GetObjectList<string, Country> method.

Country list:

Sample source code

The ASPX file for this sample is:


<%@ Page Language="C#" MasterPageFile="~/DefaultMaster.master" AutoEventWireup="true" CodeFile="Generics.aspx.cs" Inherits="Generics" Title="Generics support" %>

 

<%@ Register Assembly="ExtendedObjectDataSource" Namespace="Manu.Web.UI.WebControls" TagPrefix="manu" %>

 

<asp:Content ID="Content1" ContentPlaceHolderID="Content" Runat="Server">

    <h1>Generics Support</h1>

    Country list:

    <asp:DropDownList ID="ddlCountries" runat="server"

            DataSourceID="CompatObjectDataSource1"

            DataTextField="Name" DataValueField="Id">   

    </asp:DropDownList><br />

    <manu:CompatObjectDataSource ID="CompatObjectDataSource1" runat="server"

        SelectMethod="GetObjectList`2[System.String, [Manu.GenericTests.Country, __code]]"

        TypeName="Manu.GenericTests.Registry, __code"

        OnObjectCreating="CompatObjectDataSource1_ObjectCreating">

        <SelectParameters>

            <asp:Parameter Name="key" Type="String" DefaultValue="countries" />

        </SelectParameters>

    </manu:CompatObjectDataSource>

</asp:Content>


The code-behind file (ASPX.CS) is:


using System;

using System.Data;

using System.Web.UI.WebControls;

using Manu.GenericTests;

 

public partial class Generics : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

    }

 

    protected void CompatObjectDataSource1_ObjectCreating(object sender, ObjectDataSourceEventArgs e)

    {

        // as the register is a singleton, supply the one and only instance to the CompatObjectDataSource

        e.ObjectInstance = Registry.Instance;

    }

}