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 »

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 »

Querystring Element Insertion

One problem I frequently come across is having to redirect to a page using the current query string in the request, but also having to add new parameters. Furthermore, the parameters I want to add might already exist in the querystring, and if you're redirecting to the same page all hell breaks loose within a few clicks of the link.

The exact problem I'm trying to solve is this: I have one page which gathers parameters from the user for an advanced product search, which then passes the parameters through the querystring to a search results page. This page contains a GridView control that has column headings which also pass some values through the querystring (information about which column to sort on and in what direction - I'll blog about this another time). When the column is clicked, I still want to keep the information about the search and also the sort criteria. Not all of the parameters will be in the querystring at any given time.

I have to deal with this problem on a few pages, and I need a way to quickly and easily redirect to a url passing the current querystring parameters whilst at the same time injecting one or more new parameters if they don't currently exist. If they do exist, I might want to re-assign it a new value.

The (hopefully) obvious solution is to work with the querystring while it's still in the form of a Collection, rather than a string. Unfortunately for us, the Request.Querystring instance during a page is read-only. So how do we go about this?

What I've come up with is a pattern for achieving the solution, rather than a handy method or object. It's quick, painless and flexible, and here's the code:

Dim newQsColl As New NameValueCollection(HttpContext.Current.Request.QueryString)
newQsColl.Set(sortExpressionParam, sortExpression)
newQsColl.Set(sortDirectionParam, CType(sortDir, Integer))

Dim newQs As String = newQsColl.GlueElements("&")

The last line I'll talke about in a second. This code snippet allows you to take the current querystring and essentially add more key/value pairs to it. Here I've used the 'Set' method to create the new key/value pairs; this method will add the new pair if the key doesn't exist, but will change the key if it doesn't exist. Exactly the behaviour I was after!

An extra facility I wanted to finish this off was some easy method to glue the elements together in a format I could stick straight back into a hyperlink without any fuss. You could create a special lightweight class to do this, but since I'm using Asp.Net 3.5 I used an extension method. Ideally I'd like to implement the following on any IDictionary (since that interface follows the same key/value element structure), but alas NameValueCollection doesn't inherit from such an interface.

Nevertheless here's my extension method, named 'GlueElements':

<Extension()> _
Public Function GlueElements(ByVal collection As NameValueCollection, _
	ByVal glue As String) As String

	Dim sb As New Text.StringBuilder()

	For Each key In collection.Keys
		sb.Append(String.Format("{0}={1}{2}", key, collection(key), glue))
	Next

	Dim result As String = sb.ToString()

	If Not String.IsNullOrEmpty(glue) Then
		result = Left(result, result.Length - glue.Length)
	End If

	Return result
End Function

This method simply loops through the keys in the collection and joins together the keys and values using a specified 'glue' string. It then outputs the result, stripping off the last instance of the glue before returning it. If you set the glue string to '&', you end up with a string suitable for inserting straight into a url as a querystring.