Base4.net compile-time query handling support

Well lots to blog about, but no time to do it... at least not today - at any rate I just thought I would say if you haven't considered using Base4 up until now, why not give it a go with the latest drop :) I'm loving these new changes with the support for Compile-time query handling... I think at this point you can start prototyping stuff with base4 very quickly, between the UI for designing or discovering an existing schema, and now the ability to avoid learning the ObjectPath query syntax it's pretty compelling...



At any rate, as mentioned in the various posts from Alex James, you can now write these kinds of queries:



IItemList matchingFiles = StorageContext.Find(FileBase.Fields.FileSize > 1024000 && FileBase.Fields.MimeType == "audio/wav");


Faboo, and of course you can drill through the relationships:


Track track =  StorageContext.FindOne(Track.Fields.Name == reference.TrackName
&& Track.Fields.Release.Name == reference.ReleaseName
&& Track.Fields.Release.Artist.Name == reference.ArtistName);


This is for a simple hierarchy, where we have Artists-> Releases -> Tracks (sadly organising Music in reality isn't quite this easy...)



But as you can see these queries are getting quite verbose, personally this is where the elegance of this approach comes in... reuse is possible, for example - we can build a static class to hold common queries:


public static class FileBaseQueries
{
public static ExpressionField LargerThenOneMegabyte
{
get
{
return FileBase.Fields.FileSize > 1024000;
}
}

public static ExpressionField MimeTypeIsAudioWav
{
get
{
return FileBase.Fields.MimeType == "audio/wav";
}
}
}



And now the first query could be rewritten:


StorageContext.Find(FileBaseQueries.LargerThenOneMegabyte && FileBaseQueries.MimeTypeIsAudioWav);


The main thing I like about this approach is your queries start looking like sentences... "Larger Then One Megabyte and Mime Type Is Audio Wav"...



Another idea might be to use a method on the static class instead...


public static ExpressionField MimeTypeIs(string type, string subType)
{
return FileBase.Fields.MimeType == string.Format("{0}/{1}", type, subType);
}


And then writing something like:


StorageContext.Find(FileBaseQueries.LargerThenOneMegabyte && FileBaseQueries.MimeTypeIs("audio","wav"));


At this point it still reads quite nicely, but it's a little more flexible...



Edit: I just noticed this entry wasn't actually marked as syndicated... fixed it now.
Written on October 25, 2006