Difference between Unity 2 and 3

Dec 4, 2013 at 8:47 AM
In my project, i use Unity 2 before, but after i upgraded to Unity 3, the project didn't work.

here is my analysis:

my codes
    public interface IMyBaseInterface1
    {
    }
    public interface IMyInterface1 : IMyBaseInterface1
    {
    }
    public interface IMyInterface2
    {
    }
    public class MyBaseClass : IMyBaseInterface1
    {
        [Dependency]
        public virtual IMyInterface2 MyProperty { get; set; }
    }
    public class MyClass1 : MyBaseClass, IMyInterface1
    {
        public new IMyInterface2 MyProperty { get; set; }
    }
    public class MyClass2:IMyInterface2
    {
        IMyInterface1 p;
        public MyClass2(IMyInterface1 p)
        {
            this.p = p;
        }
    }
my unity config
 <register type="IMyInterface1" mapTo="MyClass1"/>
 <register type="IMyInterface2" mapTo="MyClass2">
     <constructor>
        <param name="p">
           <dependency type="MyClass1"/>
         </param>
      </constructor>
 </register>
when used Unity 2, it worked fine. the new MyProperty in MyClass1 didn't been resolved.
However in Unity 2, it has also been resolved and finally a StackOverflowException throwed.
Dec 4, 2013 at 8:53 AM
so is there anyone know if i can mark specified subclass as exclusion which have properties in superclass with DependencyAttribute ?
Editor
Dec 5, 2013 at 7:29 AM
The circular dependencies are what is causing the StackOverflowException. I don't know of a way to "mark specified subclass as exclusion which have properties in superclass with DependencyAttribute".

ResolverOverrides might be able to help. It's not particularly elegant but this code explicitly specifies to inject null into the shadowing MyProperty property:
IUnityContainer container = new UnityContainer().LoadConfiguration();

var myInterface2 = container.Resolve<IMyInterface2>(
    new PropertyOverride("MyProperty",
        new InjectionParameter<IMyInterface2>(null))
        .OnType<IMyInterface1>()
    );

Another approach would be to not use a attributes and explicitly declare all configuration:
IUnityContainer container = new UnityContainer();

container.RegisterType<IMyBaseInterface1, MyBaseClass>(new InjectionProperty("MyProperty"));

container.RegisterType<IMyInterface1, MyClass1>(new InjectionProperty("MyProperty", new InjectionParameter<IMyInterface2>(null)));
container.RegisterType<IMyInterface2, MyClass2>();

~~
Randy Levy
entlib.support@live.com
Enterprise Library support engineer
Support How-to
Dec 5, 2013 at 8:02 AM
Edited Dec 5, 2013 at 8:02 AM
Randy, thanks very much.

Depend on your instruction, I add a property element in the register of IMyInterface1 and didn't remove the DependencyAttribute.

so my code didn't change.

my final config:
<register type="IMyInterface1" mapTo="MyClass1">
        <property name="MyProperty" value="null"></property>
</register>
<register type="IMyInterface2" mapTo="MyClass2">
        <constructor>
          <param name="p">
            <dependency type="MyClass1"/>
          </param>
        </constructor>
</register>