Tag Archives: .NET

Stop playing with the LightSwitch

25 Aug

If you’ll excuse me for a moment, I just got the sudden urge to totally geek out. Oh, who am I kidding? Here’s my latest techno-nerd-babble:

(FYI: It’s not a groove, it’s not slightly transformed, but it is a bit of a break from the norm.)

Yesterday I installed the new Visual Studio LightSwitch (Beta 1) app. After playing with some samples, I tried to create a simple app that calls an external web service and displays the data. I read somewhere that this isn’t possible unless you first call a WCF RIA Service, which could itself call the external service and send the result back to LightSwitch. Kind of like driving from Kansas City to St. Louis to get to Denver.

So, I added a WCF RIA Services project to the solution. To this project I added a service reference to a free web service I found which takes a given year and returns the date Black Friday took place that year. Delightful! And almost completely useless. But hey, it’s free.

I created a service method in my RIA service that simply calls the external service five times with five different dates. I wanted multiple results so I could return an IEnumerable<T> back to the UI. Next, I went through LightSwitch’s Add Data Source wizard and chose “WCF RIA Service”. After waiting an eternity (they take the word “Beta” very, very seriously at Microsoft), I was finally able to complete the wizard and add a reference to my RIA service.

Finally, I added a simple screen to display the data in a grid when the app loads. I hit F5 and…hey, it worked! That was both fun and educational. Maybe I’ll post screenshots if I get something interesting built with the production version of the tool. In the meantime, I better quit playing with the LightSwitch before I start a fire.

Right…like THAT’S really gonna happen, Dad.

What’s your name?

20 Apr

In .NET, having access to the name of a property can be valuable in several scenarios, such as data-binding. However, hard-coding the name of the property in the designer or in a string variable is error prone and can be a pain to deal with when refactoring.¬†We need an easy way to get the “code” name of an object’s property, not it’s property value.

The Props class below has a static method for getting the name (as a string) of a given object’s property. This method uses the generic Expression type to build an expression tree and access the member (property/field) information. I wish there was a way to get rid of the “() =>” syntax, but I can’t seem to find one.

Try it out:

using System;
using System.Linq.Expressions;

namespace PropertyNames
{
    class Program
    {
        static void Main(string[] args)
        {
            Car audi = new Car();

            Console.WriteLine("The name of the property with the engine data is {0}",
                Props.GetName(() => audi.Engine));

            Console.WriteLine("The name of the property with the spoiler flag is {0}",
                Props.GetName(() => audi.HasSpoiler));

            Console.Read();
        }
    }

    internal class Car
    {
        public bool HasSpoiler { get; set; }
        public string Engine { get { return "V8";} }
    }

    internal static class Props
    {
        public static string GetName<T>(Expression<Func<T>> expression)
        {
            MemberExpression memberExpression = expression.Body as MemberExpression;
            if (memberExpression == null)
                throw new ArgumentException("Property specified is invalid");
            else
                return memberExpression.Member.Name;
         }
    }
}