CSLA : Step-by-Step , Getting Started

I've been using CSLA for a couple of months already, and I am really enjoying it. One thing I did notice however is that there is'nt a lot of step-by-step tutorials on how to use CSLA. So with that in mind, here is my first installment of CSLA - Step-by-Step.

Getting Started

The Tools


For this tutorial you are going to need the following:

  • The CSLA Framework.
    • You can download it from Rockford Lhotka's site. For his tutorial we're using version 3.0.1 of the framework. Be sure to read this article if you do not have .Net Framework 3 installed.

  • CodeSmith.
    • CodeSmith 2.6 is freeware. It can be downloaded here.

  • CSLAContrib CodeSmith templates.
  • Visual Basic.Net 2005

  • MyGeneration
  • NUnit
    • We'll need to test our business objects as we go along. We'll use NUnit for our unit testing framework.

  • TestDriven.Net
    • We'll use the free personal edition of TestDriven.Net to run our unit tests from within the VS.Net 2005 IDE.

  • The Northwind Traders sample SQL database
    • We'll be using the Northwind database as an example. It can be downloaded here. If you need the sql create script. Leave a comment with your contact details.

  • Rocky's book, Expert VB 2005 Business Objects.
    • A must read. You can get it at APRESS.
Now, I know this seems like a lot of stuff just to get started, but this tutorial will show you every step to get up and running with CSLA. This tutorial's intention is to have you use CSLA to write a fully functional application using all the free tools

Getting Started

OK, let's jump righ in. Create a new empty Visual Basic project called NorthwindTraders. Next add a new class library to our NorthWindTraders solution called NorthwindTraders.Library, then add a new Windows Application called NorthwindTraders.UI.

available.You can remove the NorthwindTraders project from the solution, so that your Solution Explorer looks something like this:

Add a reference to Csla.dll(Located in the CSLA Framework download) to the NorthwindTraders.Library project.

Let's create our first business object. Add a new class to you NorthwindTraders.Library project and call it Customer.

Open EditableRoot.cst(In the CSLAContrib download) with CodeSmith. The propertygrid should look like this for your Customer object:

Click on the Generate button, and CodeSmith should generate your Customer object. Copy the code and paste it into your Customer class in your NorthwindTraders.Library project.

Save and build your project. You should get the following error :

Name 'Database' is not declared.

Don't worry, It's easy to fix. Add a new module called Database to the project. Add the following code to the module:

Public ReadOnly Property NorthwindConnection() As String
    Get
        Return "Data Source=(local);Initial Catalog=Northwind;Integrated Security=SSPI;"
    End Get
End Property

Re-build the project and the errors should be gone.

Next...let's create the stored procedures to do the data manipulation for out business object. For this I use MyGeneration. Open MyGeneration, in the Template Browser, under Microsoft SQL Server you'll see Script Insert/Update/Delete Procedures for SQL Server. Right click it and select Execute. Select the Customer table and MyGeneration will copy the stored procedure code to the clipboard.

I recommend customizing the template to suit your needs, it'll save you tons of time.

Ok, so you've run the stored procedure create scripts on the Northwind DB, and created the Select stored procedure manually. You'll see what the stored procedure names must be in the ExecuteFetch, ExecuteInsert, ExecuteUpdate and ExecuteDelete Subs in your customer object.

Let's test our business object!

Add a reference to Nunit.framework.dll to your project. Add the following to at the bottom of your customer object :


Namespace UnitTests

    <TestFixture()> _
      Public Class CustomerUnitTests
        Dim CustomerNr As String = ""
        Dim objCustomer As Customer

        <SetUp()> _
        Protected Sub SetUpTest()
            CustomerNr = "ALFKI"
            objCustomer = Customer.GetCustomer(CustomerNr)
        End Sub

        <Test()> _
        Public Sub FetchCustomer()
            Assert.IsTrue(objCustomer.CompanyName = "Alfreds Futterkiste", "Failed to get customer")
        End Sub

         <Test()> _
        Public Sub UpdateCustomer()
            objCustomer.City = "Pretoria"
            objCustomer.Save()
            Assert.IsTrue(objCustomer.City = "Pretoria")
        End Sub

         <Test()> _
        Public Sub CreateCustomer()
            Dim objNewCustomer As Customer
            objNewCustomer = Customer.NewCustomer("PVDW")
            With objNewCustomer
                .CompanyName = "Coalition Software"
                .ContactName = "Pieter van der Westhuizen"
                .ContactTitle = "Big Boss Man"
                .Address = "High Lane 74"
                .City = "Pretoria"
                .Region = "Gauteng"
                .PostalCode = "0081"
                .Country = "South Africa"
                .Phone = "555-2345"
                .Fax = "555-2346"

            End With

            objNewCustomer.Save()
            objCustomer = Customer.GetCustomer("PVDW")
           
Assert.IsTrue(objCustomer.ContactName = "Pieter van der Westhuizen", "Failed to save/get new customer")
        End
Sub

        <Test()> _
       
Public Sub DeleteCustomer()
            Customer.DeleteCustomer("PVDW")
        End
Sub

    End Class

End Namespace 

You can use TestDriven.Net to run these tests or the NUnit GUI. TestDriven.Net is nice and quick, because you can just right-click and select Run Test(s)

If your stored procedures are correct, then the unit tests should work nicely. If not correct the errors and try again.

That's it for the first installment. If you have any comments or questions feel free to leave a comment and I'll be happy to help.

Check back for the second part of CSLA - Step-by-Step, where we'll have a look at Parent-Child relationships.

You can download the code and sql scripts for this post here.

Print | posted on Wednesday, August 29, 2007 10:00 PM

Comments on this post

# re: CSLA : Step-by-Step , Getting Started

Requesting Gravatar...
I know you're probably trying to earn a living, but how about the next step by step on CSLA. The first one was very useful to me. I was having a hard time finding the codesmith templates, and appreciate the link on your website. I wanted to learn something about unit testing, and I didn't know anything about myGeneration.
Left by Phil Raevsky on Sep 22, 2007 9:09 PM

# re: CSLA : Step-by-Step , Getting Started

Requesting Gravatar...
Hey Phil,

Here you go:
http://www.mythicalmanmoth.com/archive/2007/09/24/csla--step-by-step--relationships.aspx

Thanks for your comment, and feel free to give me your thoughts on part 2.
Left by Pieter van der Westhuizen on Sep 24, 2007 8:37 PM

# re: CSLA : Step-by-Step , Getting Started

Requesting Gravatar...
Nice tutorial Phil, i've used csla before but all my work has been manual creation of business objects... yuck!

A quick question before i begin if i may, what generates the sqldatareader code for persisting the BO to the database, MyGeneration or CodeSmith? The reason i ask is im after (for my current project) generator for calling my stored procedures in my database for my SQL 2005 CLR modules.

I will hopefully check out full blown csla generation as soon as i get that out of the way.

Any pointers appreciated
Left by Chris on Oct 09, 2007 9:40 PM

# re: CSLA : Step-by-Step , Getting Started

Requesting Gravatar...
Hi Chris,

It Pieter not Phil :).

CodeSmith generates the code that calls the stored procedure and loads the parameters into the parameters collection. CodeSmith generates a ExecuteInsert sub and InsertParameters sub.

The InsertParameters sub is used to populate the parameters Collection.

You can use MyGeneration to generate the stored procedure that will be called.

If you plan to call a SQL 2005 CLR Module to do the update for you, you would have to code the functions manually.

Hope this helped.
Left by Pieter van der Westhuizen on Oct 10, 2007 8:15 AM

# re: CSLA : Step-by-Step , Getting Started

Requesting Gravatar...
Pieter,
This is a great tutorial. Unfortunately, for me a Newbie, it is all new to me. I have followed along but got lost from:

[Open MyGeneration, in the Template Browser, under Microsoft SQL Server] I can't seem to find the Template Browser. Is it in SQL Server? There is a Template Explorer. But this doesn't have [Script Insert/Update/Delete Procedures for SQL Server.] or do you mean Object Explorer? This would make sense with this: [Right click it and select Execute. Select the Customer table and MyGeneration will copy the stored procedure code to the clipboard.] it has to be that...

Once in the clipboard how does MyGeneration work? I'm not sure now if I downloaded the correct version (mygenV1207.exe)?

I understand what needs to be done but I want to use the code tools to do this. Any extra help with the procedure would be very helpful.

Thanks Pieter :)
Left by ElfstoneUK on Oct 22, 2007 6:08 PM

# re: CSLA : Step-by-Step , Getting Started

Requesting Gravatar...
Hey There,

In MyGeneration, you're suppose to see a Template Browser, if you don't see it, you'll see a button on the toolbar with a folder and a plus sign, clicking it will show you the Template Browser.

Once you see the Template Browser, you'll see there is a Microsoft SQL Server folder, clicking on the plus sign next to it will show you a list of stuff, one of which will be Script Insert/Update/Delete Procedures for SQL Server.

Right, after you've right clicked and selected execute, it ask you for the database and tables. It will then copy the SQL create code to your clipboard. Another way is to double click on the template and clicking on the little green "play" button. It will then generate the SQL create scripts in the output tab.

Once the code is in your clipboard, create a new query in sql server management studio, and paste the code. Execute the script on your database. It should create all the stored procs you need.

Hope this helped!
Left by Pieter van der Westhuizen on Oct 22, 2007 7:06 PM

# re: CSLA : Step-by-Step , Getting Started

Requesting Gravatar...
Pieter thanks,
I had worked out most of this already but thanks anyway. My problem turned out to be with SQLExpress and SQL Server relating to the same Northwind.mdf - don't ask!! ;(

However, the extra info on using MyGeneration was welcome. I shall move onto Part 2 today.

Keep up the good work.

Cheers
Left by ElfstoneUK on Oct 23, 2007 4:20 PM

# re: CSLA : Step-by-Step , Getting Started

Requesting Gravatar...
oh yeah, just one other thing, for those of us who have never used NUnit or TestDriven.
Where/on what exactly do you right click and select Run Test(s)?
Depending on where/what I click I get pass or 4 failed.

Thanks Pieter :)
Left by ElfstoneUK on Oct 23, 2007 4:27 PM

# re: CSLA : Step-by-Step , Getting Started

Requesting Gravatar...
Howdy,

You click in the test sub you created, and select run Test(s). This should only run the one test.

If you click in the class, but outside a Test()> Sub you'll run all the tests in that class.

Left by Pieter van der Westhuizen on Oct 23, 2007 6:07 PM

# re: CSLA : Step-by-Step , Getting Started

Requesting Gravatar...
Is there a reason you use MyGeneration for the sprocs instead of using the StoredProcedures.cst template that comes with CodeSmith? That template will generate the select statement too and then you only need 1 tool.
Left by Jason on Nov 30, 2007 11:20 PM

# re: CSLA : Step-by-Step , Getting Started

Requesting Gravatar...
Has anyone used CSLA with a meta data driven system. Generating forms from meta data that specifies that html control types to use and other attributes?

I am looking to build an application that is highly customizable for each customer and need to be able to create custom tables and then store meta data with the table column data type, length, null, read-only etc. information and then generate editable web pages from the meta data. Some user groups will see these as read only others editable etc.

Chris
Left by Chris Tillotson on Mar 25, 2008 7:57 AM

# re: CSLA : Step-by-Step , Getting Started

Requesting Gravatar...
Hi Pieter,

Your link to download code and sql scripts is broken.
Left by Dominique on Jun 19, 2008 9:55 PM

Your comment:

 (will show your gravatar)
 
Please add 2 and 4 and type the answer here: