Finders with DetachedCriteria
December 17th 2007, 3:57 am in .NET, Active Record, C#, Castle, Patterns.
I should file this under “things that are probably a design pattern but I’m not sure exactly what kind”; it seems like one of those typical things that you will naturally stumble upon when using the Repository pattern and indeed when you start to separate out your query logic.
DetachedCriteria is an “offline” version of the NHibernate Criteria class, used to build up Criteria where an NHibernate Session is not available. The NHibernate documentation has more on how to use DetachedCriteria on its own, but I want to talk about inheriting from it to create a simple custom finder class.
Say we have a Active Record Person class, with height and weight properties:
[ActiveRecord]
public class Person : ActiveRecordBase
{
[PrimaryKey]
public int Id { get; set; }
[Property]
public int Height { get; set; }
[Property]
public int Weight { get; set; }
}
The height and weight properties are simplified, and I’m using C# 3.0 automatic properties for clarity. I want to search for a Person based on their height and weight, so to do so, I’ll create a new finder class:
public class PersonFinder : DetachedCriteria
{
public class PersonFinder(height, weight) : base(typeof(Person))
{
Add(Expression.Eq("Height", height));
Add(Expression.Eq("Weight", weight));
}
}
A simple class, but it clearly states its purpose. Here’s how it could be used:
Person.FindAll(new PersonFinder(200, 300), new Order("Id", true));
For me, the advantage of this code isn’t just in reducing what you have to type. Instead it is in the readability and discoverability of the code.
I guess this applies to any IoC container, but you get the idea. How would you describe Windsor to a newcomer in just one sentence? I just wrote:
Windsor is a way of passing in different classes to code that is going to use them, it helps you write applications that are very flexible and testable.
But that was pretty off the cuff and I’m not really satisfied with it. Give it a go, it’s pretty hard.
Just a quick one: I have a method in my controller which is about 30 lines long, and also calls another private method in that class which is about 15 lines long. The main method is 50% comments. The private method is only that long due to the formatting of parameters in a constructor and was extracted from the main method for readability; it’s not reused. So my question to you is: when does excessive method length start to become a code smell? Does this situation sound like it’s ripe for refactoring? What sort of thing makes you shudder when you open a class file?
When I installed Yellow Dog Linux on my PS3 the other week, I was really impressed with one of the window managers they make available, E17, the latest version of Enlightenment. I wanted to test it out further on my PC, so after trying out a live CD with it on in a virtual machine in Windows, I eventually decided to test it with some applications I’d actually like to use. Here’s the tale of how Ubuntu might actually make me switch from Windows.
(more…)
I had a change request today to label a couple of text input fields with a watermark. The client didn’t think it was immediately obvious what the user was supposed to enter, but we didn’t have space for the traditional text labels next to the inputs. The suggested solution was to have a watermark which "underlayed" the input box with the label text which disappears on focus and reappears on blur.
Jason alerted me to an SVN commit by Hammett which begins to introduce a new style of routing support into Monorail. The commit message says that he "Added RoutingEngine, initial work to create a cooler routing support inspired on MS MVC", and by looking at the test case for this new engine, it does indeed look cooler.
To add a rule:
routingEngine.Add(PatternRule.Build("ProductByName", "product/<name>", typeof(ProductController), "View"));
Which will match the URL http://www.domain.com/product/iPod and send it to the view action on the product controller, passing in "iPod" as the name parameter. It’s also naming the route as "ProductByName" but I’m not sure of the application of this right now…
The thing I like about this approach is that it’s a lot more readable than the current routing engine. It’s not going to set the world on fire but this, coupled with RC3′s new UrlBuilder, will make for a pain-free means of manipulating your URLs.
Technology Brainstorm
October 10th 2007, 12:35 am in Agile Development, Castle, Monorail, Personal, TDD, Windsor.
Recently Plastiscenic has been asked to quote for a fairly large project, and if it comes off I’ll have the chance to work on it from the ground up, using whatever technologies I think are best suited.
I love it when a plan comes together (a tale of Monorail, ActiveRecord and ExtJS)
October 8th 2007, 11:10 pm in .NET, Ajax, C#, Castle, Javascript, JSON, Monorail, Screencasts.
I’ve blogged before about the enabling features of Monorail and Active Record, part of the Castle stack. Another piece of software which is making my development life a pleasure is ExtJS – a collection of javascript goodies created by Jack Slocum and his team – and when these three things come together, watch that coding fly…. I’ve created a screencast in which I demonstrate how easy it is to build a paging, sorting, filtering, AJAX-powered grid using ExtJS and Castle, and you can watch it here. It’s 20 minutes long but filled with goodness – and a couple of hacks (forgive me
. Don’t forget to check out all my past screencasts too.
UPDATE: As requested in the comments, you can MonorailExt1 VS Project directory of this.
For a few months the term “ALT.NET” has been gaining traction on the blogs I read, starting from a post by David Laribee in April. From there it has taken off, with an ALT.NET Conference taking place this week. So it’s perhaps timely that Sam Gentile has written a post entitled “Goodbye CodeBetter and ALT.NET” which talks about the ALT.NET movement and some of the people surrounding it.
ALT.NET is Divisive
ALT.NET is a divisive thing. No matter what they tell you.
So says Sam, and I’m in agreement. The ALT.NET conference is so cliquey that it hurts, and that’s really highlighted by an interesting bit of news that came out yesterday regarding Microsoft’s upcoming MVC framework. I think it’s interesting that they’d talk about it at a conference where most of the people will already be using this sort of technology. Just as I find it pointless that you’d go to a conference to discuss the stuff that everyone there already knows. DDD, BDD, MVC – these aren’t things that will be unknown to people attending the ALT.NET conference.
ALT.NETter A: so… heard of that new BDD business?
ALT.NETter B: Yep.
ALT.NETter A: Oh, well there’s this great new idea called DDD!
ALT.NETter B: Yeah, right into that too.
ALT.NETter A: Oh.
And so on. But wouldn’t that conversation have been a whole lot more interesting if the second developer hadn’t heard of those ideas?
The View from on High
The ALT.NET conference is barely about the underlying technologies or ideas. It mainly serves to allow all of the ALT.NET developers to come together to talk about how great it is that they are all ALT.NET. Us, and them. Back to Sam:
I am not going to use the term “Morts.”
Good, because it’s an offensive term, but it’s one that’s being thrown around by the ALT.NET gang with abandon. Scott Hanselman, a great blogger, had this to say from the conference:
Mort is crying because mommy and daddy are fighting.
That’s the kind of patronising way in which the term is being used; I mean come on, the damn thing is short for “mortal” which is a pretty clear indication of how the people who use “mort” think of themselves: Gods of their domain. Overseers of the lower developers who haven’t had the resources, or the guidance, to work with the fantastic array of non-Microsoft technologies that exist.
And in order to address that imbalance, the ALT.NET mob organise an enormous desert circle-jerk, and I call bullshit. If they really wanted to change things then they should be writing about their techniques in detail, coming up with introductory guides to DDD, TDD, mocking, creating screencasts, or giving talks at mainstream conferences, or producing tools to make the level of entry to these technologies lower than it is.
Abandon ALT.NET – Before it Goes Too Far
A lot of people in the ALT.NET camp may not feel that they’re being divisive but I can tell you that by propagating this idea you’re creating a line in the sand, with the highly-knowledgable on one side and the masses on the other. You’re not encouraging dissemination of your information, you’re just creating another impenetrable gang of developers that is almost opaque to the guy on the ground.
UPDATE: I’ve updated this post on my LosTechies blog – see Why ALT.NET?
The title of this post sounds like an advert, but I’m eager to share the ease with which I’ve been able to bend the PS3 to meet my needs. Previously I talked about faffing around with Yellow Dog Linux to play High-Definition media, such as Prison Break downloads, but the solution to the problem doesn’t require any kind of PS3-based trickery.
Instead, the real work is centered on the PC. Using utorrent and various torrent sites, it’s pretty easy to download the shows I watch. Heroes, Prison Break, Smallville, Stargate Atlantis at the moment, and soon BSG and Lost (though last series was 80% tosh) can all be scheduled to download within half a day of their airing (less if I don’t want to saturate my connection), so that’s the simple bit. The shows which are airing in HD have started to be released in 720p .mkv files, and though the PS3 can’t play those natively, there’s a trick to them which is key to this whole idea.
Redkawa’s PS3 Video 9 has recently introduced a feature which allows you to unpack then repack .mkv files to .mp4 files – a format which the PS3 can play natively. And unlike transcoding, which is the normal means of converting from one video format to another, this unpack-repack process, which they call “remux”ing, is quick and doesn’t result in a loss in quality.
So after downloading the .mkv, I pass it through PS3 Video 9 and I have a file which my PS3 can play. From there I can copy it to a flash card and put that straight into the PS3. However, I don’t have a flash card big enough (these files are just over a gig) so I need a way of copying straight to the PS3.
TwonkyMedia‘s server allows you to set up a directory on your PC to be shared with your PS3. Thanks to a recent firmware upgrade on the PS3 side, when you run TwonkyMedia, you’ll see it show up in the GameOS screen, and from there you can stream files across your network and play it on your PS3. My network’s not fast enough, but you’re also able to copy the file from the PC to the PS3 hard disk via TwonkyMedia and play it that way.
This could be streamlined but for the satisfaction-factor of watching HD content on my shiny 42 inch screen the day after it airs in the US, it can’t be beaten.