How to use CslaGen to generate CSLA data access layer code

From Cslagen

Jump to: navigation, search

Contents

Introduction

CSLA is a free 3-tier framework for Framework 2.0 and 3.0. The Apress book "Expert C# 2005 Business Objects, Second Edition" by Rockford Lhotka documents CSLA for C# and there is also a VB 2005 edition. This is not a closed project, but rather a living and evolving one. It is supported by a dynamic and helpfull commmunity you can find at http://forums.lhotka.net/forums/default.aspx. You can get the CSLA framework source files (C# and VB) at http://www.lhotka.net/cslanet/download.aspx (I use the 2.1.4 version).

CslaGen is a code generator that takes care of generating the data access layer files needed for CSLA Framework. There are templates for generating SQL Server stored procedures, as well as C# or VB. It also supports ActiveObjects extension to CSLA that implements the Observer pattern. At the time of writing the main contributor is Andrés Villanueva (aka xal). The Google group is at http://groups.google.com/group/CslaGenerator. You can get the latest version at http://groups.google.com/group/CslaGenerator/browse_thread/thread/71dfc54f2267c629 and the latest version of C# templates can be found at http://groups.google.com/group/CslaGenerator/browse_thread/thread/0d7da7d7e6d7ea4b

While CSLA is very well documented, CslaGen contributors lack the time to document it. This project is just an example to illustrate some very basic ways to use CslaGen.

Background

Rocky Lhotka doesn't subscribe to the datacentric concept of objects but uses the behavioral concept. Objects are defined by their responsibilities. Thus you should use different objects on selection listings and when editing the object itself.

The database

The database Family is composed of the Mother and the Daughter tables. The later references the former as shown in Fig. 1

Image:TFLCslaGen1.gif

Fig. 1 - Database Family diagram

Under SQL Server create a database Family. The project file TFLCslaGen.zip includes queries to create the database tables and populate it with some data.

The project structure

We will assume that both Mother and Daughter objects are big objects, with lots of properties. Loading a collection of these objects is very expensive and we will load a abcList collection of lighter abcInfo objects (just names and enough information to load the selected object). Note that abcList and abcInfo are read only objects. As you can see in Fig. 2, the MotherList is a root collection of MotherInfo childs that allow us to select one of the Mothers and load the full object. The Mother object is a root object that is editable. It loads DaughterList, a child collection of DaughterInfo grand-childs. After selecting a DaughterInfo grand-childs, our would be application would load the full Daughter object, in order to edit it. Image:TFLCslaGen2.gif

Fig. 2 - Family objects diagram


Download the project file

Download the project file that includes:

- CslaGen project file

- SQL Server queries to create the database tables and populate them

- C# source files to build the Family tree view

Create root level selection objects

Responsability - list and select root objects

On this step we will create list objects in order to list and select the root object to edit. You can skip it if your application doesn't need to list root objects. Loading a collection of complete root objects (with all their properties and childs) may be a lenghty process. That's why we prefer to use just a subset of the properties of the root object, and to ignore the child objects. If you prefer to list complete root objects:

- on step 2. just right click and Select All;

- on step 3. create a Editable Root Collection instead of Read Only Collection.

1. On Schema pane, select the database table Mother.

2. On Columns pane, select the database columns ID, Forename, Surname and LastChanged.

3. Right click and select Create -> Read Only Collection. Note that you can't add or delete items on a read only collection.

4. On the New Object Defaults window, set CollectionName = "MotherList" and ItemName = "MotherInfo".

5. On the MotherList create a criteria to get all the items of the collection.

5.1. On the Csla Objects pane select MotherList.

5.2. On the Csla Object Info pane, under 03. Criteria edit the Criteria Objects collection by clicking on the elipsis.

5.3. On the Criteria Collection Editor window, add a member named AllCollection (or whatever name you prefer).

5.4. Under Misc unfold GetOptions and set DataPortal, Factory and Procedure to True (note the ProcedureName field changes automatically to GetMotherList ) and click OK.

6. Now create another criteria to get a list of Mother by forename and/or surname.

6.1. Repeat steps 5.1 to 5.4. but use instead the member name Name.

6.2. Change the ProcedureName field to GetMotherListByName.

6.3. Don't click OK right now but instead edit the Properties collection under Properties by clicking on the elipsis.

6.4. Add a member. Drag the CriteriaProperty Collection Editor window to the upper part of the screen and click on the DbBindColumn field.

6.5. On the Schema Objects window, select the column Forename of the table Mother. Now click on the member panel and the member name will change automatically.

6.6. Repeat the later operation for the column Surname. The Stored Procedure that CslaGen generates will combine both names. Say you specify Mar as the forename and an empty string as the surname. You will retrieve Mary Poppins, Mariah Carey, and other Mar%.

Create the root object "Mother"

Responsability - edit Mother details

1. On Schema pane, select the database table Mother, right click it and chose Create Editable Root. A new object named Mother is created that includes all the columns of this table.

2. Under 09. System.Object Overrides change the ToString Property by clicking on the down arrow, select Forename and Surname and press ENTER.


Create child level selection objects

Responsability - list and select childs

We assume that loading a collection of complete child objects can be a lenghty process. Thus we prefer to use a collection of subsets of Daughter. After selecting what Daughter to use, we will load the complete object. Of course there are other possible strategies to handle large child objects. After understanding this example, you will find easy to adapt it to load a collection of complete child objects.

1. On Schema pane, select the database table Daughter.

2. On Columns pane, select the database columns DaughterID, Forename, Surname, MotherID and LastChanged.

3. Right click and select Create -> Read Only Collection. Note that you can't add or delete items on a read only collection.

4. On the New Object Defaults window, set CollectionName = "DaughterList" and ItemName = "DaughterInfo".

5. On the Csla Object Info pane of DaughterList, under 04. Child Object Options change the Parent Type by clicking on the down arrow, select Mother object and press ENTER.

6. Now we must link the Mother object to this read only collection so it knows it has to load the collection.

6.1. On the Csla Object Info pane of Mother, under edit the Child Collection Properties collection under 02. Business Properties by clicking on the elipsis.

6.2. On the ChildProperty Collection Editor window, add a member and under Definition set the Name, ParameterName and TypeName to DaughterList. Now click OK.


Create the root object "Daughter"

Responsability - edit Daughter details

Child objects are never load directly; their parent takes care of it. The parent of Daughter is Mother but instead of loading a collection of complete Daughter objects, we assumed this could be a lenghty process and loaded a collection of subsets of Daughter.

1. On Schema pane, select the database table Mother, right click it and chose Create Editable Root. A new object named Mother is created that includes all the columns of this table.

2. Under 09. System.Object Overrides change the ToString Property by clicking on the down arrow, select Forename and Surname and press ENTER.


The example program

The example program is a very simple one. It fills a tree view with mothers and daughters (although some mothers don't have any daughters at all). The date and place of birth only show up on the tooltip. There are no provisions for editing data.

Image:TFLCslaGen3.gif

Fig. 3 - Family program


1. Download the project file and unzip it. The file FamilyGen.xml is the resulting file of the CslaGen project.

2. On your SQL 2000 or SQL 2005 create a database Family and run the scripts in SQL scripts directory. If something goes wrong, delete the Mother and Daughter tables and rerun the scripts following the appropriate order.

3. On VS 2005 open the Family solution under the CS source directory. Firts of all you must:

- correct the CSLA reference;

- edit App.config and correct the connection string.

Now you can compile and run the solution.

Appendix - C# code to load the Family TreeView

private void formFamily_Load(object sender, EventArgs e)
{
    // Load the collectin of all light MotherInfo objects
    MotherList motherList = MotherList.GetMotherList();
    foreach (MotherInfo motherInfo in motherList)
    {
        // Create the root node
        TreeNode node =
            treeViewRelations.Nodes.Add(motherInfo.Forename.Trim() + " " + motherInfo.Surname.Trim());

        // Get the complete Mother object (along with the DaugtherInfo collection)
        Mother mother = Mother.GetMother(motherInfo.ID, motherInfo.LastChanged);

        // Display mother birth date and place on the tooltip
        node.ToolTipText = mother.BirthDate + " " + mother.BirthPlace;
        
        foreach (DaughterInfo daughterInfo in mother.DaughterList)
        {
            // Create a daughter node
            TreeNode subNode =
                node.Nodes.Add(daughterInfo.Forename.Trim() + " " + daughterInfo.Surname.Trim());

            // Get the complete Daughter object
            // NB - From CSLA point of view, the Daughter object is a root object (as it loads itself)
            Daughter daughter = Daughter.GetDaughter(daughterInfo.DaughterID, daughterInfo.LastChanged);

            // display daugther birth date and place on the tooltip
            subNode.ToolTipText = daughter.BirthDate + " " + daughter.BirthPlace;
        }
    }
}

Tiago Freitas Leal


Personal tools

sl
דומיין בעברית  דומיין  דומין  תוכנה לניהול  קשרי לקוחות  CRM, ניהול קשרי לקוחות  דומין בעברית  פורומים  ספרדית  גיבוי