Saturday, January 31, 2009

The pain of session variables in LINQ

Linq to Entities rocks. Seriously, but there are a few little bugbears.

One of them is in how errors are reported in a totally misleading manner.

Consider this little snippet of code.


var q=from t in MyEnities.SomeEntitySet
where t.ID==(int)Session["userid']
select t;

if (q.Count() > 0)


q.Count threw an exception that said:

LINQ to Entities does not recognize the method 'System.Object get_Item(System.String)' method, and this method cannot be translated into a store expression.

This drove me mad for hours because I tried everything such as verifying that the session existed, that the variable was real, was of the right type and was of the value I expected. All to no avail.

In the end I solved the problem by first saving the session value into a local variable which I then used in the LINQ expression.

It seems to me that this has to do with the order of how things are evaluated within a LINQ expression and may even constitute a bug in the language. Why? well because it seems obvious now that the direct value from the session variable is somehow used before the cast operation by the LINQ parser and so an exception that seems to have no real relation to actual circumstances is thrown.

I hope that this helps to trap similar errors for people hunting for hours on Google like I did.

Wednesday, January 28, 2009

Fire and forget storyboards...

I have fallen deeply in love with Silverlight and it's evident, in Silverlight 2 at least, that one cannot do everything in XAML and there is still an element of code needed to do a job.

Because of the lack of triggers in the same sense as those that exist in WPF, Silverlight can be a pain when we need to create dynamic animated effects. However I had an idea that leans heavily upon the joys of garbage collection and anonymous methods to provide a fire-and-forget style storyboard which can do animations and handle its own events.

One little job I recently accomplished with this was to create some animation effects that had two stages; for example, to fade one element out and replace it with another.

The joy of this technique is that we can build a storyboard and the associated animation details dynamically, set it running and then just forget it while it does its thing. After the events have fired the whole thing -should- be reclaimed and garbage collected. (He said, wondering about shooting himself in the foot.

Here's some XAML...

<UserControl x:Class="FireAndForget.Page"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

Width="400" Height="300">

<Grid x:Name="LayoutRoot" Background="White">

<Ellipse Fill="red"/>

<Button Content="Click Me" Width="100" Height="25" Click="Button_Click"/>

Grid>

UserControl>

And here’s the button click code..

private void Button_Click(object sender, RoutedEventArgs e)

{

Storyboard sb = new Storyboard();

DoubleAnimation da = new DoubleAnimation();

da.BeginTime = TimeSpan.FromMilliseconds(0);

da.Duration = new Duration(TimeSpan.FromMilliseconds(1000));

da.From=1;

da.To = 0;

sb.Children.Add(da);

Storyboard.SetTargetProperty(da, new PropertyPath("(UIElement.Opacity)"));

Storyboard.SetTarget(da, this.LayoutRoot.Children[0]);

sb.Completed += delegate(Object o, EventArgs ea)

{

Random rnd=new Random();

((Ellipse)this.LayoutRoot.Children[0]).Fill = new SolidColorBrush(Color.FromArgb(255, (byte)rnd.Next(255), (byte)rnd.Next(255), (byte)rnd.Next(255)));

sb = new Storyboard();

da = new DoubleAnimation();

da.BeginTime = TimeSpan.FromMilliseconds(0);

da.Duration = new Duration(TimeSpan.FromMilliseconds(1000));

da.From=0;

da.To=1;

sb.Children.Add(da);

Storyboard.SetTargetProperty(da, new PropertyPath("(UIElement.Opacity)"));

Storyboard.SetTarget(da, this.LayoutRoot.Children[0]);

sb.Begin();

//done!

};

sb.Begin();

}

Good eh?

Tuesday, January 06, 2009

Oh the shame!!

Ok, I admit it, I'm a child of the sixties. I have grey hair, I'm overweight, I'm nearly fifty and probably in line for a major mid-life crisis. I also work as a software architect on a job that requires that I become intimately familiar with the in's and out's of.. wait for it... VB6! Is it any wonder I am going insane??

Today, my pals at work had a great laugh at my expense. I was quite happy to show them my new pages on my site, yeah, the ones with the Silverlight banners.. and after a couple of seconds, they laughed so hard that one of them fell off his chair.. Seriously.

So my Silverlight efforts are too "bling bling". My site has a 1970's look 'n' feel. My banners are crass and I have no taste in, well, in anything much!

I live and work in the country of my own country's traditional enemy. I speak their language and accept their customs. I even listen to their ever-so twisted version of history with good humour. I suppose that I'll just have to accept the fact that my site is "moche". Anyone want the code?

Sunday, January 04, 2009

Silverlight fulfilment!

Well, you can see the difference in times between these two posts. That's how long it took Don at Brinkster to get my IIS all configured for Silverlight. [Well, this isn't actually true because I made a draft of the previous post from my iPhone this morning and finished it up a few minutes ago. Don took precisely 6 minutes and 20 seconds to fix my site]

I must say that Brinkster have really made leaps and bounds on the quality of their hosting and particularly their support.

I use GoDaddy for some of my sites because, well, let's face it, they're cheap and cheerful but for all my mission critical stuff I use Brinkster. Even though it costs just a little bit more it's worth the money for the level of support that these guys give.

You can see the new silverlight XmplXaml page by following the link..

Silverlight frustration

This holiday I decided to join the horde of silverlight and WPF bloggers and article authors because I thought that the technology is sufficiently mature to at last allow serious work to be undertaken using them as a medium. As a result my development site on my PC with some examples of animated Silverlight banners and such. As I mentioned before, I have experienced a certain amount of frustration at the lack of currently valid material, especially for Silverlight, so I have written a little application that serves a similar purpose to good old reflector but with a twist

The application, which I call XmplXaml, uses reflection to discover the properties of an object and provides a simplified view of the various collections and the types they can contain. So, rather than a class hierarchy you can see the Xaml hierarchy of tags. Running this application on PathGeometry for example, gives a rather nice view of the different PathSegments we can use and their relation to the root object.

So, now to the contunuing frustration part.. My bobpowell.net site is hosted on Brinkster and has been for many years now. I uploaded my new applications only to find that they haven't enabled silverlight on their servers. AARRGGHH!

I will either have to rename all the XAML files to XML, a soloution I am not happy with because I like the idea of doing everything directly from Visual Studio, or to get my old pal Jared over at Brinkster to have his team set up the silverlight MIME types properly. Failing that, I'll have to move my site to a server that supports them. That would be a shame because I like the Brinkster setup.

Here's a little screen-shot of my utility to be going on with...




Thursday, January 01, 2009

Misinformation is driving me insane!

I am heads down on a bunch of ASP.Net, WPF and Silverlight projects at the moment and I can categorically state that the amount of useless chatter on the web pertaining to all the previous beta versions of WPF and Silverlight technologies is more than a little annoying.

Try any simple search for information and you'll be swamped with good looking content that, after you read a few lines, turns out to be for Silverlight 1.0 beta versions and has absolutely no relevance to anyone using the technology on front-line applications today.

I call upon all authors and bloggers to clean up their blogs and remove every article that still turns up with old and confusing information about uselessly dead beta versions.

Oh.. happy new year too!