This tutorial will walk you through the process of creating business logic class manually.
- Creating BLL Classes manually:
- Assuming that your database has Customers, Orders, OrderItems tables (as provided in the QO_Shop sample database. You can very easily create your own Business Logic classes that leverage the power of the Quick Objects Business Logic Framework.
- You can either hand code your classes or use any tool that generates the classes such as QuickObjects.Designer tool. For this example, we have used the class designer in VS 2005. Here is a diagram of the end result and the code is available in the sample project.
- Create four new class file in your project and name them as follows
-
- Customers.cs
- Orders.cs
- Products.cs
- OrderItems.cs
- Now Create a folder and name it Base and in this folder create the following four class files
-
- Customers_Base.cs
- Orders_Base.cs
- Products_Base.cs
- OrderItems_Base.cs
- Each class i.e. Customers, Orders, Products and OrderItems should inherit from their respective Base classes.
using System;
using Akal.QuickObjects.ObjectBase;
namespace QuickObjects.ObjectBase.Samples.BusinessLogicLayer
{
public class Customers : CustomersBase
{
}
}
- Each Base class should inherit from Akal.QuickObjects.ObjectBase.BaseBusinessObject as shown below.
using System;
using Akal.QuickObjects.ObjectBase;
namespace QuickObjects.ObjectBase.Samples.BusinessLogicLayer
{
public class CustomersBase : BaseBusinessObject
{
}
}
- We will review the process of doing one class, and the rest of the classes can be completed by following the exact same process. Now it is time to add the fields to the class. For each column in the Customers table as a property in the class.
-
- CustomerID field is of type int and we will use its corresponding class in the QuickObjects Business Logic Framework i.e. Int32Field.
private Int32Field _CustomerID = null;
public Int32Field CustomerID
{
get //get accessor method
{
return _CustomerID;
}
set //set accessor method
{
// if the _CustomerID variable holds a reference to an instance of Int32Field type then we want to ensure
// that the current instance is removed from the PrimaryKeyFields and Fields collections, so that we can add
// the new instance (being received in the value) to these collections (and avoid duplicate CustomerID fields)
if (_CustomerID != null)
{
this.PrimaryKeyFields.Remove(_CustomerID as BaseField);
this.Fields.Remove(_CustomerID as BaseField);
}
if (value != null)
{
// Name is a very important property for a Field.
// Name property value must be the name of the field in the table
value.Name = "CustomerID";
// AllowNull indicates weather the underlying table's field allows null values or not
value.AllowNull = false;
// If the value for this field is generated by the database, or it is generated by a trigger
// then set the AutoGenerated to true. For example, if CustomerID is an identity column (SQL Server)
// or (in Oracle) the value for CustomerID comes from a sequence and there is a trigger on the table
// that supplies this value, then this field is considered to be AutoGenerated.
// Essentially, if the developer does not need to specify the value for this field while doing an Insert
// in that scenario you can safely set this field as AutoGenerated = true;
value.AutoGenerated = true;
// NOTE, if you have a SEQUENCE (only applicable to Oracle) and would like the business object to
// generate this field's value from the SEQUENCE then you must specify the SEQUENCE Name in the
// SequenceName property to successfully get the value for this field at the time of insertion.
// The following line is commented as it is not applicable to SQL Server.
//value.SequenceName = "MyOracleSequenceName";
// The AllowSave property determines if a field can be included in Insert or Update statements.
// Sine CustomerID is auto generated (i.e. Identity) and it is primary key and we don't intend to change its
// value, we will set the AllowSave to false.
value.AllowSave = false;
// This sets the Field's BusinessObject property to reference the current instance of the business object.
value.BusinessObject = this;
// If the ObjectMode is any different than None then we set the appropriate property values
if (this.ObjectMode == ObjectModes.Search)
{
value.UseInSearch = true;
}
else if (this.ObjectMode == ObjectModes.Save)
{
value.UseInSave = true;
}
// Now its time to add the field into the PrimaryKeyFields and Fields collections
this.PrimaryKeyFields.Add(value);
this.Fields.Add(value);
}
if (this.IsLoaded)
this.IsDirty = true;
_CustomerID = value;
}
}
- FirstName field is of type NVARCHAR in the database and hence we will use the StringField type. As you will notice that the set accessor method for FirstName is very similar to CustomerID. The AutoGenerated property and the PrimaryKeyFields collection are not needed in fields other than the primary key members. Also, there is a property called MaxLength which is specific to the StringField type, and it is used to set the maximum length of the field in the database. Essentially, this allows the field object to trim any values it gets upto the maximum length specified in the MaxLength property.
private StringField _FirstName = null;
public StringField FirstName
{
get //get accessor method
{
return _FirstName;
}
set //set accessor method
{
if (_FirstName != null)
{
this.Fields.Remove(_FirstName as BaseField);
}
if (value != null)
{
value.Name = "FirstName";
value.AllowNull = false;
value.BusinessObject = this;
value.MaxLength = 200;
if (this.ObjectMode == ObjectModes.Search)
{
value.UseInSearch = true;
}
else if (this.ObjectMode == ObjectModes.Save)
{
value.UseInSave = true;
}
this.Fields.Add(value);
}
if (this.IsLoaded)
this.IsDirty = true;
_FirstName = value;
}
}
- Follow the same process as creating a property for LastName field as well.
- Now lets add a constructor to the class and further customize the business object and initialize its field instances. Each field has a constructor that expects parameters that allow you to customize the field. For example, the code below shows where we are passing the the business object (by using C# "this" keyword and also passing the name of the column in the database.) It is necessary to pass the business object to the field's constructor otherwise you will need to assign the field's BusinessObject property before the class is able to perform any operations on the database.
public Customers_Base() : base()
{
// ResultSet is a DataSet that holds the results returned by any database command as long as the AddResultsToDataSet property is set to true.
this.ResultSet.DataSetName = "Customers";
// The value for TableName must match the table name in the database.
this.TableName = "Customers";
// Create an instance of each Field
this.CustomerID = new Int32Field();
this.FirstName = new StringField();
this.LastName = new StringField();
}
- Now lets take a look at the last step in creating a base class for business logic layer. We create one property and one method each for each relationship that the customer table has with any other table in the database. We don't have to do this for all the relationships, but it is very useful in the long run to create these properties and methods ahead of time. Note: If you use the QuickObjects Designer application this is automatically done for you. The purpose creating a method is to ensure that there is an easy way to ensure that there is an instance in the property for the related object. Also the method ensures that both the main object and the related object share the database connection and the transaction (if being used). The method also creates a new instance of a Join object and adds it to the Joins collection of the main object (i.e. Customers)
using System;
using Akal.QuickObjects.ObjectBase;
namespace QuickObjects.ObjectBase.Samples.BusinessLogicLayer
{
public class CustomersBase : BaseBusinessObject
{
// Internal variable to hold the instance of the related object
private Orders _CustomerID_Orders_Child;
// Property to expose the instance of the related object
public Orders CustomerID_Orders_Child
{
get //get accessor method
{
return _CustomerID_Orders_Child;
}
set //set accessor method
{
_CustomerID_Orders_Child = value;
// if the related object is not null we will share the DBConnection and DBTransaction property, by calling
// the AssignConnection method and passing the related object instance.
if (_CustomerID_Orders_Child != null)
AssignConnection(_CustomerID_Orders_Child);
}
}
// This method can be overloaded as shown further below so that you don't have to even pass any argument
// The purpose of this method is to ensure that there is an instance in the related object property, and also that the
// database connection and transaction properties are shared.
public virtual void Join_CustomerID_Orders_Child(JoinTypes joinType)
{
if (CustomerID_Orders_Child == null)
{
CustomerID_Orders_Child = new Orders();
}
else
{
this.AssignConnection(CustomerID_Orders_Child);
}
// The following method call creates a new instance of a Join and adds it to the Joins collection on the Customers object.
this. CreateJoin("CustomerID_Orders_Child" , this.CustomerID, _CustomerID_Orders_Child.CustomerID, _CustomerID_Orders_Child, joinType, RelationshipTypes .Child);
}
}
}
|
The Naming convention used above is just a sample. You can choose the naming convention that meets your liking or any other coding standards you follow. |
The steps outlined above will have you ready to go with creating your logic with amazing simplicity and speed!

