Skip to main content

Command Palette

Search for a command to run...

CSLA : Step-by-Step , Getting Started

Updated
4 min read
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:

[solutionexplorer.gif]

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:

[CustomerProperties.gif]

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

_
Public Class CustomerUnitTests
Dim CustomerNr As String = ""
Dim objCustomer As Customer

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

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

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

_
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

_
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.

More from this blog

M

Mythical Man Moth

80 posts

Hi, I’m Pieter van der Westhuizen. I'm a professional freelance web & mobile developer from South Africa that has been code slinging for more than 23 years. https://youtube.com/shorts/aCCKAnDNrzM