/ Home / HowTo / DAL

The Data Access Layer

A Data Access Layer (DAL) is useful for simplifying data access functionality and reuse. It separates the data access code from the calling programs. It also provides a strongly typed, object oriented view of the data.

A DAL library usually provides classes for a group of related database tables. These are often classes for a specific application, module or common application function.

A DAL library usually has the name of the group of tables it represents followed with the text "DAL".

Since the DAL pattern is used extensively in the LJC Code, the following examples will follow this structure. The sample code will also attempt to use the common LJC structure, formatting and naming conventions.


The following examples use the table created with this script.

IF NOT EXISTS(SELECT* FROM INFORMATION_SCHEMA.TABLES
  WHERE TABLE_NAME = N'Person')
BEGIN
CREATE TABLE[dbo].[Person]
(
  [ID][bigint] IDENTITY(1,1) NOT NULL,
  [Name] [nvarchar] (60) NULL,
  [PrincipleFlag] [bit] NOT NULL,
  CONSTRAINT[PK_Person]
  PRIMARY KEY CLUSTERED(
  [ID] ASC),
)
END
GO

The Data Object

A Data Object contains public properties for each data value in a data row. It is a strongly typed, object oriented representation of the data row for use in code. A data row is sometimes referred to as a "record". A data value is sometimes referred to as a "column".

A Data Object usually has the same name as the base table that contains the data columns it represents. It may contain methods for cloning, sorting and displaying an object string identifier.

A Data Object in this context refers to a basic DAL object type. It does not refer to the System.Windows.Forms.DataObject class. A "record" in this context is a basic concept and does not refer to the C# (record) built-in reference type.

using System;

namespace DataTestDAL
{
  // The Person table Data Object.
  public class Person
  {
    #region Constructors

    // Initializes an object instance.
    public Person()
    {
    }
    #endregion

    #region Data Methods

    // The object string identifier.
    public override string ToString()
    {
      return Name;
    }
    #endregion

    #region Data Properties

    // Gets or sets the ID value.
    public int ID { get; set; }

    // Gets or sets the Name value.
    public string Name { get; set; }

    // Gets or sets the PrincipleFlag value.
    public bool PrincipleFlag { get; set; }
    #endregion

    #region Class Data

    /// <summary>The table name.
    public static string TableName = "Person";

    /// <summary>The ID column name.</summary>
    public static string ColumnID = "ID";

    /// <summary>The ID column name.</summary>>
    public static string ColumnName = "Name";

    /// <summary>The ID column name.</summary>
    public static string ColumnPrincipleFlag = "PrincipleFlag";

    /// <summary>The Name maximum length.</summary>
    public static int LengthName = 60;
    #endregion
  }
}

The Data Object Collection

A Data Object Collection can contain multiple Data Object elements. It is a strongly typed, object oriented representation of a set of related data records.

A Data Object Collection usually has the plural name of the base table that it represents. It may contain methods for sorting, searching and adding new data records.

using System;
using System.Collections.Generic;

namespace DataTestDAL
{
  // Represents a collection of Person objects.
  public class Persons : List
  {
  }
}

The Manager Class

A Data Manager class contains methods for accessing table data. It is a strongly typed, object oriented class containing standard data access methods for a specific base table and related data columns.

A Data Manager usually has the same name as the base table followed with the text "Manager".

A Data Manager usually inherits from or contains a Standard Manager class from the Data Service Client Library.

using System;
using System.Collections.Generic;
using LJCDBClientLib;

namespace DataTestDAL
{
  /// <summary>Provides object specific data manipulation methods.</summary>
  public class PersonManager
    : ObjectManager
  {
    // Initializes an object instance.
    /// 
    public PersonManager(DbServiceRef dbServiceRef, string dataConfigName
      , string tableName = "Person")
      : base(dbServiceRef, dataConfigName, tableName)
    {
    }
  }
}
The DAL Test Program
/ Home / HowTo / DAL

Copyright © Lester J. Clark and Contributors.
Licensed under the MIT License.