Contact Me

Contact Us
For an informal discussion

Everyone's business is different. Our business is software - ask where we can help.
eMail: john at appsolo.com

RIA Services – Nullable results and aggregates and a side order of RIA services structure and methodology

Posted on Monday, 19th July, 2010

if you are using a aggregate function in SQL for Entity Framework in the middle layer of RIA services Domain Service then the possibility that the projection query or an Object Context collection that you are using can return a nullable causes an issue for the aggregate queries that need to use Int32 return types only. Casting to int? will not help. A way around this is to check the count property of the of the generic collection returned and then take appropriate action. Here is the relevant part of the Data model behind all this

image thumb RIA Services   Nullable results and aggregates and a side order of RIA services structure and methodology

 

Case 1 based on executed EF Query

   1: [Invoke]

   2:         public int GetMaxMasterNodeID(int K)

   3:         { 

   4:             if(this.ObjectContext.NodeMasters.Where(m => m.TaxKeyID == K).Count<NodeMaster>() == 0 )

   5:                 return 0;

   6:             else return this.ObjectContext.NodeMasters.Where(m => m.TaxKeyID == K).Max(m => m.NodeID);

   7:         }

 

Case 2 based on possible empty collection in the Context Object of the Linq To Entities Domain Service (very similar)

   1: [Invoke]

   2: public int GetArcMax()

   3: {

   4:     if (this.ObjectContext.Arcs.Count<Arc>() == 0)

   5:         return 0;

   6:          else return this.ObjectContext.Arcs.Max(a => a.ArcID);

   7: }

 

The reason for doing this in our case here at Appsolo would be to programmatically create entities that are represented as a collection of tables for normalisation reasons while linking entities in the database as is done in the  following piece of code…

 

   1: [Invoke]

   2: public  void AddNewNodeMaster(int K)

   3: {            int CurrentMax = GetMaxMasterNodeID(K);

   4:              this.ObjectContext.NodeMasters.AddObject(new NodeMaster() { NodeID = CurrentMax + 1, TaxKeyID = K, MasterDescription = "Question Set Description to be filled" });

   5:             // When you add a new MasterNode you need to add a default node and Arc

   6:     this.ObjectContext.Arcs.AddObject(new Arc() { ArcDescription = "Not Set", ArcLabel = "Not Set" });

   7:     int NextArcId = this.GetArcMax(); // have to check for nullable otherwise it will throw an error is no arcs at all in DB

   8:     this.ObjectContext.Nodes.AddObject(new Node() { NodeID = CurrentMax + 1, ArcID = NextArcId, TaxKeyID = K });

   9:     this.ObjectContext.SaveChanges();

  10: }

 

SIDE TRACK

Of course you could do all or parts of this as a stored procedure(s) in the Database but “that my dear Doogle  is an ecumenical matter” as Father Ted would say.

If you do it as sprocs then you would have to use the Model Browser in the Entity Domain Model which I would call the backend of the Middle Tier/Layer of RIA services

 image thumb1 RIA Services   Nullable results and aggregates and a side order of RIA services structure and methodology

 

to include the sproc in the EDM and then flush forward the changes through the middle layer. This is an approach that I have taken previously. But I think this approach is more cumbersome (trackback 1). It also probably depends on whether you are more comfortable as a programmer or a DBA? Bottom line as always is that it all has to hang together some way with some level consistency of method and design pattern, otherwise you will GO MAAAAD! But sometimes, due to inadequacies in the RIA/Silverlight programming model behaviour( hey nothing’s perfect), you can be forced to work in the database side and work forward from there.

I may be inventing my own terminology here or re-inventing for RIA, (feel free to comment – maybe MS have their own terminology for what’s going on? – Possibly trackback 2), but as I see it RIA has a backend (Entity Domain Model and associated cs code) to communicate between and handle the database traffic and a front end to communicate between the client application using Domain Services and the Entity Framework with linkage between the EF and EDM handle . The business logic mentioned in trackback 2 resides mostly in the form of EF management code as good chunks of the the EDM code are hidden and probably should remain so.

image thumb2 RIA Services   Nullable results and aggregates and a side order of RIA services structure and methodology

One the front layer of RIA is also the hidden client side code (if you select a Domain Service function in the client side code in VS2010 and hit F12 you go to the client side (hidden and generated) code) to call the Domain Service EF based code that you have writen (what another layer of abstraction!! yes.)

   1: /// <summary>

   2:         /// Asynchronously invokes the 'AddNewNodeMaster' method of the domain service.

   3:         /// </summary>

   4:         /// <param name="K">The value for the 'K' parameter of this action.</param>

   5:         /// <param name="callback">Callback to invoke when the operation completes.</param>

   6:         /// <param name="userState">Value to pass to the callback.  It can be <c>null</c>.</param>

   7:         /// <returns>An operation instance that can be used to manage the asynchronous request.</returns>

   8:         public InvokeOperation AddNewNodeMaster(int K, Action<InvokeOperation> callback, object userState)

   9:         {

  10:             Dictionary<string, object> parameters = new Dictionary<string, object>();

  11:             parameters.Add("K", K);

  12:             this.ValidateMethod("AddNewNodeMaster", parameters);

  13:             return this.InvokeOperation("AddNewNodeMaster", typeof(void), parameters, true, callback, userState);

  14:         }

  15:         

  16:         /// <summary>

  17:         /// Asynchronously invokes the 'AddNewNodeMaster' method of the domain service.

  18:         /// </summary>

  19:         /// <param name="K">The value for the 'K' parameter of this action.</param>

  20:         /// <returns>An operation instance that can be used to manage the asynchronous request.</returns>

  21:         public InvokeOperation AddNewNodeMaster(int K)

  22:         {

  23:             Dictionary<string, object> parameters = new Dictionary<string, object>();

  24:             parameters.Add("K", K);

  25:             this.ValidateMethod("AddNewNodeMaster", parameters);

  26:             return this.InvokeOperation("AddNewNodeMaster", typeof(void), parameters, true, null, null);

  27:         }

 

P.

Comments are closed.

latest news

Configuring Deployment of Silverlight

Posted on Wednesday, 17th August, 2011

It is sometimes necessary to configure Silverlight differently especially when using WCF services. I read this useful post by Mohamed Ibrahim Mostafa on how to configure the ServiceReferences.ClientConfig XML file to cater for different deployment environments. I found this most useful. As the post suggests the the XAP contains references to the ServiceReferences.ClientConfig XML file. [...]

Testimonials

Excellent design skills

Posted on Sunday, 2nd May, 2010

We at Taxonomy.ie are happy to be associated with Appsolo and look forward to further work together.

follow me

twitter facebook delicious

AppsoloLtd. VAT No. IE97548691 - Copyright © 2010.