CSLA : Step-by-Step , Relationships
Monday, September 24th, 2007Due to popular demand…ok…just Phil
… we’re back with the second installment of CSLA: Step-by-Step.
In the previous tutorial, we took a look at how to setup the various tools and templates to easily generate your first CSLA business object.
In this tutorial we’ll dive a little deeper into parent-child objects, and which CSLA object to use where.
The Relationship
-
Order (BusinessBase)
-
OrderDetail (BusinessBase)
-
OrderDetails (BusinessListBase)
Private _orderdetails As OrderDetails = OrderDetails.NewOrderDetails()
‘todo: If parent use identity key, fix fk member with value from parent
cm.Parameters.AddWithValue("@OrderID", _orderID)
cm.Parameters.AddWithValue("@OrderID", parent.OrderID)
OK,so next we need to generate the OrderDetails class, that the compiler complained about. Using CodeSmith again open the EditableChildList.cst template and change the property grid to look like this:
Allrighty. Now, we’ve got our Order Object and the Order Details collection(of Order Detail). We also have the stored procedures for the Order object, BUT just how will we get the Order Details? You’ll notice that in the Order Detail’s fetch method there is nothing about which stored proc to use. Not to worry.
dr.NextResult()
_orderdetails = OrderDetails.GetOrderDetails(dr)
SELECT *
FROM [Orders] WITH (NOLOCK)
WHERE
[OrderID] = @OrderID
SELECT *
FROM [Order Details] WITH (NOLOCK)
WHERE
[OrderID] = @OrderID
Ok, so let’s recap. We’ve created a parent object named Order, a child object called OrderDetail and a collection of OrderDetail, called OrderDetails. We’ve changed the parent Order object’s stored procedure to also select the related child objects. And we’re ready to test our code.
Just as in part 1, let’s add the unit test code:
Namespace UnitTests
Public Class OrderUnitTests
Dim OrderID As Integer
Dim objOrder As Order
Protected Sub SetUpTest()
OrderID = 10248
objOrder = Order.GetOrder(OrderID)
End Sub
Public Sub FetchOrderChildren()
Assert.IsTrue(objOrder.Orderdetails.Count = 3, "Failed to get customer")
End Sub
End Class
End Namespace
_discount = dr.GetDecimal("Discount")
_discount = dr.Item("Discount")
That concludes Part 2, I hope it was useful, please feel free to give your comments. In part 3, we’ll look at troubleshooting your objects and frequent errors as well as some more object integration.
Thanks for reading, I’ll chat to you soon.