New drop posted - Jan 6 2010

Jan 6, 2010 at 6:52 PM

Happy new year everyone!

I've just posted the latest drop of Unity 2.0. The major new stuff in this drop it:

Injection Factory improvements:

When using InjectionFactory, you can now pass in a delegate that gets the currently being resolved type and name as well as the container:

new InjectionFactory(container, type, name) => { construct object here })

if you don't care about the type and name, the previous signature (container only) continues to work. This delegate signature is only supported through InjectionFactory, we have not updated the old StaticFactoryExtension API (that one's deprecated anyway).

Automatic Factories:

This one is simple but powerful. Now you can resolve a Func<T> out of the container (or simply have a dependency on Func<T>). When you invoke that func, it calls back into the container to do a Resolve<T>. This makes it simple to get objects out of the container later without having to inject the entire container.

If you resolve the Func with a name, then the later Resolve call will use that name to resolve.

And finally, if you resolve a Func<IEnumerable<T>>, invoking the func will call ResolveAll<T> on the container.

Namespace and Assembly Searching in Configuration:

This one reduces the need for explicit aliases in configuration files. You can specify a set of namespaces and assemblies in your unity configuration section:

<namespace name="My.App.Stuff" />
<namespace name="My.App.Stuff.More" />

<assembly name="My.App" />
<assembly name="Extra.Stuff" />

When the config system hits a name that it can't load as a type and isn't an alias, it will search through the combination of assemblies and aliases. With the above lines, all types in My.App and Extra.Stuff in those two namespaces are now available in the config file using just their simple type name.

Aliases do not use the namespace and assembly search, so when you define an alias you still need to give the fully qualified type name.


Simplified Syntax for Generic Types in Configuration:

Previously, generic types didn't work with the alias system - you had to alias the entire type name (including the type parameters) or none of them. The CLR syntax for generic types is also pretty awkward, to say the least.

Generic type resolution is now tied into the alias and namespace/assembly search mechanism. So with the appropriate namespace & assembly references, you can now do this:

<register type="Dictionary[int, string]">

Simple and straightforward. Aliases and searching works not just on the generic type name itself, but also on the type parameters. Also, note you didn't have to put the "`2" in there - the configuration figures that out now. Simlarly, if you want to specify an open generic type, you can use the CLR "`N" syntax, or you can do:

<register type="Dictionary[,]">

to specify an open type, in this case with two type parameters. You cannot specify partially closed types with this - you need to give either all the type parameters or none of them (the CLR doesn't let you do that, so we don't either.)

Please give it a try and let us know what you think!


Jan 15, 2010 at 4:56 PM


Were you planning on providing any suport for Lazy<T> and Lazy<T,TMetadata> in .NET 4?




Jan 16, 2010 at 6:04 AM

Not out of the box. We can't take a dependency on .NET 4.0 in Unity 2.0.