Apr 18 2006

DataObjectTypeName and ObjectDataSource

Category: ASP.NET 2.0 - GeneralBil@l @ 10:32

For most of you using the ObjectDataSource, you must have passed by DataObjectTypeName property of the ObjectDataSource.
I have been working with it for a while, but thought of sharing with you the beauty of that property.

Usually, ObjectDataSource uses two modes to pass parameters for the data mapper's methods:

  • Simple Data Types (string, int, etc ...)
  • Custom Objects (Input for methods would be an object)

When you set the DataOjectTypeName ="MyClass" of the ObjectDataSource, then the following should be valid:

  • All CRUD (Create, Retrieve, Update, Delete) methods specified in the TypeName property of ObjectDataSource should expect a parameter of type "MyClass" as an input parameter, except for the Retrieve method for sure. For instance:
    public static void Delete(MyClass p_Myclass)
    { }
  • The MyClass is a simple object, that has some public properties like: ID, Name, Email, etc ... Make sure that the properties' names match exactly witht the names of the fields displayed by the Data-Bound Controls. For instance, if you have a property called ID in the MyClass, the GridView displaying the records, should have the name of the BoundField representing the ID as "ID", you see how names match?
  • No more needed to list all Select,Update,Insert,Delete parameters in the ObjectDataSource. At runtime, an instance of type MyClass is created for us out of the Data-Bound Control (example: GridView) and sent to the data mapper's methods!

I asked myself, wouldn't that be waste of time if I am sending the whole object to the Delete Method, in a time, I only need for example ID to delete a record? Well, the answer came back directly from the ObjectDataSource as follows: The proccess is so inteligent that when the Delete Button is clicked, the key specified in the DataKeyNames property of the GridView for example, is supplied to the MyClass instance, so only the ID is filled and the rest of fields are igonored. Isn't that great?

Check this simple example to see How Stuff Works ;)

GridView + ObjectDataSource       

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="ObjectDataSource1">
            <Columns>
                <asp:BoundField DataField="LastName" HeaderText="LastName" SortExpression="LastName" />
                <asp:BoundField ReadOnly="true" DataField="EmployeeId" HeaderText="EmployeeId" SortExpression="EmployeeId" />
                <asp:BoundField DataField="FirstName" HeaderText="FirstName" SortExpression="FirstName" />
            </Columns>
        </asp:GridView>
        &nbsp;</div>
        <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" DataObjectTypeName="Employee"
            DeleteMethod="Delete" SelectMethod="Get" TypeName="EmployeeDal"></asp:ObjectDataSource>

Employee and EmployeeDal

public class Employee
{
    #region Private Members
    private int m_EmployeeId;
    private string m_FirstName;
    private string m_LastName;
    #endregion

    #region Constructors
    public Employee()
    { }

    public Employee(int p_EmployeeId, string p_FirstName, string p_LastName)
    {
        this.m_EmployeeId = p_EmployeeId;
        this.m_FirstName = p_FirstName;
        this.m_LastName = p_LastName;
    }
    #endregion

    #region Public Properties
    public int EmployeeId
    {
        get { return this.m_EmployeeId; }
        set { this.m_EmployeeId = value; }
    }

    public string FirstName
    {
        get { return this.m_FirstName; }
        set { this.m_FirstName = value; }
    }
    public string LastName
    {
        get { return this.m_LastName; }
        set { this.m_LastName = value; }
    }
    #endregion
}

--------------------

using System;
using System.Data;
using System.ComponentModel;
using System.Configuration;
using System.Collections.Generic;

[DataObjectAttribute]
public class EmployeeDal
{
    public EmployeeDal()
    {   }

    [DataObjectMethodAttribute(DataObjectMethodType.Delete, true)]
    public static void Delete(Employee p_Employee)
    {
        // your logic to delete a record using the parameter EmployeeId as follows
        int EmployeeId = p_Employee.EmployeeId;
    }
   
    [DataObjectMethodAttribute(DataObjectMethodType.Select, true)]
    public List<Employee> Get()
    {
        // your logic to retrieve all records

        return new List<Employee>();
    }
}

Hope this helps,

Regards

Tags:

Comments are closed