/ Home / HowTo / DAL

The Data Access Layer

The Test DAL Program

For the following examples we will concentrate on the LJCDBDataAccessLib.DbDataAccess class.

using System;
using System.Collections.Generic;
using LJCNetCommon;
using LJCDBClientLib;
using LJCDBDataAccessLib;
using PersonDAL;

namespace PersonDALTest
{
  // The program entry point class.
  public class Program
  {
    // The program entry point function.
    static void Main(string[] args)
    {
      PersonManager personManager = null;
      bool success = true;

      string dataConfigName = "PersonTest";
      DbServiceRef dbServiceRef = CreateServiceRef(dataConfigName);
      if (null == dbServiceRef || null == dbServiceRef.DbDataAccess
        || dbServiceRef.DbDataAccess.IsError)
      {
        success = false;
      }
    }

    #region Helper Methods

    // Creates the DbServiceRef object.
    private static DbServiceRef CreateServiceRef(string dataConfigName)
    {
      DbServiceRef retValue = new DbServiceRef()
      {
        DbDataAccess = new DbDataAccess(dataConfigName)
      };
      return retValue;
    }
    #endregion
  }
}

The DbServiceRef Class

The constructor for an ObjectManager derived class takes an LJCDBClientLib.DbServiceRef parameter.

The DbServiceRef class contains three properties:

The Manager class checks these references in the previous order and uses the first one that is not null.

The program starts by create a reference to the DbServiceRef object. It does this by calling the CreateServiceRef() method.

The DbServiceRef.DbDataAccess property is initialized with the DataConfig name. This name refers to a DataConfig entry in the DataConfigs.xml file. This file must be available in the same folder as the calling program. The program folder must also contain the LJCDataConfig.dll library.

<?xml version="1.0" ?>
<DataConfigs xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <DataConfig>
  <Name>PersonTest</Name>
  <DbServer>DDbServiceName</DbServer>
  <Database>DatabaseName</Database>
  <ConnectionType>SQLServer</ConnectionType>
  </DataConfig>
</DataConfigs>

The program folder must also contain the ConnectionTemplate.xml file.

The ConnectionType value in the DataConfig.xml file refers to the Name value in the ConnectionTemplate.xml file. The ConnectionTemplate is used to create the Connection String for the program data access. It changes the replaceable parameters in braces with the associated DataConfig values.

<?xml version="1.0" ?>
<ConnectionTemplates xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <ConnectionTemplate>
  <Name>SQLServer</Name>
  <Template>Data Source={DbServer}; Initial Catalog={Database}; Integrated Security=True</Template>
  </ConnectionTemplate>
</ConnectionTemplates>

The ObjectManager Derived Class

// Creates the PersonManager object.
private static PersonManager CreatePersonManager(DbServiceRef dbServiceRef
  , string dataConfigName)
{
  PersonManager retValue = new PersonManager(dbServiceRef, dataConfigName);
  return retValue;
}

In the Program class, Main() function, add the code to create the PersonManager object immediately after the code that creates the DbServiceRef object.

if (success)
{
  personManager = CreatePersonManager(dbServiceRef, dataConfigName);
  if (null == personManager)
  {
    success = false;
  }
}

The Manager class inherits data access methods from the ObjectManager base class. The most commonly used of these methods are: Add(), Delete(), Load(), Retrieve() and Update(). We will create test methods for each of these common manager methods.

// Write the record string.

private static string WriteRowString(Person person)
{
  string retValue = null;
  if (person != null)
  {
    retValue = string.Format("{0}, {1}, {2}", person.ID
      , person.Name, person.PrincipleFlag);
    Console.WriteLine(retValue);
  }
  return retValue;
}
The DAL Test Load() Method
/ Home / HowTo / DAL

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