Stored Procedure Wrapping

The Enterprise edition of RapTier generates stored procedure wrapper methods that significantly simplify code, which is needed to execute stored procedures. Also, the wrapper methods isolate the business logic code from the low-level ADO.NET classes and allow the compiler to check the parameter types at design time.

RapTier generates two wrapper methods for every stored procedure in the database:

<StoredProcedureCodeName>_Command - creates an ADO.NET command that can be used to call the stored procedure using the ExecuteReader, ExecuteNonQuery, or ExecuteScalar method.

<StoredProcedureCodeName>_DataTable - executes the stored procedure and returns output as a DataTable object.

Below you will find two examples. The first one demonstrates  calling a stored procedure using a wrapper method generated by RapTier. The second example shows a piece of code that has the same functionality, but utilizes the low-level ADO.NET classes. As you can see, the wrapper methods are easier to use.

// Call AddCustomer stored procedure using wrapper method
using(MyDb db = new MyDb())
{
    IDbCommand cmd = db.StoredProcedures.AddCustomer_Command(
            firstName, lastName, birthday, phoneNumber);
    cmd.ExecuteNonQuery();
}

// Call AddCustomer stored procedure using raw ADO.NET
OleDbConnection conn = new OleDbConnection(myConnectionString);
try
{
    OleDbCommand cmd = new OleDbCommand();
    cmd.Connection = conn;
    cmd.CommandText = "AddCustomer";
    cmd.CommandType = CommandType.StoredProcedure;
    OleDbParameter parameter;
    parameter = cmd.Parameters.Add("FirstName", OleDbType.VarChar);
    parameter.Value = firstName;
    parameter = cmd.Parameters.Add("LastName", OleDbType.VarChar);
    parameter.Value = lastName;
    parameter = cmd.Parameters.Add("Birthday", OleDbType.Date);
    parameter.Value = birthday;
    parameter = cmd.Parameters.Add("PhoneNumber", OleDbType.VarChar);
    parameter.Value = phoneNumber;
    conn.Open();
    cmd.ExecuteNonQuery();
}
finally
{
    conn.Close();
}

Overriding wrapper methods

You can customize the wrapper methods by overriding them. For example, you may want to perform additional validation of the parameter values before passing data to the stored procedure. To override a wrapper method add your custom code to the StoredProcedures class.

Example:
public override IDbCommand AddCustomer_Command(string firstName,
                                               string lastName,
                                               DateTime birthday,
                                               string phoneNumber)
{
    if (birthday.Year < 1900 || birthday > DateTime.Today)
        throw new ArgumentException("Invalid parameter value.", "birthday");
    return base .AddCustomer_Command(firstName, lastName, birthday, phoneNumber);
}