Reputation: 53
I have reading Pro Business Application with Silverlight 4 by Chris Anderson, Regarding the topic EXPOSING DATA FROM THE SERVER: USING WCF RIA SERVICES - Presentation Model Types, it is not so details about a presentation model object contains a property exposing a collection of another presentation model object.
I have the basic idea of how to create a presentationModel, but how about a PresentationModel contains a collection of another PresentationModel? Example a ProductPM contains a collection of ProductInventoryPM? How to write the CRUD code at the Domain Service Class?
Thank You!
Upvotes: 1
Views: 436
Reputation: 53
Thank you so much, it is working fine.
And I have modified the Insert domain operation for the ProductInventoryPM object as below so a new ProductInventory record can be add into an existing Product.
Public Sub InsertProductInventory(ByVal ProductInventoryPM As ProductInventoryPM)
If ProductInventoryPM.ProductID <> 0 Then
Dim productInventory As New ProductInventory
With productInventory .Bin = 1
.LocationID = 1
.ModifiedDate = DateTime.Now
.ProductID = ProductInventoryPM.ProductID
.Quantity = ProductInventoryPM.Quantity
.Shelf = "A"
End WithProduct.ProductInventories.Add(productInventory)
Me.ChangeSet.Associate(ProductInventoryPM, productInventory, AddressOf UpdateProductInventoryKey)End If
End Sub
Upvotes: 0
Reputation: 8369
There's several things you need to do in your code. First, to be able to see the list of product inventory records, you need to select them as part of your GetProducts domain operation:
Public Function GetProducts() As IQueryable(Of ProductPM)
Return From p As Product In Me.ObjectContext.Products.Include("ProductInventories") Select New ProductPM With
{.ListPrice = p.ListPrice,
.ModifiedDate = p.ModifiedDate,
.Name = p.Name,
.ProductID = p.ProductID,
.ProductNumber = p.ProductNumber,
.ProductInventory = From i In p.ProductInventories Select New ProductInventoryPM With
{
.Quantity = i.Quantity,
.ProductID = p.ProductID
}
}
End Function
That will get them showing in the second data grid.
You'll also need to add a constructor to your ProductPM class that initialises the ProductInventory class:
Public Sub New()
ProductInventory = New List(Of ProductInventoryPM)
End Sub
The final issue is that the insert domain operations are being called in the wrong order. As far as I can tell, the best way to handle this is as follows:
Create an empty Insert domain operation for the ProductInventoryPM object. No logic is required in it, but the method is required in order for the ProductInventoryPM object to be able to be added to the ProductPM's inventory collection.
Public Sub InsertProductInventory(ByVal ProductInventoryPM As ProductInventoryPM)
End Sub
NOTE: If you want to be able to add new inventory records after you've created the product + initial inventory record, then you will need to add logic to this domain operation after all.
Insert the inventory objects in the ProductPM's Insert domain operation. An advantage of this is that there's no need to call SaveChanges.
Public Sub InsertProduct(ByVal ProductPM As ProductPM)
Dim Product As New Product
Product.Name = ProductPM.Name
Product.ProductNumber = ProductPM.ProductNumber
Product.ListPrice = ProductPM.ListPrice
Product.SellStartDate = DateTime.Now
Product.ModifiedDate = DateTime.Now
Product.SafetyStockLevel = 1000
Product.ReorderPoint = 700
Product.StandardCost = 0
Product.DaysToManufacture = 3
Product.rowguid = Guid.NewGuid()
Me.ObjectContext.Products.AddObject(Product)
Me.ChangeSet.Associate(ProductPM, Product, AddressOf UpdateProductPMKey)
For Each ProductInventoryPM As ProductInventoryPM In ProductPM.ProductInventory
Dim productInventory As New ProductInventory
With productInventory
.Bin = 1
.LocationID = 1
.ModifiedDate = DateTime.Now
.ProductID = ProductInventoryPM.ProductID
.Quantity = ProductInventoryPM.Quantity
.Shelf = "A"
End With
Product.ProductInventories.Add(productInventory)
Me.ChangeSet.Associate(ProductInventoryPM, productInventory, AddressOf UpdateProductInventoryKey)
Next
End Sub
Now if you run it, everything should work OK. Let me know how it goes.
Chris
Upvotes: 1
Reputation: 8369
The CRUD methods for the related types are exactly the same as for the main types. In the SL4 version of the book, there's a note at the bottom of the section titled "Updating Your Presentation Model Types" which discusses this. Basically, you just write the Get, Update, Insert, and Delete methods for your ProductInventoryPM models (in the same domain service), and RIA Services will take care of the rest. Have you experienced any problems doing this?
Upvotes: 0