Pixata Custom Controls
For Lightswitch

Recent Posts

Popular tags (# posts in brackets)

Anonymous types (3) ASP.NET (5) C# (3) C# tricks and tips (2) Computers (6) Design patterns (3) DomainDataSource (3) Dynamic data (4) Entity framework (3) Entity model framework (5) F# (3) LightSwitch (12) Linq (6) Microsoft (2) MVP (2) MVVM (2) Project Euler (2) RIA services (5) Silverlight (2) SQL Server (4) Unit testing (4) Visual Studio (7) WCF (3) WPF (3)

Gratuitous link to StackExchange




The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

Actually, as I'm self-employed, I guess that means that any views I expressed here aren't my own. That's confusing!


Theme modified from one by Tom Watts
C#/F# code styling by Manoli (for posts pre-2016) and Google code prettify (for post from Jan 2016 and beyond)

My rambling thoughts on exploring the .NET framework and related technologies

RIA services, and Linq in general is great for grabbing entity objects. The code is simple, and you end up with known objects that you can use.

BUT, when you want to deal with anything slightly off the beaten track, it gets a bit harder. For example, if you have a Linq query that returns an anonymous type, you can easily manipulate it in the same code block. For example, to take a gratuitously silly example, if you had a Bird class...

   1:    public class Bird {
   2:      private int m_Weight;
   3:      public Bird(int Weight) {
   4:        m_Weight = Weight;
   5:      }
   7:      public int Weight {
   8:        get {
   9:          return m_Weight;
  10:        }
  11:        set {
  12:          m_Weight = value;
  13:        }
  14:      }
  16:    }

...then you could write a Linq query like this...

   1:        var someBirds = from b in birds where b.Weight > 2 select b;
   2:        foreach (Bird b in someBirds) {
   3:          Console.WriteLine("  I weigh " + b.Weight.ToString() + "Kg");
   4:        }

...and since you are dealing with objects of type Bird, the compiler can cope happily.

Even if you are dealing with anonymous types, you're OK in the same code block. Imagine the Bird class had a Name property (amongst others), then you could write code like this...

   1:        var someBirds = from b in birds where b.Weight > 2 select new { Name = b.Name, Weight = b.Weight };
   2:        foreach (var b in someBirds) {
   3:          Console.WriteLine("My name is " + b.Name + ", and I weigh " + b.Weight.ToString() + "Kg");
   4:        }

Now here, the variable b in the foreach loop is of an anonymous type, but as you're in the same code block, the compiler knows what it's dealing with.

The problem comes when you want to pass the Linq query to another code block. This can be as a parameter in a method, or as the result of a RIA service call. Now you have a problem, as the code on the other side of the block boundary doesn't know what the type of the query object is. The framework seems to fall apart at this point!

The simplest way (that I've found so far) is to create a utility class that you can use in the Linq query...

   1:    public class TmpBird {
   2:      public int Weight { get; set; }
   3:      public string Name { get; set; }
   4:    }

I called this TmpBird to distinguish it from the normal Bird class. Note that this class only needs the two properties that this query uses, and we are taking advantage of C#'s auto-implemented properties, which means we only need to supply the access modifier, type and name for property. C# creates a private member variable, and codes up the getter and setter methods.

Now we can change our Linq query to look like this...

   1:        var someBirds = from b in birds where b.Weight > 2 select new TmpBird { Name = "Ferret", Weight = b.Weight };

And everything works fine. As long as the code block that receives the query knows about the utility class, it can handle the query easily. In particular, code that uses a RIA service can handle the query sent back.

This has the advantage that you don't have to send back a full entity object if you don't want. In the above example, we only wanted the weight and name of the bird, so we could ignore any other properties. In this simple case, there wouldn't have been any great loss in sending back the whole Bird object, but if you were dealing with bigger objects, it could make a significant difference.

For example, if you have a User object in your entity model, you may have a large number of properties. If you wanted to populate a drop-down list with users, so one could be chosen, you would only want the user ID and name. Instead of throwing around a collection of full User objects, you could create a light User utility class, and pass that around instead.

The one disadvantage of this approach is that you can end up with a proliferation of utility classes. However, if you make each class private to the code block that uses it, then the issue can be contained. If the query changes, then the code that uses it would need to know anyway, so updating the utility class isn't such a huge issue.

Comments are closed.