日期:2012-07-26  浏览次数:20389 次

  • Download source files - 22 Kb
Note: after downloading be sure to consult ReadMe.txt first.

Introduction

This sample is an introduction to Rockford Lhotka's Component-based Scalable Logical Architecture (CSLA) as described in his book, Visual Basic 6 Business Objects (Wrox Press, 1998). The CSLA is an implementation of n-tier design, where an application is logically partitioned into four layers - presentation, UI-centric business objects, data-centric business objects, and data services. The purpose of the architecture is to achieve two overarching objectives.
  1. Scalability.
  2. Flexibility in the physical placement of each layer.
The latter means that we can decide whether to place all the layers on a single machine or to distribute them between client and server machines with little or no impact on the business logic code. It also means that we can provide a variety of presentation layers, ranging from a traditional Windows interface to a Web interface, with little or no impact on the business logic code. A typical physical architecture, utilizing a rich Windows-style user interface, is to place the UI- centric objects on a client workstation where they are close to the user interface. The UI- centric objects communicate with the data-centric objects, which are placed on an application server, and the latter communicate with the data services, which are on a database server. The code presented here in C# is not a full implementation of the architecture. Rather, it serves to illustrate the basic ideas. It presents a single UI-centric business object that captures basic information about a person - social security number, name and date of birth. It shows how a Microsoft .Net Windows Forms user interface can interact with the business object while allowing the latter to validate all the business rules as the user enters data. The user interface is completely insulated from detailed business rule validation. It just needs to act on a property of the business object - whether it is currently in a valid or invalid state. UML Class Diagram

The Person Class

The Person class has three supplied properties:
  • Social Security Number
  • Name
  • Birth Date
and one computed property:
  • Age
It has three business rules.
  • Social Security Number contains exactly 11 alphanumeric characters (though the sample asks the user for digits).
  • Name must be non-empty.
  • Birth Date must be in a valid format.
A Person object is not considered to be in a valid state until these rules are satisfied. In order to provide the richest and most responsive user experience the idea is to validate these rules while the user is entering their data, rather than after it has all been entered. Other user interfaces, such as an HTML 3.2 interface, can make do with a "batch"-style interface but we want to be able to use a variety of user interfaces and a Windows-style GUI provides the richest. The basic idea is that the controls for saving the user's data should only become available when the user has supplied all the data necessary to make a Person valid. It is the user interface developer's job to write the code to do this. But they don't need to write detailed validation code they need only query the Person's "Valid" property. However, in practice, the UI developer has to know more about a Person than this. But what they have to know is not to do with business rules but with the operational state of a Person at any time. Let's consider the operations that can be performed on a Person:
Add a new Person. Save it to a database. Load an existing Person from the database. Delete a Person.
A rich user interface will have features that enable a user to:
Edit their data, save their changes and close the application. Edit their data, save their changes, do further editing, save