IDependencyResolverPolicy in Unity 2.0

Jan 22, 2010 at 4:45 PM

I guess maybe I'm being dense, but I can't figure out how to implement a dependency resolver policy in 2.0. I had created a family of 4 classes based on the "OptionalDependency" example in the old ExtensibilityDemos for getting dependency values out of appSettings. It worked fine on 1.2. However, in 2.0, InjectionParameterValueElement is gone, and I can't see a replacement.

I'm sure I'm missing something simple, but what is it?


Jan 23, 2010 at 12:04 AM

The equivalent in 2.0 is ParameterValueElement. Is the config the only thing you're getting stuck on or are there other areas that are confusing? I should be able to clear it up. The way you extend the unity config schema has changed radically - for an example, look at the new interception config parts. The important thing to note is the SectionExtension class.

Config is under pretty heavy development as well, so things might churn a little bit depending on which drop you're looking at. Should be pretty solid by the end of this iteration (end of the month).


Jan 25, 2010 at 7:42 PM

Thanks Charles. As you suggest, that seems to not really be my problem. I am able to make my code compile using ParameterValueElement, but it doesn't actually work when I try and use the custom element in the config file (as I did in 4.1). I don't have time, though, to investigate SectionExtension right now, so I wouldn't want to waste your time with questions I can't follow up with. I'll be back to it in a month or so, and if I can't figure it out then, I will post again.

An un-related question, but I was wondering if the new configuration system will include an "offical" XML schema, preferably with a target namespace, to give the file Intellisense in Visual Studio? I know there is something in Contrib (for the 4.1 schema), but it seems a little incomplete.



Jan 25, 2010 at 10:51 PM

It's Chris, actually, not Charles. But that's ok, I forgive you. :-)

Fair enough. You will need to write a SectionExtension class to hook in your new element to the schema, and then reference it from your config file. It's pretty straightforward, and looks much, MUCH cleaner than the old way, so we consider it a net positive.

And we wrote an XSD for the new schema last week. Should be in the next drop. Haven't quite decided on the XML namespace issue yet. There are a couple of things we did in the config file that aren't representable in XSD, so it's not perfect, but it should move a fair way along.


Feb 9, 2010 at 11:29 PM

Hi Chris (a thousand pardons for my previous mistake),

I got it to work with the new beta 1 release. I can put this in my App.Config:

    <sectionExtension type="MyCompany.Unity.AppSettingsSectionExtension, UnityTest" />
      <register type="MyCompany.Unity.INumber, UnityTest" mapTo="MyCompany.Unity.MyObject, UnityTest">
        <property name="Number" type="System.Int32">
          <appSetting key="myNumber" />

    <add key="myNumber" value="31" />
    <add key="mySecondNumber" value="7" />

And the "Number" property on the resolved MyObject instance gets initialized to 31, as expected. However, I also have an attribute class defined like this:

    [AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Property, AllowMultiple = false)]
    public class AppSettingsDependencyAttribute : DependencyResolutionAttribute
        public AppSettingsDependencyAttribute(string key)
            : base()
            this.key = key;

        public override IDependencyResolverPolicy CreateResolver(Type typeToResolve)
            return new AppSettingsDependencyResolver(typeToResolve, key);

        private string key;

When I set it on the "Number2" property in MyObject, it doesn't get set. In fact, the attribute constructor never gets called. Is there something I need to do to turn DependencyResolutionAttribute's on? (The returned "AppSettingsDependencyResolver" is the same one wired into the configuration elements).

Quite happy with the beta 1, incidentally.


Feb 10, 2010 at 5:13 AM

No, you shouldn't need to do anything special. Is there other configuration for properties on MyObject? if you configure any properties via config or the API, that overrides the default attribute search mechanism. Other than that I can't think of anything.



Feb 10, 2010 at 5:55 PM

Yup, I'm an idiot. I woke up this morning and thought "oh yeah, if I do any injection on the object in the config file, the attributes don't work".

To be honest, I haven't used attributes for injection in the past; I just had one defined for the sake of "completion". However, I am thinking differently about that for my next project. My last project had a huge unity configuration file, and 90% of it was just properties or constructor parameters with nested <dependency /> elements.

It all works fine now (doing injection entirely in config, or only in attributes for a given object).