CSLA 3.6!
Monday, June 22nd, 2009CSLA 3.6 introduces a host of new ways to do the things we used to do, one primary change is the use of LINQ and LINQ to SQL entities as your data layer. This change is pretty cool and the code reduction is even cooler! It is a bit of a mindset change so I seriously recommend reading Rocky Lhotka’s latest book on the framework before diving into it;it’ll make your life easier in the long run.
In this latest version of my CSLA: Step-by-Step guide, we’ll once again go through the process of creating business objects, bind them to Windows and Web UI’s and we’ll even throw in a WPF UI;
So without further ado let’s get started.
Step one: If you haven’t done so already, head over to Apress and buy a copy of Rocky Lhotka’s latest book to get a good understanding of the framework before you start using it.
Step two: Get the latest Codesmith templates from Codeplex. Not sure how to use Codesmith? read this post.
Step three: We’re going to be using a altered version of the Northwind database for this example, so download it here.
Right, so we’ve got our database, we’ have our code-gen templates. Fire up Visual Studio and create a new Windows Solution called NorthwindWPF, add a new Class Library Project, named Northwind.Library, add another Class Library Project named Northwind.DalLinq and finally add a VB.Net Windows Forms Application Project named Northwind.WinUI(Yes, I’m not forgetting my VB brethren)
Your Solution Explorer should look something like this:
Next, expand your Northwind.DalLinq project, and add a new LINQ to SQL Class called Northwind. Next, from the Server Explorer, drag the following tables to the Object Relational Designer window:
- Customers
- Orders
- OrderDetails
- Categories
- Products
The result should look something like this:
Next, add a new Class named Database.cs to your project. In here we’ll store the connection string to the database, use the following code:
1: using System;
2: using System.Collections.Generic;
3: using System.Linq;
4: using System.Text;
5:
6: namespace Northwind.DalLinq
7: {
8: public class Database
9: {
10: public const string Northwind = "Data Source=(local);" +
11: "Initial Catalog=Northwind;Integrated Security=True";
12: }
13: }
That’s all we need for now in the DalLinq project, so expand your Northwind.Library project. Add a Reference to Csla.dll, and add a New Class called Customer to your project.
Next, open Codesmith and double-click on the EditableRoot Template. We’re going to generate an Editable Root object for our Customers table. The properties window should look like this:
You’ll notice a new property called DalNamespace, this tells your object where to look for the LINQ to SQL entities. Right, click Generate and viola! our Customer class is generated. Copy and paste the generated code to your Customer.cs class. Next, change this line :
1: using (var mgr = ContextManager<Northwind.DalLinq.NorthwindDataContext>
2: .GetManager(Northwind.DalLinq.Database.Northwind))
to:
1: using (var mgr = ContextManager<Northwind.DalLinq.NorthwindDataContext>
2: .GetManager(Northwind.DalLinq.Database.Northwind,false))
This tells CSLA not to check in the configuration file for the connection string, but in our Database class.
Right! We’re ready to test our class, add a unit test to the bottom of the Customer Class, it’ll look something like this:
1: [TestFixture]
2: public class Customer_Tests
3: {
4: [Test]
5: public void Load()
6: {
7: Northwind.Library.Customer myCustomer = Northwind.Library.Customer.GetCustomer(1);
8: Console.WriteLine(myCustomer.CompanyName);
9: }
10: }
Run it and if you see Alfreds Futter in the Output window, you know our business object is working! And guess what? We did all this without a single line of SQL! LINQ handles all the queries for us. Pretty cool stuff.
Check back next time, where we’ll look at adding and updating a customer, as well as generating some parent-child objects.