With Windsor 2.1.1 out the door, we’re already working on version 3.0 (and looking for your feedback and ideas for the next version!). Yesterday I committed first “big” feature that probably will make it to the release.
All the code shown here represents a work in progress. Keep in mind that final version may be different. This also means that any feedback on this feature is appreciated.
In current version of Windsor you can define inline dependencies for a component in at least three different ways:
- When registering it (with value obtained at registration time):
- When registering it (with value obtained at resolution time), AKA – Dynamic Parameters
.DynamicParameters((k, d) =>
d["timeStamp"] = DateTime.Now;
d["user"] = Thread.CurrentPrincipal.Identity;
- When resolving it (although this is something you should strive to avoid, using constructor injection and/or Typed factory facility)
var component = container.Resolve<ClassWithArguments>(new
TimeStamp = DateTime.Now,
User = Thread.CurrentPrincipal.Identity
What all these approaches have in common is that you specify dependency by name. Windsor when resolving the component will then use the value you provided for constructor arguments or settable properties with that name it finds on the component. What if you don’t care about the name?
Trunk version of Windsor lets you do just that – omit the name in which case Windsor will use the type of the argument as the sole information when building your component.
As you can see here we use the type as the key for the dependency. This works also with dynamic parameters as well as when passing arguments straight from the call site. A new class – Arguments has been introduced to accommodate this new feature (although ordinal Hashtable or Dictionary<object,object> will do just fine as well) .
new Arguments(new object
The Arguments class itself implements IDictionary and can wrap either another IDictionary containing either named or typed arguments, anonymous type containing named arguments or array of objects to be used as typed arguments. It is advised to use this type when you plan to use typed arguments.
Additionally to help you deal with filling the dictionaries in more usable fashion an extension method on IDictionary (so they work for Arguments type as well) called Insert (with few overloads) has been introduced. It lets you insert arguments in a fluent, more user friendly fashion.
.DynamicParameters((k, d) =>
While this may be appealing at first to use typed arguments everywhere, resist the temptation unless you have a good reason. Named arguments should always be your first choice. Name is a valuable contextual information that may improve the readability of your code. Windsor will favor named arguments over typed and use the latter only if it can’t find a matching named argument.
02-15-2010 6:09 PM