Expert C# Business Objects by Rockford Lhotka - Chapter 5 - Data Access and Security
----
I will be providing a chapter by chapter summary of Expert C# Business Objects by Rockford Lhotka (Amazon) as well as any thoughts and commentary when appropriate. Enjoy.
Read Expert C# Business Objects Chapter 1.
Read Expert C# Business Objects Chapter 2.
Read Expert C# Business Objects Chapter 3.
Read Expert C# Business Objects Chapter 4.
Chapter 5 discusses object persistence of business objects and the creation of tables and stored procedures. This chapter is fairly technical, so the information here will be spotty at best.
----
Chapter 5 creates the following components in CSLA.NET:
- CSLA.Server.DataPortal.dll
- CSLA.Server.ServicedDataPortal.dll
which are responsible for persisting business objects in your business framework.
All business objects in you application will derive from one of four base classes:
- BusinessBase
- BusinessCollectionBase
- ReadOnlyBase
- ReadOnlyCollectionBase
For these objects to support persistence, 5 virtual methods are added to each of these classes that must be overriden in your business classes:
- DataPortal_Create()
- DataPortal_Fetch()
- DataPortal_Update()
- DataPortal_Delete()
- Save()
For example:
virtual protected void DataPortal_Create(object criteria)
{
throw new NotSupportedException("Invalid operation - create no allowed");
}
These methods are not called directly by the object itself, but by the DataPortal Server object using reflection.
For example, let's consider the following business object.
[Serializable()]
public class Employee : BusinessBase
{
...
[Serializable()]
public class Critieria
{
public string SSN;
public Criteria(string SSN)
{
this.SSN = SSN;
}
}
...
override protected void DataPortal_Create(object criteria)
{
// Custom code needed to create object...
// Custom SQL Code...
}
}
During object creation an instance of the object's criteria class is passed to the DataPortal Server's create method:
static public object DataPortalServer.Create(object criteria, object principal)
{
... GetMethod(criteria.GetType().DeclaringType, "DataPortal_Create") ...
}
Using the criteria object, the portal can get the business object's "DataPortal_Create" method using reflection by using the DeclaringType property. This method can then be invoked by the DataPortal to create the object. The other methods work in a similar fashion.
The rest of the chapter talks about database security and building utility classes, like SafeDataReader that automatically handles null values so you don't have to always write code like-
If (dr.IsDBNull(0))
{
// Handle null value appropriately
}
else
{
// Handle regular value appropriately
}
SafeDataReader automatically handles null appropriately.
There is some pretty cool stuff in this chapter. And, even if you don't want to use CSLA.NET in your projects, there are some good utility classes and best practices in CSLA.NET that can be of use in any project.
The next chapter, Expert C# Business Objects Chapter 6 - Object-Oriented Application Design, takes you through creating a project to use CSLA.NET.