Expert C# Business Objects by Rockford Lhotka - Chapter 4 - Business Framework Implementation
----
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.
Chapter 4 walks the reader through the creation of many of the business classes in CSLA.NET.
----
Chapter 4 uses the business concepts and key technologies discussed in chapters 1 - 3 to build many of the business classes in CSLA.NET:
- Core.BindableBase
- Core.BindableCollectionBase
- NotUndoableAttribute
- Core.UndoableBase
- BusinessBase
- BrokenRules
- RulesCollection
- Rule
- BusinessCollectionBase
- ReadOnlyBase
- ReadOnlyCollectionBase
- SmartDate
This is one of those chapters that one really needs to read a couple of times and study to get real value. It is not too advanced, but just a lot of technology and concepts thrown at the reader in one chapter. The chapter talks a lot about the real world issues of databinding, tracking business rules, and managing object statutes (IsNew, IsDirty, IsDeleted) as well as the author's desire to support n-level undo. Throughout the chapter you actually build the classes mentioned above in Visual Studio.
The beginning of the chapter focuses mainly on the n-level undo functionality, which is a good education in the power of Reflection and Binary Serialization. The Core.UndoableBase class essentially has 3 methods: CopyState(), UndoChanges(), and AcceptChanges(). CopyState() reads all private and public instance variables of an object, serializes them in binary fashion, and then pushes the snapshot on a stack. UndoChanges() pops the snapshot off the stack and does just the opposite of CopyState(). AcceptChanges() just pops the stack.
Rockford then begins to focus on implementing the proper interfaces to support windows databinding, in particular, IEditableObject, which needs BeginEdit(), CancelEdit(), and ApplyEdit() methods. He also discusses Object Status Tracking, which is essentially making sure objects are properly marked as New, Dirty, or Deleted so that the Save() method on an object knows whether to do an Insert, Update, or Delete in SQL on the object.
Next the chapter constructs the Rule and RulesCollection classes. The rules are used by the DataPortal to decide if an object is valid. If the object is valid (no broken rules in the RulesCollection), the object is saved, otherwise it is not. The RulesCollection is also available to the UI developer to display errors. The rules are not enforced, which means the business onbject could be invalid unless the developer throws the proper exceptions. The rules framework only tracks broken rules and doesn't enforce them.
There are some other goodies throughout the chapter dealing with handling dates, strongly-typed collections, and null values. There is also a lot of good OOP concepts being thrown around to assure the developer cannot directly mess with certain areas of the business framework. A lot of good use of Internal and Protected Internal access modifiers as well as virtual methods, operator overloading, events, etc.
Again, the chapter was excellent, but it is not an easy read and requires a bit more studying to get the full educational impact. Before I continue with the next chapter, which starts to focus more on Data Access and Security, I am going to download the CSLA.NET source code ( CSLA .NET CS version 1.4 ) as well as read some other resources. I also want to build a business object on top of the CSLA.NET framework and see it work for myself. More on this in a couple of days.
Now on to Expert C# Business Objects, Chapter 5, Data Access and Security.