Jerry Nixon @Work: June 2010

Jerry Nixon on Windows

Wednesday, June 30, 2010

EF4 Insert, Update, Delete in a WCF method

Once you have your models created and you are ready to save, the syntax is easy – once you know the syntax. And if you want to see how we do it, the code is [here].

6a00d8341bf6c153ef0115705c165b970c-800wi[1] So, we decided to use TT files to build this for us. We’re using the STE (Self-Tracking Entities) referenced in an outside assembly, and that seems to work just fine.

To be honest, I have been quite happy with the Entity Framework – including our decision to dump Data Services and stick to entity-specific CRUD methods on the service tier.

We also noticed that cascade logic seems to be plain-ole automatic. Even when cascades are not enabled within the SQL schema, they appear to work properly as an EF operation. Cool.

To the right, that Ted – and his “Street CRUD”: Create, Read, Update and Delete. My method sample is the CUD part.

WPF Linear Gradient Fill and animated Fade

So much is not obvious in WPF. For example, say you have a linear gradient filling a control. Want to transition to another color? Just animate to a new brush. Oh, if only you could.

imageOf course, you can switch brushes with a style trigger. But if you want a slow and sexy transition, you are stuck animating each gradient stop one at a time. That’s right. Yuck.

Anyway, there is no time for judgment. Let’s just hope future feature enhancements enable this. Meanwhile, here’s how you do it with WPF4:

The scenario is a simple buttons list. You want them to track your mouse’s movements and highlight. Then lose their highlight slowly as you move away.

I have wrapped them in a ScrollViewer. And these aren’t really buttons, they are just Border controls. But, it just goes to how you can do the same magic to just about any control.

A shared Style resource is attached to each of the Border controls, and if you look at the code (here) you can see the syntax is tricky, but not rocket science.

Thursday, June 24, 2010

WPF Data Presentation: step-by-step

My scenario is simple. I wanted to do a search for Contacts, and have the results be a bunch of rolodex cards loaded in a WrapPanel. Here’s how I did it.

Please note that each screen shot builds on the previous screen shot. You can’t just jump to one of them and get something (unless you jump to the end).

 

Sample Data

I needed some classes. I don’t like declarative data. I like classes. So I created Contact and Contacts. There’s nothing special about them. Here’s the code:

image

See that ObservableCollection I am using for People? That’s just like List<Person> except ObservableCollection has events indicating membership has changed.

 

Databinding the ItemsControl

Next, data bind People to to the ItemsControl. The ItemsControl reminds me a lot of the Asp.Net Repeater control. Other controls handle layout. And here’s how to do it:

image

See the PersonTemplate I created? I could have made it a resource of the Grid, or a property of ItemsControl. Eventually, I will move it to a separate file. Here’s the code behind:

image

Yep. That’s it – and we won’t be changing this code behind for the rest of the scenario. And here’s what it looks like when you run it:

image 

 

Adding a WrapPanel

This is a start, right? But where are my cards? My WrapPanel? And where is a little formatting to make this look like a professional app and not a sample? First the WrapPanel:

image

See how the WrapPanel ends up as a child of the ItemsControl? I point that out because it is not 100% intuitive to do things like that. But that’s the technique. Here’s the UI:

image 

 

Adding Grouping

Now, in a sense, we are done. But let’s add grouping? We’ll need a CollectionViewSource to do that. It defines which property is the “group by” property. Here’s now:

image

See the CollectionViewSource with a PropertyGroupDescription of Title? Now, it doesn’t have a reference to the People class. Not yet. I will do that from Code Behind like this:

image

See the FindReference bit? That’s where I go get the CollectionViewSource (there’s no other way, really) so I can set its Source property to People.

Now, to show groups we specify a HeaderTemplate under GroupStyle. There will be an item for every group header with a WrapPanel as its next sibling with a filtered list of items. Here:

image

See that I have added a new HeaderTeamplate in the Windows Resources? Like PersonTemplate, we’ll be moving that off to it’s own file.

Here’s a really helpful note: no matter what field you are filtering on, in the HeaderTemplate you bind to Name. You see, the header is binding to the group, not the item anymore.

The real magic around grouping is handled in ItemsControl.GroupStyle. It allows you to indicate the HeaderTemplate if the CollectionViewSource indicates a Group.

Here’s the resulting UI:

image 

 

Adding Sorting

But what if you wanted to add some sorting? I want to sort the Groups Descending (Z-A) and I want to sort the People Ascending (A-Z). Is this difficult? Not with CollectionViewSource.

image

There’s something very important to notice, and I highlighted it in the screenshot. See that I have added a namespace reference to System.ComponentModel – that is important because SortDescription is located there (for some reason). Here’s the result:

image 

 

Moving Resources to Files

Now, let’s clean out those Windows.Resources. This will make your XAML smaller (more manageable) and let you reuse resources on separate Pages or Windows.

First you will need to add a ResourceDictionary to your project (it’s just a file); pick it from the Add New Item dialog. Here’s a screen shot:

image

All I did was Copy-n-Paste from Windows.Resources into my PersonTemplate and HeaderTemplate files. Then I replace them with this in Windows.Resources:

image

In so many ways this is like a CSS link. Now my DataTemplates are stored in separate files that can be checked out by separate developers. By the way, I only left the CollectionViewSource to show you how to combine linked and local resources.

 

Styling the Output

Let’s take some time to make our data look a little nicer. We’ll start with the header. Let’s make it look nice – but remember, not too sexy. Let’s not unleash every WPF feature on our user.

If we update the HeaderTemplate with some extra style like this:

image

We get this:

image

If we update the PersonTemplate with some style like this:

What’s that? You can’t read all the code? Don’t sweat it. 1) you need to learn to develop a little on your own and 2) I will include all the code it at the end.

See the CornerRadius attribute on the Border? That’s what allows me to have rounded corners. The bigger the number, the greater the rounding.

See the Style.Triggers where I handle the classic MouseOver effect? You can do anything, of course, but I color them orange. It’s the IsMouseOver property that you want to test.

You get this:

image 

 

Making the Header Collapse

Let’s say you want the Header to expand and collapse. We’ll need to make a few changes. Specifically, we must use a ControlTemplate instead of a DataTemplate for the Header.

Unfortunately, this is not possible using ItemsControl. We need to switch to ListView. They are from the same base class. Then we update ItemsControl.GroupStyle like this:

image

See the ContainerStyle? That’s where I am injecting the template name. But our current HeaderTemplate is a DataTemplate and that is not right. We’ll need to change it like this:

image

When the UI does what we want:

image

And when you look at the code, there really isn’t a lot. Not even a lot of XAML. But there sure is a lot of opportunity to mess it up or get the syntax wrong. But once you have it, it’s beautiful.

 

Warning

If you are like me, and I hope you are not, then you might occasionally get this error:

“Operation is not valid while ItemsSource is in use”

Although it seems that the error message is a little to “generic” it typically means that there is a syntax error in the way you have the XAML configured. If you get that, then double-check your XAML. Odds are, you have something typed wrong or in the wrong location or something.

Good hunting.

Here’s the code: You’re Welcome 
(64k zip of the whole solution)

Thursday, June 3, 2010

Open Windows Live Messenger mailto-like syntax

Did you know it was possible to have a link open Messenger and start a chat with a specific contact? It’s similar to the technique you use to send an email like (mailto:email@domain.com).

The syntax looks like this: (msnim:chat?contact=nixon_jerry@hotmail.com) You still need to add the contact to your contact list for it to fully work, but the simple syntax it shortcuts the add-new-contact process a bunch.

Start a chat (msnim:chat?contact=nixon_jerry@hotmail.com)

Add to contact list (msnim:add?contact=nixon_jerry@hotmail.com)

Start a voice chat (msnim:voice?contact=nixon_jerry@hotmail.com)

Start a video chat (msnim:video?contact=nixon_jerry@hotmail.com)

Tuesday, June 1, 2010

An Service Oriented (SOA), multi-tier implementation of the Microsoft Entity Framework (EF4) using WCF Data Services

Using the Entity Framework is Microsoft’s latest Data Access technique. Hopefully, they will settle on this one for a few versions. The version of the Entity Framework in .Net 4 is the first real version, if you ask me. The problems with predecessors are sick. Good work guys.

The Entity Framework lets you use LINQ syntax to access your data with a robust mapping engine to use across various data sources. LINQ allows you to create controlled, in-code queries against your data sources without requiring stored procedures or lots of code.

The Entity Framework allows you to, optionally, use code generated Self-Tracking Entities, for example, that have the ability to track their changes. Code generated entities allow you to introduce your own enhancements to the entities without worrying about screwing up the Entity Framework.

The Entity Framework allows you to break out the Model into a separate project, and your entities into a separate, reusable project.

Introducing ADO Data Services. It isn’t new, but now it’s native. It’s a WCF Service that enables LINQ queries on the client. Data Services isn’t super-documented right now. So custom query strings, and reusing entity types on the client isn’t straight forward.

There’s a bug in how types are reused on the client. And the techniques to use custom query strings and security in Data Services isn’t clear.

Data Services really lets you use a service oriented architecture with EF. But, to be fair, it even lets you use it with LINQ 2 SQL (LinqToSQL). LinqToSQL is not deprecated, but you should consider it deprecated in your own head. Just start using Entity Framework and thank me later.

imageBlah, blah, get to it already!

Okay, I hope these short screen casts help you. Let me know!

1. How do you create an EF model in a stand-alone, reusable assembly – and then consume it in a simple WCF Data Service?

Watch the video | Get the code

2. How do you extract the entities from the model, and move them to a stand-alone, reusable assembly – and then consume them?

Watch the video | Get the code

3. How do you use the Data Service and enable the service reference to properly reuse the entity types on the client?

Watch the video | Get the code

4. How do you introduce a custom SQL connection string when using a data service? Is it really this difficult?

Watch the video | Get the code

In case you wanted to know, I use VS 2010, and SQL 2008. I’m inside Windows 7 Ultimate, but I don’t think those details matter so much. I hope these casts help you. Honestly, I created them to help me remember the little tricks necessary to get it all to work – in six months ;)

Next to figure out: How to save data back to the database without an error?!

Then to figure out: Simple Security with Data Services

[Update 6/2/10] As the architect of our current project, I had to make the call to drop using Data Services. EF is just fine. The delays we hit trying to get simple tasks working killed us. Fortunately, we dedicated a sprint to validate Data Services. I believe, until the WCF team fixes the known Service Reference bug, Data Services is useless.

I asked:

  1. Why can’t Data Services support the SQL [time] data type?
  2. Why can’t Data Services have used Include(), like EF, instead of Expand()?
  3. Why can’t Data Services’ Expand() use “Child.GrandChild” syntax like EF Include() instead of a just-to-be-different “Child/GrandChild” syntax?
  4. Why can’t Data Services attach and update with EF syntax?
  5. Why can’t I query against an Expanded child collection/navigation property?
  6. Why can’t Data Services just work? There was so much promise!

Some of those gripes, to be fair, might be a result that ODATA was the result of a standards committee and, maybe, EF needs a little hit on the head instead. Uniformity, guys!

Also, I saw where WCF Data Services (actually ODATA) was written up in MSDN magazine this month (June 2010). The article never makes this point, but it’s clear to me based on my experience and the documentation and samples I see: ODATA is good as a simple “read” operations, but complex reading or simple writing isn’t where is should be. Not at all.

So, it’s “wait for Service Pack next” for us…