Search code examples
silverlightwcf-ria-services

Silverlight PresentationModel contains a collection of another presentationModel


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!


Solution

  • 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:

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

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