This morning, Fabio released the official NH 3 Alpha 1 binaries and source on SourceForge. Go play!
Oh, yeah. Frist!!!!!1!!!!one!!
This morning, Fabio released the official NH 3 Alpha 1 binaries and source on SourceForge. Go play!
Oh, yeah. Frist!!!!!1!!!!one!!
Yesterday, I spent an embarrassing amount of time searching for a bug. I’m sure this is well-documented somewhere on MSDN. It even generates a compiler warning in some cases. Still, it’s not the behavior one would expect from C#.
When creating lambdas (including LINQ expressions) inside a for each loop, don’t use the iterator variable in your lambda. Let me explain with some code:
foreach (Type controllerType in controllerTypes)
{
kernel.Bind(controllerType).ToSelf().InRequestScope();
kernel.Bind<IController>().ToMethod(ctx => ctx.kernel.Get(controllerType)).Named(GetName(controllerType));
}
Why am I using a method to get the controller? It just so happens that my AccountController is also a domain event handler for my AccountNameAlreadyUsed event. This goes back to Ayende’s tip in my domain events post: To get a message back to the UI, fire a new event and have the UI listen for it. In this case, I need the UI to complain when the account name they selected is already being used.
In case your mind has wandered to the dark side, throwing exceptions is not an acceptable way of passing messages in an application.
Now, why the odd mappings? Let’s say I bind IController and IHandle<AccountNameAlreadyUsed> to AccountController in the request scope. It doesn’t quite work like you would first expect. You will have one instance of AccountController returned for IController and a separate instance for IHandle<AccountNameAlreadyUsed>.
Instead, I’m saying that for each request for an IController, go get an AccountController, essentially delegating the request to the ToSelf() binding. I have a similar ToMethod() lambda binding for IHandle<AccountNameAlreadyUsed>. Since both interface bindings are fulfilled by the ToSelf binding, only one instance of AccountController is created for the request, instead of two.
So, this explains why I need the lambda in the first place. Why didn’t the code above work?
As it turns out, there was some funny business going on underneath the covers between the foreach iterator and the lambda. Here’s the symptom: No matter which “instance” of the lambda was being referenced, inside the lambda, the iterator variable (controllerType) was always the first value that was iterated. No matter which controller I requested, I always got an instance of AccountController, since it happens to be first alphabetically.
Once you realize what’s going on, the fix is simple. Create another variable and use it inside the lambda instead. So, instead of the code above, we get this:
foreach (Type controllerType in controllerTypes)
{
var controllerType2 = controllerType;
kernel.Bind(controllerType).ToSelf().InRequestScope();
kernel.Bind<IController>().ToMethod(ctx => ctx.kernel.Get(controllerType2)).Named(GetName(controllerType));
}
If, instead of a lambda, we had a LINQ expression, the compiler would generate a warning about this issue – at least in VB.NET. If I hadn’t already seen that warning from LINQ expressions, I would probably still be bug hunting.
Jason
You may have noticed a lack of VB.NET source code on my blog recently. There’s a reason. I’ve always been able to read and write C#. at least enough to order off the menu or ask for directions to the Men’s room.
The ALT.NET crowd is almost exclusively C#, except where they’ve moved beyond it. If I’m going to participate, it’s past time for me to become fluent. Until at least April, I won’t write a single line of VB.NET outside of work.
Tags: C#
Recently, I was asked to provide feedback on someone’s writing about a specific technical subject. Their work is targeted at average .NET programmers who are new to this particular subject – much like I was to NHibernate just a few months ago. By the time I had finished the 2nd chapter, I was concerned to the point I considered withdrawing from the project. There were some obvious technical flaws, and the whole thing generally rubbed me the wrong way. My feedback was pretty harsh.
A week later, I realized my real objections were over the author’s process. He did things exactly backwards from the order I typically follow. He did things in this order because that’s probably how he works, but more importantly because it’s easier to explain. From the other end, he’d have to assume some knowledge that the reader may not have.
So as not to sink in to the meta-blogging quicksand, I’ll simply ask these questions about the series up to this point:
Please comment or send me an email with your feedback.
Today, I started the world’s newest Micro-ISV. It may be several months before I share any details, but the journey started today.
Horn is an open source project used to build other often-interdependent open source projects, such as NHibernate, Fluent NHibernate, NHContrib, the Castle suite of projects, MVCContrib, Rhino Tools, and Ninject.
This solves a couple of very common problems when working with the average ALT.NET stack.
When I published the first part of my series about setting up the solution, there was an issue where the assembly for Ninject’s ASP.NET MVC integration was only available from the trunk. I posted in the comments that the readers could either wait for me to post the source, including all the 3rd party libraries, or they could grab the source from the trunk and build it themselves.
The next day, I got a series of tweets from Tuna (@tehlike) and @dagda1 about Horn, so I read up on it. It looked cool, but I had just spent the afternoon updating everything and writing about it, so I was a bit burned out. Now that it’s been two weeks, there are a couple of projects in the stack that need to be updated. FNH released v1 RC (Congratulations James!) and Ninject and MVCContrib also have some updates.
I also have two medium sized web apps at work that should be updated to the latest and greatest when we move them from a VPC to a physical server next month. I’m hoping Horn will save me from .NET’s version of DLL HELL.
If you want more info, check out the discussion group, or just grab the source code. Horn even has its own contrib project.
Here are my thoughts:
Very first impression – It’s an awesome idea, but it needs a lot more SEO. To find it, I had to search in Google code. Hopefully, this post will help with that. If you’ve got a blog (and you should) and you use Horn, write about it. It deserves great press.
I grabbed the latest source from their Google Code SVN. In the /src directory, there’s a hornbuild.bat. I fired up a command prompt, changed directories and ran it. 20 seconds later, I had it – /src/build/net-3.5/debug/horn.exe.
From there, I started grabbing everything I needed
horn –install:fluentnhibernate horn -install:ninject horn -install:mvccontrib
Well, that’s what I had planned. I got a BadImageException on SharpSvn when trying to grab FNH. After trying a few things on my own, I tweeted for help. Tuna, who I’ve decided is either stalking me or involved in every .NET open source project out there, suggested checking the assemblies with the corflags utility. If you’re new to corflags, like I was, it’s in C:\Program Files\Microsoft SDKs\Windows\v6.0A\Bin and part of the Windows SDK. As it turns out, horn.exe and horn.core.dll in my particular build were not marked as 32-bit. I marked them both as 32-bit with these commands:
corflags horn.exe /32BIT+ corflags horn.core.dll /32BIT+
I tried it again. Success! It started by grabbing the castle project, then built it. Then it moved on the NHibernate, and finally Fluent NHibernate.
In the end, the only project out of the three that didn’t work 100% was Ninject. The build for it’s monorail integration had an issue signing the assembly. Since I’m not using monorail, much less Ninject’s monorail integration, I commented that out of the .build file and tried it again. It worked perfectly.
Horn is still a little rough around the edges. For example, when a build fails, horn crashes out to windows error reporting. Even with those issues, it has saved me a lot of time. It’s a great idea and even at this early stage, it’s an amazing tool.
Thanks again to Tuna for answering my stupid questions.
The 5th post in the ASP.NET MVC / NHibernate series, Fixing the Broken Stuff, will be out Friday night.
Today, a coworker asked me about my living room setup. I figured it would be a nice change from the ASP.NET MVC / NHibernate series. It’s a little unorganized and rushed. I apologize.
I hate remotes. There is absolutely no reason to have more than one remote control. I also hate looking at wires and equipment with blinking lights in my living room.
Here’s what I did to fix the problem.
Just on the other side of the wall from my LCD TV is a large walk-in closet.
In the living room, in addition to my Sharp Aquos LCD TV, I also have Energy brand 5.1 surround speakers and a Logitech Squeezebox (now referred to as Squeezebox Classic) network music device. In the closet, I have my laptop, external SATA hard drive, Onkyo TX-SR706 receiver, and AT&T U-Verse set top box.
If you don’t know what you’re doing, you could burn down your house, die or worse. Hire a professional. Also, if you live inside city limits, there’s probably some law or ordinance about having the work done or inspected by a licensed electrician.
Problem #1 – The walk in closet didn’t have any electrical outlets. Luckily, there’s one in the living room on the same wall. I ran a few feet of spare 12 gauge home electrical wire from the outlet in the living room to a new outlet in the closet. Be sure you know which side of the wall stud the outlet is on. You can’t easily run wire through a wall stud without destroying your drywall.
So, I added an outlet in the closet. Problem solved.
Problem #2 – Your standard home electronics power cord is not rated for in-wall use, or at least that’s what I read on the internet. I didn’t want a power cord hanging from my TV running down to an outlet. I needed to add an electrical outlet behind my LCD TV, which was mounted on the wall. I also wanted some sort of surge protection.
I went shopping online for something that would solve this problem and ran across this recessed low voltage wall plate with recessed power at Monoprice.com.

It’s actually a kit designed specifically for this problem. There are two wall plates included in the package. The one shown in the picture goes behind the LCD. It has a single electrical outlet plus a channel for low-voltage cables, such as speaker and HDMI cables. The other wall plate goes in the closet and is identical except the channel feeds down and the outlet is male. You provide two single electrical junction boxes – one for each of the electrical outlets. Between the two, use standard in-wall rated 12 gauge electrical wire.
This is not directly wired in to the home’s electrical. The two wall boxes are wired to each other only. You connect the male outlet to a surge protector, which is plugged in to a standard electrical outlet. Then, just plug your LCD in to the female side. You have powered your LCD, protected it from surges and lightning strikes, hidden the power cable behind the TV, and made a path through the wall for your low-voltage cables.
I had help with this part. My little brother ran the speaker wires through the attic and down the walls while I pulled from below and connected everything.
We ran 14 gauge speaker wire to the surround speakers and the subwoofer. Since the subwoofer is powered, it uses an RCA cable. I soldered a male RCA connector to each end of the subwoofer speaker cable. For the speakers, I used standard banana plug speaker wall plates.
We also pulled some coax cable in to the closet and connected it in the attic to the rest of the cable TV lines with a splitter. .
The left, right, and center speaker cables run through the low-voltage side of the wall plate behind the TV.
I found some cheap plastic speaker mounts on sale at Best Buy. You get what you pay for. The package said they were rated for up to 25 pounds. I seriously doubt these things could support that kind of weight, but they worked for my surround and left/right speakers. The center speaker is a little heavier, so it just sits on a shelf below the TV.
For the TV, I bought a generic LCD TV wall mount at Fry’s electronics. It doesn’t tilt or pivot or any of that mess. It just holds the TV on to the wall, which is just fine with me.
The laptop and the U-Verse set top box are connected to the receiver with HDMI cables. The Squeezebox is audio only and is connected with a single digital RCA cable. The receiver is also connected to the TV with an HDMI cable.
Both the TV and the receiver have RS232 9-pin serial management ports you can use to control the devices. The receiver also allows you to query its state – is it turned on, what’s the volume set at, what input is active, etc.
Just a note: You will need a null modem cable when connecting to a Sharp Aquos TV and a standard straight through serial cable when connecting to Onkyo brand equipment. Also, there is at least one person in the hardware department at the Webster, TX Fry’s Electronics who still knows what a null modem cable is. That is not the case with any of the area Best Buy or Radio Shack stores. ![]()
Since like most modern PCs, my laptop didn’t have any serial ports, I bought some USB serial ports from Amazon. Be sure to read the fine print and the comments. Most of these don’t come with drivers for Vista at all. I only found one that would work with Vista x64, and the install was a little hackish.
The only way to control the U-Verse box is with an infrared remote. It doesn’t have a serial port, the USB port is disabled, and even though it’s running WinCE, I couldn’t find a way in through the network.
I bought a USB UIRT and some stick-on infrared blasters off the internet to control it. There are all sorts of internet posts explaining where the IR receiver is located inside the U-Verse box. This is what worked for me.
That’s almost all of the hardware. I also have an RF keyboard, RF optical mouse, and an iPod touch that I use as my one and only remote control.
All of the software is written in Visual Basic.NET with ASP.NET MVC and iUI, a javascript library for making web applications look and behave like native iPhone apps.
All of the device control is done with macros and commands stored in an XML file. For example, each button on the U-Verse remote is mapped to an array of 32-bit integers stored as hex strings. These hex strings represent the actual IR commands that are sent to the USB UIRT to control the U-Verse box.
TV guide information is downloaded once a week from the internet using XMLTV and stored in a SQL Server database on the laptop.
Here are some pictures of the front-end on the iPod touch and the setup in general.
Send me an email if you would like a copy of the xml file with the UVerse remote codes.
Or so says Windows Media Player. This made me laugh.
Polkastra’s new album, Apolkalypse Now, with special guest William Barton on Didgeridoo.
Tags: Music
In part 4 of the series on ASP.NET MVC and NHibernate, we’ll be using NUnit. If you don’t already have some snazzy Visual Studio add-in with full NUnit integration, here’s a not-so-glitzy alternative. It’s not my idea and I’d love to give credit to the right person, but I’ve slept a lot since I first discovered this.
It should look something like this:
Now, when you want to fire up NUnit, just choose it from the Tools menu. It’ll launch, load up the latest debug build of the current project, and start running the tests.
Of course, if you’re doing any kind of significant real-world project, especially with a team, you should really look in to snagging one of the awesome continuous integration (CI) setups out there. Every time you commit, it’ll build and test your app, send you the results, and with a little tweaking, even start a fresh pot of coffee. Oh. You are using some kind of source control, right? And it’s not Visual Source Safe, right?
Jason
- On a blogging high
You are currently browsing the archives for the Uncategorized category.
hhariri: Tried using a desktop client again for mail for a month but no good. GMail has me too spoiled
jarrettmeyer: Using SQL Server Timestamps with Fluent NHibernate AutoMapping - Problem: You’ve got timestamp columns on... http://tumblr.com/xsoeocg2o
Arclite theme by digitalnature | powered by WordPress