Thursday, June 17, 2010

IQueryable and Repositories

One approach for coding repositories that I have seen a couple of times recently is that of exposing IQueryable collections through the repository interfaces. On the one hand this allows an extremely convenient way of exposing a flexible data source to your business layer or controllers. However, it also breaks the Single Responsibility Principle thus creating less reusable and testable solutions.

By allowing your business layer to define queries within its own methods, those methods are taking on two responsibilities, performing data access and performing logic on the results of that data operation. It's all well and good to complain about not hitting some high ideal, but what affect does this have in the real world?

First, if you happen to reuse the same data query in multiple parts of your application, you would have multiple places that you would then need to make changes and test. DRY this isn't.

Second is the testability of this code. In the terms of this afore mentioned white paper, you would need to write your tests for your business layer or controller in such a way that the state is correct as well as the interactions, for each method. As the paper says, testing the state aspect requires a larger set of data. Why would you want to write that extra code for every method in your business layer / controller that uses the same query? If you don't test each one, how do you know they work? How do you know when they break?

The simple solution is to keep your queries in the repositories behind the repository interfaces. Test the state of each one thoroughly and then you only need to test the interactions in your business layer. Yes it requires an extra function call in your business layer, another method in your repository interface and another function declaration in the repository. And while that might be a lot of words, it's only four or five more lines of code for each query (including brackets on their own line in C#.) And if you reuse your queries, you get those extra lines back anyway.

I will certainly concede that no one paradigm fits all of the software problems out there. Maybe what I suggest is overkill. Maybe it's another example of a programmer too set in his ways. But by exposing IQueryable collections through the repository interface, we're ignoring a number of the principles of coding that have been making our lives easier and our code better for a while now.

After a second read through of the article, K. specifically states that enumerables could be returned instead of queryables. Not that this is the only publication I have read where queryables were suggested. Let's just say that now all four of my regular readers know where I stand on the subject.


This white paper by K. Scott Allen is a great read and was my formal introduction to the Unit-Of-Work concept.

For a while, the recommended approach for providing data contexts was to create the context and store it in a session of some sort. Each call to your server would provide a single data context for all of the logic needed to render a new page or perform some sort of data operation.

UnitOfWork might not be a name that explains exactly what the object is (a data context container). But, it does describe what the object does. It frames a the data context within a single logical unit of work. The distinction being that it may take many logical routines to render a single web page.

One advantage to the technique is that you are committing data changes at the end of each logical routine. This means that many of the data changes for that routine can all be committed at once using fewer network and database resources over a shorter period of time.

Another advantage is that you are holding the data necessary for each operation in memory for only as long as it is needed. When the operation is done and the data is likely no longer needed, the memory is flushed and reused for other operations.

Lastly (that I'll mention anyway), it separates your data context scope from session scope. The session will likely be different in web, windows and service applications. This means that your session across different applications may comprise a different number of business methods which can have unforeseen affects on performance and functionality. Instead, you can tie the scope of your data context to each business method as those make good candidates for a unit of work. This in turn means that your routines will perform more predictably across different types of applications.

Thursday, June 10, 2010

Backgrounds, Google? Really?

If you got to this post, you're probably wondering how to turn off Google backgrounds. I for one am pretty sure that if there isn't already a setting, there will be one shortly. I can't be the only person that want's my plain white, pleasant and unobtrusive background back. I'm also far too lazy to go look for that setting given that a link to it doesn't exist on the front page.

That being the case, I created some 800 x 600 solid color backgrounds and uploaded them to Picasa. 800 x 600 by the way is the minimum resolution that can be used. Yes, I do like the fact that Google is hosting my solution to my Google problem.
Download early, download often. Here's the links:
I recommend the dark gray though the white does give a good 'White Album' feel. Obviously, you can also easily create your own image using MS Paint or any simple image editor if you prefer, say, color. Click on the 'Change background image' link in the lower left hand of the Google front page and upload the file from your computer.

Fine, this didn't take much imagination or skill. But I was so dumbstruck when I saw the page, it was all I could do to listen to various suggestions from coworkers.

In retrospect, this may have just been really good marketing. Google did seem to have a fix in place fairly quickly. I'm sure ours wasn't the only office where the new background had everyone talking for a few minutes. And who knows how many bloggers out there were duped into creating buzz about it.