Programming Within the CSLA
- Understanding CSLA and the Factory Pattern
- Working with the DataPortal
- Key Features of CSLA Features
- The Pros and Cons of CSLA
- Conclusion
- Further Reading
An architecture that can provide fully functional, data-aware objects that behave consistently regardless of network infrastructure and distributed environment is one of the ongoing dreams of programmers and application architects working in client/server settings. In many ways this dream has come true with the introduction of Component-based Scalable Logical Architecture (CSLA).
The CSLA framework created by Rockford (Rocky) Lhotka provides a business object architecture in which any object can be created and stored from anywhere, anytime—regardless of whether the consuming application is Windows Forms–based or web browser–based. In addition to consistent client behavior, CSLA works transparently with regard to back-end data access, whether the network protocol is based on web services, .NET Remoting, or .NET Enterprise Services. Lastly, the framework provides built-in functionality that alleviates many of the common chores of fundamental enterprise programming. For example, CSLA gives you an easy way to implement roles-based read/write access permissions to an object's property; to apply business rules validation; and to save data within a hierarchical parent-child relationship by saving the parent only.
This article shows you the conceptual basics of CSLA and illustrates them with some C# code examples. CSLA uses the factory design pattern extensively, so you'll come away with a firm operational understanding of the pattern. Finally, we'll discuss some of the strengths and weaknesses of CSLA.
We assume that you have at least intermediate knowledge of C# and have created distributed applications. If you've created a web-based .NET application using ADO.NET, you're in good shape. You should be familiar with object-oriented programming concepts such as abstract classes, method overriding, and the use of static fields and methods.
Consider the fundamental principle of CSLA—it is a framework for distributed programming that provides a set of base classes from which you derive business classes that are relevant to the needs of your application domain. These base classes allow derived objects to be constructed from anywhere within the enterprise network, and the mechanisms to get and save data from anywhere within the enterprise network. To get a clear, high-level appreciation of CSLA, you need to understand two core concepts. The first is the way that CSLA implements the factory design pattern. The second is the nature and use of the CSLA DataPortal.
Understanding CSLA and the Factory Pattern
CSLA uses the factory pattern to allow the framework to create very powerful business objects, so that much of an object's construction is hidden from the application programmer using the business object. With the factory pattern, you never new an object directly. Instead, you ask a factory for the object that you want and it is provided by way of a calling method in the factory object.
Mr. Lhotka uses a variation of the factory pattern in CSLA called class-in-charge. In this model, the object of interest itself is a factory, but constructed so that the factory's object constructors are declared private, and creation methods are declared as static and provide an instance of the object itself. Listing 1 and Figure 1 illustrate the concept behind the class-in-charge variation of the factory design pattern.
The code in Listing 1 is from the Music Catalog sample project that we created for this article (CogArtTechMusic.zip). This sample project shows you how CSLA uses the factory pattern to construct objects that know how to return instances of themselves. In addition, the sample code demonstrates the internals of CSLA data management. For the project code, along with installation notes and instructions, download the source file.
Listing 1 CSLA use of the factory pattern
/***************************************** * Get an instance of a Catalog object by * calling the static GetCatalog(string countryOfBand) * creation method. NOTE: The Catalog object contains a * Bands property, which is a collection of Band objects. ******************************************/ Catalog catalog = Catalog.GetCatalog("USA"); BandsList bands = catalog.Bands;
Figure 1 The factory design pattern allows you to provide a high degree of construction functionality, yet present a very simple interface to those using your CSLA business objects.