Expert C# Business Objects by Rockford Lhotka - Chapter 3 - Key Technologies
----
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.
Chapter 3 describes the key technologies used in the creation of the CSLA.NET Framework.
----
This chapter is extremely good from a technical perspective as it describes the following technologies and how they are used in CSLA.NET:
- Remoting
- Serialization
- Enterprise Services (COM+)
- Reflection
- .NET role-based security
- ADO.NET
In my opinion, Rockford sums up the usefulness and practicality of these technologies very well in a single chapter. I must have read the chapter three times, and in my opinion, this chapter alone is well worth the price of the book. Although I cannot do the chapter justice, I wanted to briefly makes some points about Remoting, Serialization, and Reflection, which I found particularly interesting.
Remoting
As I mentioned before, "remoting is all about allowing code running in one process to communicate with code running in a different process - even if that different process is on another machine across the network." - Rockford Lhotka
The book discusses object proxies, formatters (binary and SOAP/XML), and channels (HTTP and TCP) used by the remoting subsystem to allow client-server communications. Binary formatters were chosen in the CSLA.NET framework because they provide a deep copy of objects and the binary blob being sent over the channel is 30% the size of payloads being sent by the SOAP/XML formatter. The HTTP Channel is used as opposed to the TCP channel because it works best through firewalls and is easier to implement as it comes with IIS.
Rockford actually takes you through the building of a sample remoting project step-by-step. Very cool indeed.
Serialization
Serialization is used to convert a complex object type into a single byte stream. Remoting and Web Services use different types of serialization. Remoting uses a type of serialization that finds all the variables within an object and converts their values into a byte stream. To suppress serialization of some variables, you would need to use the [NonSerialized()] Attribute.
Web Services uses the XMLSerializer object, which only grabs public fields and public read-write properties only. Web services will not serialize private fields, read only properties, or write only properties. In short, The XMLSerializer does *not* make a clone of the object.
Rockford provides a useful code snippet, which is used in the CLSA.NET Framework, for manually invoking serialization for cloning objects using the binary formatter. The following method would be placed in an object class-
public object Clone()
{
IO.MemoryStream buffer = new IO.MemoryStream();
Runtime.Serialization.Formatters.Binary.BinaryFormatter formatter = Runtime.Serialization.Formatters.Binary.BinaryFormatter();
formatter.Serialize(buffer, this);
buffer.Position = 0;
return formatter.Deserialize(buffer);
}
Reflection
The CLSA.NET framework uses Reflection quite a bit, which is especially important in the DataPortal. The DataPortal needs to initiate business objects directly even though the business objects have private constructors. This is possible via reflection as long as the class has a parameterless private constructor.
Using reflection, one can get class and object information (fields, properties, methods, etc.) as well as set private and public variables and invoke methods on objects.
You can get type information on a class:
Type typeData = typeof(string);
Type typeData = typeof(Customer);
or on an object:
Type typeData = objCustomer.GetType();
Using the type information, for example, you can then get all private instance variables and their values:
FieldInfo[] fields = typeData.GetFields(BindingFlags.Instance | BindingFlags.NonPublic);
foreach (FieldInfo field in fields)
{
System.Diagnostics.Debug.WriteLine(field.Name + ": " + field.GetValue(cust).ToString();
}
Conclusion
This chapter was really cool. A good practical overview on the power of these key technologies. Expert C# Business Objects by Rockford Lhotka Chapter 4 Business Framework Implementation goes into further detail on how these key technologies are applied in CLSA.NET.