Tweak to that join syntax

I really wasn’t happy with the way you need to break up the method chain to do certain joins, and while writing the release post last night an alternative occurred to me, which I have just spiked and liked:

dynamic manager; // To hold the aliased reference
var q = _db.Employees.Query()
    .Join(_db.Employees.As("Manager"), out manager).On(manager.Id == _db.Employees.ManagerId)
    .Select(_db.Employees.Name, manager.Name.As("Manager"));

This requires a variable to be declared ahead of the query, which gets passed as an out parameter to the Join method and receives the aliased ObjectReference. That variable can then be used for that reference throughout the rest of the query. It’s still a compromise, but I think it’s a better one.

It still works with named parameter syntax, but because runtime “magic” methods don’t support out parameters in .NET 4, I’ve had to shunt the named parameters into an overload of the On method:

dynamic manager; // To hold the aliased reference
var q = _db.Employees.Query()
    .Join(_db.Employees.As("Manager"), out manager).On(manager.Id == _db.Employees.ManagerId)
    .Select(_db.Employees.Name, manager.Name.As("Manager"));

I don’t know about you, but I occasionally release a piece of software into production and then, next time I look at the code, think of a better way of achieving something. The nice thing about having good test coverage is that you can make those changes and be confident that what you’ve done hasn’t broken anything.

Anyway, this syntax will be in 0.6.9, along with the Having clause support.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: