Silverlight Handle how to Mutiple Async calls

Answered Silverlight Handle how to Mutiple Async calls

  • Thursday, October 11, 2012 4:30 PM
     
     

    I have a situation where , i have a custom object on client like

    where Entity2 is dependent on Entity1 values and so on

    public classs CustomClass

     {

          public RiaEntity1 Entity1{get; set;}

          public RiaEntity2 Entity2{get; set;}

          public RiaEntity3 Entity3{get; set;}

          public RiaEntity4 Entity4{get; set;}

     }

    // Implementation

    public CustomClass MyCustomClass{get;set;}

    FillCustomClass()

     {  

    MyCustomClass.Entity1 = LoadOperation1();

    LoadOperation1Completed()

    {

    MyCustomClass.Entity2 = LoadOperation2

    }

    ...

    ..

     } 

    Instead of calling them Async individually and nesting them sequentially on the dependecy order  ...Is there a better way ...?

All Replies

  • Thursday, October 11, 2012 4:46 PM
     
     

    What does your DomainService look like? If you are using EntityFramework you would just need to make sure that the associations were decorated with the [Include] attribute and in the EF query use the .Include{"Entity2"} to eager load the related entities.

  • Thursday, October 11, 2012 5:44 PM
     
     

    See , OUR data base is normalized, which means lot of affiliation tables .So for example to get a Employer for a customer

    i have to deal with

    Income table

    Emplr table 

    Emplr Phone Affill table

    Emplr Addr Afill table

    Supervisor table

    Supervisor Phone Affill table

    supervisor Addr Afill table

    I was thinking creating a custom object on server and fill all these and send back to client ?

    I am having issues doing so

  • Thursday, October 11, 2012 6:10 PM
     
     Answered

    RIA Services allows you to maintain the same Domain Model on the client as you have on the server. It is best to keep inside those bounds.

    Let me see if I understand your table structure correctly. I am supplying my own table names to be more descriptive. You have:

    Customer, CustomerIncome

    Employer, EmployerPhone, Phone, EmployerAddress, Address

    Supervisor, SupervisorPhone, SupervisorAddress

     

    To start with, you need to figure out what you application actually needs. If you are not editing Employers and Supervisors and you only care about one phone and one address each then you could flatten those objects down. There are many ways to do that but I personally prefer just adding views to the Database that flatten the data down to the way I want to access them. That would result in single Employer and Supervisor entities that would match your application better. But, for now, I will assume you need all of the different entities.

    One of the important rules to keep in mind in Entity Framework is that including across a many will result in poor performance. So, I would split your data load up into separate loads from the database. Those loads are:

    return ObjectContext.Customer.Include("CustomerIncome").Include("Employer").Include("Supervisor").Where(c=>c.CustomerId == customerId);

    return ObjectContext.CustomerPhone.Include("Phone").Where(cp=>cp.Customer.Customers.Where(c=>c.Customer == customerId).Any());

    return ObjectContext.CustomerAddress.Include("Address").Where(cp=>cp.Customer.Customers.Where(c=>c.Customer == customerId).Any());

    return ObjectContext.SupervisorPhone.Include("Phone").Where(cp=>cp.Supervisor.Customers.Where(c=>c.Customer == customerId).Any());

    return ObjectContext.SupervisorAddress.Include("Address").Where(cp=>cp.Supervisor.Customers.Where(c=>c.Customer == customerId).Any());

    That would require multiple load calls, but you can issue them all at the same time if you wish. If you want you can also run those queries server side within a single call. Something like:

    public Customer GetCustomer(int customerId)
    {
        customer = qry1.FirstOrDefault();
        qry2.ToList();
        qry3.ToList();
        qry4.ToList();
        qry5.ToList();
        return customer;
    }

    The ToLists are loading the entities into the server side cache then when the customer is returned to the client all of the related objects would be serialized and sent to the client.

    Finally, if you do flatten the objects down, that first qry all by itself would be all that you need. Do keep in mind that to serialize the related objects the associations do need to be decorated with the Include attribute as well as the Include in the query itself. I hope all of this made sense.