/ Home / HowTo / ProvinceManager

This Strong Typed Manager is specific to the Province Data Object and the associated Province Table. This makes it easier to use with less application code than the SQLManager alone.

This class encapsulates the setup code for the Province Data Object and prevents it from having to be repeated everywhere it is used in the application code.

using LJCDBClientLib;
using LJCDataGridLib;
using LJCDBMessage;
using LJCNetCommon;
using LJCWinFormControls;
using System.Collections.Generic;
using System.Data;

/// <summary>Provides Province SQL data methods.</summary>
public class ProvinceSQLManager
{
  #region Constructors

  // Initializes an object instance.
  /// <include path='items/DbManagerC/*' file='../../LJCDocLib/Common/SQLManager.xml' />
  public ProvinceSQLManager(string dataConfigName, string tableName
    , string connectionString = null, string providerName = null)
  {
    Reset(dataConfigName, tableName, connectionString, providerName);
  }

  // Resets the data access configuration.
  public void Reset(string dataConfigName, string tableName
    , string connectionString = null, string providerName = null)
  {
    if null == tableName)
    {
      tableName = "Province";
    }

    SQLManager = new SQLManager(dataConfigName, tableName, connectionString
      , providerName);
    if (SQLManager.DataDefinition != null)
    {
      BaseDefinition = SQLManager.DataDefinition;
      DataDefinition = BaseDefinition.Clone();

      // Create the list of DB Assigned and Lookup column names.
      SQLManager.DbAssignedColumns = new List()
      {
        "ID"
      };
      SQLManager.SetLookupColumns(new string[]
      {
        "Name"
      });

      ResultConverter = new ResultConverter<Province, Provinces>();
    }
  }
  #endregion

  #region Public Data Methods

  // Adds a record to the database.
  /// <include path='items/Add/*' file='../../LJCDocLib/Common/SQLManager.xml' />
  public Province Add(Province dataObject, List<string> propertyNames = null)
  {
    Province retValue = null;

    // The data record must not contain a value for DB Assigned columns.
    DataTable dbAssignedValues = SQLManager.Add(dataObject, propertyNames);
    AffectedCount = SQLManager.AffectedCount;
    SQLStatement = SQLManager.SQLStatement;
    if (dbAssignedValues != null)
    {
      retValue = CreateProvince(dbAssignedValues);
    }
    return retValue;
  }

  // Deletes the records with the specified key values.
  /// <include path='items/Delete/*' file='../../LJCDocLib/Common/SQLManager.xml' />
  public void Delete(DbColumns keyColumns, DbFilters filters = null)
  {
    SQLManager.Delete(keyColumns, filters);
    AffectedCount = SQLManager.AffectedCount;
    SQLStatement = SQLManager.SQLStatement;
  }

  // Loads a collection of Data Objects.
  /// <include path='items/Load/*' file='../../LJCDocLib/Common/SQLManager.xml' />
  public Provinces Load(DbColumns keyColumns = null
    , List propertyNames = null, DbFilters filters = null
    , DbJoins joins = null)
  {
    Provinces retValue = null;

    DataTable dataTable = LoadDataTable(keyColumns, propertyNames, filters
      , joins);
    if (dataTable != null)
    {
      retValue = CreateProvinces(dataTable);
    }
    return retValue;
  }

  // Loads a DataTable of records.  
  public DataTable LoadDataTable(DbColumns keyColumns = null
    , List propertyNames = null, DbFilters filters = null
    , DbJoins joins = null)
  {
    DataTable retValue;

    retValue = SQLManager.GetDataTable(keyColumns, propertyNames
      , filters, joins);
    SQLStatement = SQLManager.SQLStatement;
    return retValue;
  }

  // Retrieves a record from the database.
  /// <include path='items/Retrieve/*' file='../../LJCDocLib/Common/SQLManager.xml' />
  public Province Retrieve(DbColumns keyColumns
    , List propertyNames = null, DbFilters filters = null
    , DbJoins joins = null)
  {
    Province retValue = null;

    DataTable dataTable = SQLManager.GetDataTable(keyColumns, propertyNames
      , filters, joins);
    SQLStatement = SQLManager.SQLStatement;
    if (dataTable != null)
    {
      retValue = CreateProvince(dataTable);
    }
    return retValue;
  }

  // Updates the record.
  /// <include path='items/Update/*' file='../../LJCDocLib/Common/SQLManager.xml' />
  public void Update(Province dataObject, DbColumns keyColumns
    , List propertyNames = null, DbFilters filters = null)
  {
    SQLManager.Update(dataObject, keyColumns, propertyNames, filters);
    AffectedCount = SQLManager.AffectedCount;
    SQLStatement = SQLManager.SQLStatement;
  }
  #endregion

  #region GetKey Methods

  // Gets the ID key record.
  /// <include path='items/GetIDKey/*' file='../../LJCDocLib/Common/Manager.xml' />
  public DbColumns GetIDKey(int id)
  {
    var retValue = new DbColumns()
    {
      { "ID", id }
    };
    return retValue;
  }
  #endregion

  #region Private Methods

  // Creates the object from the first data row.
  private Province CreateProvince(DataTable dataTable)
  {
    Province retValue = null;

    if (NetCommon.HasData(dataTable))
    {
      // Sets object values where DataTable column names match
      // the object property names and DataTable column row
      // values are not null.
      retValue = ResultConverter.CreateDataFromTable(dataTable
        , dataTable.Rows[0], DataDefinition);
    }
    return retValue;
  }

  // Creates the collection from a DataTable.
  private Provinces CreateProvinces(DataTable dataTable)
  {
    Provinces retValue = null;

    if (NetCommon.HasData(dataTable))
    {
      retValue = ResultConverter.CreateCollectionFromTable(dataTable
        , DataDefinition);
    }
    return retValue;
  }
  #endregion

  #region Properties

  /// <summary>Gets or sets the non-select affected record count.</summary>
  public int AffectedCount { get; set; }

  /// <summary>Gets or sets the Base Columns definition.</summary>
  public DbColumns BaseDefinition { get; set; }

  /// <summary>Gets or sets the Data Columns definition.</summary>
  public DbColumns DataDefinition { get; set; }

  /// <summary>Gets or sets the ResultConverter reference.</summary>
  public ResultConverter ResultConverter { get; set; }

  /// <summary>Gets the SQLManager reference.</summary>
  public SQLManager SQLManager { get; private set; }

  /// <summary>Gets or sets the last SQL statement.</summary>
  public string SQLStatement { get; set; }
  #endregion
}
/ Home / HowTo / ProvinceManager

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