Friday, July 3, 2009

Dynamic LINQ To Entities Queries Using WCF/WPF demo code


It has been a while since I have undertaken a large article, so I thought it was about time I corrected that, and wrote a fairly meaty article. This article represents this meat.

So I suppose I should start by stating what it is this article will cover. Well it's going to cover quite a lot actually. The basic idea is that there is a SQL Server database somewhere that has the Northwind database installed, and that we will be using an ORM to obtain data from this database using a WCF service. The WCF itself will be hosted inside a Windows service which will be installed via a customer installer.

In order to interact with this Windows hosted WCF service we will be using a WPF client application. The WPF client application will allow the user to obtain particular entities (Customers only as I would have been writing this article for ever to allow for everything) to be queried using a custom query builder that will then be used to send to the Windows hosted WCF service, which in turn will then query the ORM for the queried entities, and return the results to the WPF client app.

In essence that is it, it may not be that exciting, but there is enough here for me to show you how to create a service/client and an hosting windows service. Also along the way I will be taking slight detours to talk about certain practices and cool code that may make your life easier.

I should point out that the UI was meant to be a throw away, ok I tried to make it an ok looking one, but that's just because I like WPF. I mean there is still some nice ideas in there, like the way the queries are constructed, that could be ported to a richer query generator, but that is left for an excercise to the reader.


You will need VS2008 (for the Entity Framework) and VS2008 SP1, and .NET 3.5 SP1 installed.

Things To Do To Get It Running For You

In order to get the attached code (WCF service hosted in a windows service/WPF client) you will need to make sure the following has been done

  1. That the windows service has been installed and provided with login details as part of the installation
  2. That the LINQ to Entities connectionStrings section of the associated .Config file are updated to point to your local SQL server installation
  3. That the windows service is running when you attempt ot use the WPFClient
  4. That the App.Config within the WPFClient project is configured to use the correct security login, this is in the identity/userPrincipalName element.

Just to get you a little bit more familiar with it all here is the project structure

Show It

So what does it look like when running, well I decided to show you some screen shots and also have a link to a small video for this one.

Initial view loaded

Changing the type of view for the selected Customer Orders

Starting to build up a dynamic query.

LINQ to Entities

For the ORM I had originally thought about using LINQ to SQL, but you know I think Microsoft are slowly sweeping (personal opinion) that one under the carpet, in favour of LINQ to Entities. So I thought why not use LINQ to Entities. So that's what this article uses for the ORM.

I had in the past installed the LINQ to Entities whilst it was still in BETA and it was horrible, the designer used to churn out 3 sections of XML:

CSDL : Conceptual schema definition language (CSDL)

MSDL : Mapping specification language (MSL)

SSDL : Store schema definition language (SSDL)

As I saw the designer used to create these, but not all that well in the old days and you always had to get your hands dirty in the XML, and there was loads of it, a couple of 1000nd lines for a small database. Eeek.

If you want to know how bad this was just use any of the 3 links above.

Mantaining these 3 sections of XML using the old beta of LINQ to Entities was a frigging nightmare. Thankfully the new designer does away with the user having to worry about the XML directly. Which I think is a good thing. What happens now is that the cod behind for the designer is now a C#|VB class that has a App.Config file associated with it, with a very strange ConnectionStrings section. Let us have a look at one of these:

See full detail:

No comments: