AbstractDataProcessor<T>
Reference Document | DyDf.js


AbstractDataProcessor<T> -
is a generically typed abstract class that eliminates the need for writing basic select, insert, update, and delete queries. This abstract class works with the following databases.
   - MySQL (MySql.Data.MySqlClient)
   - Oracle (Oracle.ManagedDataAccess.Client)
   - SQL Server (System.Data.SqlClient)

See the following code examples and click the above Reference Document link for more information.


SQL Server example of table Customer.

CREATE TABLE [dbo].[Customer](
       [CustomerID] [int] IDENTITY(1,1) NOT NULL,
       [CustomerName] [varchar](75) NULL,
       [CustomerAddress] [varchar](100) NULL,
       [CustomerCity] [varchar](50) NULL,
       [CustomerState] [varchar](2) NULL,
       [CustomerDate] [datetime] NULL
) ON [PRIMARY]
GO



The following Customer class is the model that represents the above SQL Server table. Customer inherits AbstractDataProcessor<Customer>.

using TaktTech.Com.DB;

public class Customer : AbstractDataProcessor<Customer>
{
       public Customer()
       {
       }

       //CustomerID is an identity column that auto increments. The following property is required when the data table has an identity column.
       protected override string IdentityColumnName { get { return "CustomerID"; } }

       public int CustomerID { get; set; }
       public string CustomerName {get; set;}
       public string CustomerAddress {get; set;}
       public string CustomerCity {get; set;}
       public string CustomerState {get; set;}
       public DateTime CustomerDate {get; set;}
}


Override default page size: The Select method returns data in pages. The default and max page size is 10,000 records. You can add the following constructor to reduce the page size.

       public Customer( int maxRecordsPerPage): base(maxRecordsPerPage)
       {
       }



Examples of methods that use Customer class.

public class CustomerBLL
{
       private readonly string  _dbConn = Hlp.ConfigFile.DbConnStr("CustomerDB");

       public string SaveCustomer(string customerJSON)
       {
              try
              {
                     //Convert JSON string into Customer object
                     Customer customer = Customer.Deserialize(customerJSON);

                     return Save(customer);

              }
              catch
              {
                     throw;
              }
       }

       public string SaveCustomer(int custID, string custName, string custAddr, string custCity, string custState, DateTime custDate)
       {
              try
              {
                     Customer customer = new Customer();

                     customer.CustomerID = custID;
                     customer.CustomerName = custName;
                     customer.CustomerAddress = custAddr;
                     customer.CustomerCity = custCity;
                     customer.CustomerState = custState;
                     customer.CustomerDate = custDate;

                     return Save(customer);

              }
              catch
              {
                     throw;
              }
       }

       private string Save(Customer customer)
       {
              try
              {
                     //The DB Type and Connection string must be set before your class can process data.
                     customer.SetDBConnection(Db.Types.SqlServer, _dbConn);

                     //CustomerID should be zero when adding new customers.
                     if(customer.CustomerID == 0)
                     {
                            customer.Insert();
                     }
                     else
                     {
                            //The where clause parameter is not required when the IdentityColumnName property is populated.
                            customer.Update();
                     }

                     return customer.ToString(); //Returns JSON string.

              }
              catch
              {
                     throw;
              }
       }

       public string GetCustomer(int custID)
       {
              try
              {
                     ManageDataPages customers = new Customer().SetDBConnection(Db.Types.SqlServer, _dbConn)
                                                                                                           .Select(new SqlWhere().Clause("CustomerID", Operator.Num.equal, custID));

                     // Alternate call
                     ManageDataPages customers = new Customer().SetDBConnection(Db.Types.SqlServer, _dbConn)
                                                                                                           .Select(new SqlWhere().Clause($"CustomerID={custID}"));

                     //Return JSON array string
                     return customers.SerializedData;
              }
              catch
              {
                     throw;
              }
       }

       public string GetCustomer(List<int> custIdList)
       {
              try
              {
                     ManageDataPages customers = new Customer().SetDBConnection(Db.Types.SqlServer, _dbConn)
                                                                                                     .Select(new SqlWhere().Clause("CustomerID",custIdList);

                     // Alternate call
                     var list = string.Join<int>(",", custIdList);
                     ManageDataPages customers = new Customer().SetDBConnection(Db.Types.SqlServer, _dbConn)
                                                                                                     .Select(new SqlWhere().Clause($"CustomerID IN({list})"));

                     //Returns a JSON array string
                     return customers.SerializedData;
              }
              catch
              {
                     throw;
              }
       }

       public void DeleteCustomer(int custID)
       {
              try
              {
                     SqlWhere whereClause = new SqlWhere();
                     whereClause.Clause("CustomerID", Operator.Num.equal, custID);

                     new Customer().SetDBConnection(Db.Types.SqlServer, _dbConn).Delete(whereClause);
              }
              catch
              {
                     throw;
              }
       }
}