Category Archives: Castle Project

Dependency Injection with Castle Windsor

I’m writing a presentation on dependency injection with Castle Windsor to give at work this coming week. I’m going to present it at a lunch and learn for the software developers in our department. It should be interesting, I’m curious to see how it goes. If all goes well, it will be the first of a series that I’m going to put together.

Once I have the content and have run through it, I’ll post the samples and text here for everyone to enjoy. While it is introductory in terms of DI/IoC, it’s a fairly advanced topic compared to general programming topics. Should be fun!

Friday Ramble

Wow, today is going to be a long one. Last night, I went to see EOTO play. EOTO is a spin-off of the now defunct String Cheese Incident. It was a solid 2-hour techno show, but not the hard house or trance that I’ve heard so much about. It was more trip-hop or something like that, very cool stuff. The drummer played for two hours straight without a break — pretty wild.

The MacBook Pro continues to evolve. Post Fusion install I’ve gotten all my development tools installed, including Resharper, TestDriven.NET, TortoiseSVN, and NAnt. So far everything seems to be working great. I’ve yet to install PowerShell (so I can have my NIX command-line goodness for the Windows box). I pulled down the trunk of Castle and got it to build as well. I had to create the Test & Test2 databases on my local instance of SQL Server 2005 in order for all the tests to complete.

It looks like I’ll be giving a presentation on MonoRail at the TulsaTechFest 2007. It will be an introduction to building a web site using MonoRail and Brail. MonoRail is part of the Castle Project and provides a MVC (model-view-controller) framework within ASP.NET. MVC is a solid pattern for building web applications that is a significant improvement over the page-based WebForms model. MonoRail embraces web standards allows applications to be built based on controller logic and helps facilitate the separation of concerns. It’s good stuff, I can’t wait ot talk about it.

Did I mention that I’m old and staying out until 2 AM at a live show is painful the next day? Where is the Tylenol again? Strange that I can stay up until 3 AM playing with a new computer and not feel fried the next day but this was… different.

Oh yeah, Peggle Extreme is part of the pre-order package for TeamFortress2 on Steam. I highly recommend it as a fun diversion! Speaking of TF2, I’m all set for the beta that starts on Monday. I can’t wait to play the game, I hope for some cool in-game video recording (with FRAPS or otherwise) next week. I wonder if voice-chat is built into the game? Things I don’t know — they are plentiful today it seems.

I got my iPhone store credit, $100, fast and easy from Apple. I guess this means a trip to the Apple store is coming soon.

Oh, and today’s post is an experiment in link diarrhea to see how long it takes this post to go up!

Castle.Windsor and Generics

Don’t you just love great documentation? Hey, it’s a work in progress…

In digging to figure out how to use a generic data access interface with specific implementations through Castle.Windsor, I came up with the following.

In the configuration file, you specify something like this:

<component
id=”MyAssembly.MyObjectDataAccess”
service=”MyAssembly.IGenericDataAccess`2[[MyAssembly.MyObject, MyAssembly],[System.Int32]], MyAssembly”
type=”MyAssembly.GenericDataAccess`2[[MyAssembly.MyObject, MyAssembly],[System.Int32]], MyAssembly”>
</component>

Once you have that, you can request that type from the container by asking:

IGenericDataAccess<MyObject,int> dao = Container.Resolve<IGenericDataAccess<MyObject,int>>();

Of course, you could always override that implementation with a custom class for specialized extensions to the IGenericDataAccess interface. This seems to work pretty well, and avoids defining a class and interface for each and every data type in our system (there are a lot of them). I guess it would be cleaner to simply define the interface and class in code now that I think about it.

Another thing, if you are stuck trying to figure out the type name to put in the configuration, write a quick test that does the following:

GenericDataAccess<MyObject, int> dao = new GenericDataAccess<MyObject, int>();
string aqn = dao.GetType().AssemblyQualifiedName;

That will give you the full .NET style name for the class that you can put into the configuration.

Thoughts?