Simple object conversion pattern

Here's a quick conversion pattern you can use if you've got a lot of object conversion going on within your project, especially where databases and web services are involved. Bear in mind that this is essentially what the adapter pattern handles, but doesn't involve any sort of inheritence. It's like a cruder version of that pattern. It's also not as quick to use as a simple linq projection. However, you might want to do that tranposition more than once, so you'd want to stick that logic into a utility method or something similar.

This approach just formalises that utility method and enables you to keep all these kinds of conversions in one place; under one roof, if you will.

My solution involves an interface and (optionally) a utility method. It's handy for when you've got a set of data objects that you want to easily transpose and modify in order to send through a web service call to a client. Even if Linq To Sql (for example) does support such serialisation with WCF services without having to do much work, this often isn't desirable as generally you, as the programmer, want more control over exactly which data is sent across the wire. You might want to do this to guarantee packet sizes, for example, not to mention keeping the interface intact since your domain objects will change.

Read more »

LinqToLfsWorld 1.0.2 released

Just a quick post to say that LinqToLfsWorld 1.0.2 has been released - you can grab it from the project homepage.

This version of the library adds:

  • Support for serialization and deserialization to/from Xml and Json
  • A new demo website page, demonstrating the serialization features.
  • Some helper constants for use in your car type, track ident and steering method queries
  • A couple of bug fixes

The entity classes also support exposure through WCF services - I haven't added a test for this scenario yet to the project, but have verified in my own quick test project that it does work as expected.

I've written a more detailed account of how the serialization works on the 1.0.2 download page.

Querying LfsWorld with LinqToLfsWorld

In this post I'm going to show you all the different types of queries you can perform with LinqToLfsWorld. For an overview of what the library is and how it works, please feel free to read an earlier blog post which takes you through the demo website (included with the library download file).

The Queries

With the library you can perform all the queries that you would normally be able to using a plain url to LfsWorld (according to v1.4 of the pubstat service, which is the latest version at the time of release). These queries are accessed through the LfsWorldContext. All the examples below assume I have already initialised my context with the name lfswContext, and called Dispose() when I've finished with it. Disposing the context is important so that it can free up memory and unregister internal event handlers. If you don't dispose the context, you might find some weird things happening if you've handled the context's RequestMade event.

Read more »

LinqToLfsWorld 1.0 released!

Just a quick post to say that LinqToLfsWorld 1.0 has been released; you can grab it from the project homepage on codeplex.com.

Whilst the interface hasn't changed, under the bonnet the expression parsing code has been vastly improved and you can now do a lot more with it, compared to the 0.8 release from the beginning of the month.

For example, with the preview code you couldn't write a query such as:

var stat = from stats in context.RacerStats
where stats.RacerName == GetRacerName()
select stats;

.. the point here being that the expression parser couldn't evaluate local expressions, i.e. any expression which didn't contain a parameter expression from inside the query itself (in the example above, this local expression would be the call to GetRacerName()). Now though, all expressions which can be evaluated before the query is even looked at are evaluated down to a constant value. This means you can call any method or access any property you like from inside the query.

Some of the common selection methods are now supported also; you can directly call Single, SingleOrDefault, First, FirstOrDefault, Last, LastOrDefault and Count on an LfsWorldContext query. You can also call Select, provided the query only selects either a single entity or a list of entities. In other words, you cannot create an anonymous type from these queries. This limitation is trivial however, since you can just convert your results into a list and use plain Linq To Objects to select only the fields you need. The example website included with the download demonstrates this.

This released also fixed a number of bugs which were flagged up from testing the preview version, mainly to do with caching and the expression engine.

There are a couple of issues I'd like to sort out with regards to serialization, which will come out as some sort of '1.0.1' release perhaps. Over the next week or so I'm also going to give you a full run down of each type of query you can perform with this library on this very site.

If anyone is using the library and finds any issues with it, please direct them to the issue tracker on the project home page.

LinqToLfsWorld: A look at the demo website

Now that LinqToLfsWorld has been out for a couple of days, I've got time to explain a bit more about the features, using the demo website as an example.

The v0.8 Preview release contains the code library and the demo website for you to have a look at the interface first-hand. I should probably mention that for release 1.0 this will probably change; I'll make the demo website a separate and optional download, and the library in a separate download on its own. This blog post will use that demo website as an example.

Read more »

LinqToLfsWorld v0.8 Preview

After a bit of umming and arrrring I managed to make quite a bit of headway with a project I've been mulling over for a while - LinqToLfsWorld. It's a custom LINQ provider which allows the client to communicate with LfsWorld via its Pubstat web service.

What is that exactly? Live For Speed (LFS) is (primarly) an oAnline racing simulator, and LFSWorld is an online base for checking your racing stats, viewing and comparing hotlaps and watching other races in real time via a neat flash application. Pubstat is it's webservice which you can query to retrieve information about racers, hosts, teams and hotlaps. You send it a url and it will return either JSON, PHP or XML formatted responses.

Since it's url based, and it has a fairly simple interface, I thought it would be a good introduction into finding out what it takes to build a custom LINQ provider. My implementation isn't great nor totally versatile (yet), but it works and I'm pretty pleased with the project so far.

The reason the release is tagged as 0.8 Preview is because there's a couple of issues I'd like to sort out before I give it that 'release 1.0' milestone; the object documentation (currently there isn't any) and I'd like to work a bit more on the expression visitor, to make the queries just a bit more friendly.

So, if you're interested in having a look, please visit the project page on Codeplex and download the latest release and/or the source code and play around with it. Send me any issues if you want via the issue tracker, and feel free to contact me with any suggestions or queries relating to the project.

I'm going to publish a couple more articles over the course of the next couple of weeks as I improve the library, and to show you a couple more examples on how to use it.

VB Dynamic Query Library woes

According to my searches on the internet, not many people were having trouble trying to use the Linq Dynamic Query library inside a class library project.

The problem was that with my Asp.Net web project, I wanted to have my middle-tier code inside a class library which would then be referenced by the website (and another planned website later on). This class library would contain (amongst other things) my Linq data context and the Dynamic Query library. However, whenever I added the Dynamic Query library to the project the compiler would start complaining about missing objects.

After much tearing my hair out and research I still couldn't find the problem so I left it to work on something else. A similar issue arised when I was working on a completely unrelated piece of code, so it wasn't just attributed to the Linq library in my original problem. The real issue?

VB.Net class library namespaces. When you create a VB.Net class library project, you can specify a default namespace for everything in that library, and so when you are creating something inside a sub-namespace you leave off the default namespace which is already set on the project.

The dynamic library is implemented inside the namespace System.Linq.Dynamic, and so when I added it to my class library project the namespace it was in was actually MyNamespace.System.Linq.Dynamic. This meant that the System namespace now clashed with MyNamespace.System, and the data context which imports the System namespace, and was in MyNamespace, was importing the wrong one. Obviously, this was also happening to anything which imports the System or any sub-namespace of System. Which means essentially that the hellmouth opened and the world was in chaos. This issue doesn't arise in a normal web project, since you don't define a default namespace for classes. I haven't tried this, but I also assume the issue won't arise in a C# class library project since, while you can specify a default namespace for your classes, the compiler doesn't anonymously prepend your default namespace name onto the one you've explicitely set in the code file.

The solution is simple - dive into the code file for the Linq Dynamic Query library and remove the namespace declaration.