The Business Logic Layer (BLL) sits between the User
Interface and the Data Access Layer and implements the business logic of the
application. This is where validation rules should be implemented.
1.
Right click on the OrderSystem solution in the Solution Explorer and
select Addà New Project… from the
pop-up menu.
2.
Select Visual C# Class Library and name the project OrderSystemBLL and
click the OK button.
3.
Right click on the References in the BLL project and select Add
Reference. The Add Reference dialog box should appear.
4.
Click on the Projects tab and select OrderSystemDAL from the list of
projects. Click the OK button. You can now use the classes declared in the
DAL project
5.
You also need to add a reference to the System.Data.Entity namespace.
6.
Right click on the Class1.cs file in the Solution Explorer and select
Rename from the pop-up menu. Rename the file UserAccountEO. The EO is a
suffix which stands for Edit Object. This distinguishes this class from the
UserAccount entity that was declared in the DAL. Visual Studio will prompt you
to rename all reference to this class, you should click the Yes button.
7.
Add the following using statements.
using System.Collections;
using OrderSystemDAL;
8.
Add the following property declarations to the class.
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime InsertDate { get; set; }
public DateTime UpdateDate { get; set; }
public string FullName
{
get
{
return LastName + ", " + FirstName;
}
}
The FullName property is a read only property that is useful
when displaying this object to the user.
9.
Add the follow methods to implement the logic for saving a record to the
database. The Save method will either add or update a record depending of the
Id of the object. A zero Id indicates that the record should be added. The
data in this object is validated before it tries to save it to the database.
If there are any validation errors the record will not be saved and the
specific validation error message will be passed back to the caller.
public bool Save(ref ArrayList validationErrors)
{
ValidateSave(ref validationErrors);
if (validationErrors.Count == 0)
{
if (Id == 0)
{
Id = UserAccountsData.Insert(FirstName, LastName, DateTime.Now);
}
else
{
UserAccountsData.Update(Id, FirstName, LastName, DateTime.Now);
}
return true;
}
else
{
return false;
}
}
private void ValidateSave(ref ArrayList validationErrors)
{
if (FirstName.Trim() == "")
{
validationErrors.Add("The First Name is required.");
}
if (LastName.Trim() == "")
{
validationErrors.Add("The Last Name is required.");
}
}
10. Now
you can add the methods for deleting a record. If you needed to do any
referential integrity checks or other validation before deleting you would add
the logic in the ValidateDelete method.
public void Delete(ref ArrayList validationErrors)
{
ValidateDelete(ref validationErrors);
if (validationErrors.Count == 0)
{
UserAccountsData.Delete(Id);
}
}
private void ValidateDelete(ref ArrayList validationErrors)
{
//Check for referential integrity.
}
11. The
next methods to add will allow a caller of this object to load a specific
record.
public bool Select(int id)
{
UserAccount userAccount = UserAccountsData.SelectById(id);
if (userAccount != null)
{
MapData(userAccount);
return true;
}
else
{
return false;
}
}
internal void MapData(UserAccount userAccount)
{
Id = userAccount.Id;
FirstName = userAccount.FirstName;
LastName = userAccount.LastName;
InsertDate = userAccount.AuditFields_InsertDate;
UpdateDate = userAccount.AuditFields_UpdateDate;
}
12. The
last method to add will create a list of UserAccountEO objects. This is used
when you want to select all the records from the UserAccounts table.
public static List<UserAccountEO> SelectAll()
{
List<UserAccountEO> userAccounts = new List<UserAccountEO>();
List<UserAccount> userAccountDTOs = UserAccountsData.SelectAll();
foreach (UserAccount userAccountDTO in userAccountDTOs)
{
UserAccountEO userAccountEO = new UserAccountEO();
userAccountEO.MapData(userAccountDTO);
userAccounts.Add(userAccountEO);
}
return userAccounts;
}