Expert C# Business Objects by Rockford Lhotka - Chapter 2 - Framework Design
----
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.
Chapter 2 talks about the high-level functions and capabilities we want out of the business framework, CSLA.NET, that is going to be built throughout the book.
----
In chapter 2, Rockford begins to discuss the functionality built into the framework design:
- n-Level undo capability
- Tracking broken business rules to determine whether an object is valid
- Tracking whether an object's data has changed (is it "dirty"?)
- Support for strongly-typed collections of child objects
- A simple and abstract model for the UI developer
- Full support for databinding in both Windows Forms and Web Forms
- Saving objects to a database and getting them back again
- Table-driven security
- Other miscellaneous features
Rockford decides to use a "Class in Charge" model for the simple and abstract model for UI development, which is where the business class has static methods to handle the creation of the new class as well as save and get instances:
[Serializable()]
public class Customer
{
private Customer() {...}
private Customer(...) {...}
public static Customer NewCustomer()
{
// Create new customer
// Set default values
// Return Customer Object
}
}
Rockford talks a bit about enabling the new business objects for databinding by supporting the IEditableObject interface and handling property change events as well as the business collections supporting the IBindingList Interface.
Throughout the second half of the book the reader is introduced to all the classes in the CSLA.NET Framework. For business object creation, for example, the following classes are available for which to inherit your own business classes:
- BusinessBase
- BusinessCollectionBase
- ReadOnlyBase
- ReadOnlyCollectionBase
- SafeDataReader
- SmartDate
- NameValueList
If you want to create an editable business object that supports n-level undo, tracking of business rules, object persistence, etc., you just inherit from BusinessBase:
[Serializable()]
public class Customer : BusinessBase
{
}
The chapter goes on to discuss the different classes as well as the high-level functionality in detail. Here is an example on how the broken rules work:
public int Quantity
{
get { return _quantity; }
set
{
_quantity = value;
BrokenRules.Assert("BadQuantity", "Qty must be positive", _quantity < 0);
BrokenRules.Assert("BadQuantity", "Qty can't exceed 100", _quantity > 100);
MarkDirty();
}
}
The chapter ends with discussion on the DataPortal functionality, which is responsible for handling object persistence. I won't go into it in detail, but Rockford focuses on how the DataPortal, and object persistence in general, should work whether the client is running in the same process on the same machine or a different process on a different machine. And, the developer should not have to change any code (only config settings) if the client is moved to a separate tier.
Read Expert C# Business Objects by Rockford Lhotka - Chapter 3 - Key Technologies.