Latest drop posted: 12/15

Dec 15, 2009 at 7:22 PM
Edited Dec 16, 2009 at 5:22 AM

(Sorry the drop's a little late, holiday and then a nasty cold slowed me down).

Hey folks, I just posted our latest Unity 2.0 drop to source control. New this time is the single most voted Codeplex issue: the container.IsRegistered method. Yes, you can now ask Unity what's registered in the container.

There are two parts to the API. First is the Registrations property. This returns an IEnumerable<ContainerRegistration>, which means it works with Linq, which means you can, for example, do this to get all the names registered for a particular interface:

 

from reg in container.Registrations
    where reg.RegisteredType == typeof(IFoo)
    select reg.Name;


(Name here includes the default, by the way, so expect a null in there). You can get the registered type, what it's mapped to, the name, and the type of lifetime manager for that registration. Other information could be made available (with a lot of work) but I didn't want to do that work until people presented some use cases where it would be useful. So if there's something else you'd like to be able to query (and you have a need for it, not just logical completeness) please let us know.

Also, there's extension methods to make the simple IsRegistered check simple:

 

container.IsRegistered<IFoo>("someName");

Second, we've started (and almost finished) a complete rewrite of the Unity XML config system. It's mostly backwards compatible, but we've removed a ton of redundancy and extra XML to get something that should be a lot less pain to write.

As an example, before to register a type with a constructor and a couple of properties, you'd write this:

 

<unity>
<typeAliases>
<alias alias="int" type="System.Int32, mscorlib" />
<alias alias="string" type="System.String, mscorlib" />
<alias alias="object" type="System.Object, mscorlib" />
<alias alias="IFoo" type="MyProject.IFoo, MyProject" />
<alias alias="Foo" type="MyProject.Foo, MyProject" />
</typeAliases>

<containers>
<container>
<types>
<type type="IFoo" mapTo="Foo">
<typeConfig>
<constructor>
<param name="index" type="int">
<value value="17" />
</param>
</constructor>
<property name="lock" type="object">
<dependency />
</property>
</typeConfig>
</type>
</types>
<container>
</containers>
</unity>

 

 

 

You can now write this instead:

 

<unity>
<alias alias="IFoo" type="MyProject.IFoo, MyProject" />
<alias alias="Foo" type="MyProject.Foo, MyProject" />

<container>
<register type="IFoo" mapTo="Foo">
<constructor>
<index value="17" />
</constructor>
<property name="lock"      />
</register>
<container>
</unity>

Lots of changes here. First off, extraneous wrapping elements (typeAliases, containers, types, typeConfig) are now all gone. You don't need to explicitly alias common types anymore - all the types that have language keywords in C# and VB are automatically aliased for you. Parameter and property elements don't need to give the type of the parameter or property anymore, the name is sufficient (unless it's needed for method overload resolution), and you can use the name of the property directly as the element name. If you're giving a value for a parameter or  property, and it's a simple one, you can specify it using an attribute, instead of another nested element.

Finally (although I don't have time to demonstrate it here) the configuration extension stuff has changed radically. No longer will you need to specify extensionType="really long type name" everywhere to configure interception, for example.

Please check the configuration and interception.configuration unit tests for lots of examples of the new configuration schema. The interception configuration isn't quite done yet, but it's usable now.

Please take a look at the new stuff and give feedback!

Thanks,

-Chris

 

 

Dec 15, 2009 at 11:52 PM

Oh, one thing I forgot to mention - this drop includes a change to the configuration API. Previously, you did:

section.Containers[name].Configure(container);

In this version, you do:

section.Configure(container, name);

or, for the default container, you can just do:

section.Configure(container);

The old API is not supported in this drop, but it'll be back (and marked obsolete) in the next one.

Also, there's now an extension method provided on IUnityContainer, so you can do:

container.LoadConfiguration()

to grab the default container from the default configuration section, along with associated overloads.