unitycontainer / configuration

Unity.Configuration package
Apache License 2.0
13 stars 13 forks source link

Multiple method elements no more supported under register? #16

Closed patrikrosquist closed 4 years ago

patrikrosquist commented 5 years ago

Description

The support for unbounded number of method elements under the register node seems to have been removed. Now only the first method call is made. The schema for InjectionGroup defines "method" to be: minOccurs="0" maxOccurs="unbounded".

I'm not sure where it got lost but we are upgrading from old 4 to latest.

To Reproduce

    public interface IChild
    {
        int Age { get; set; }
    }

    public interface IParent
    {
        IList<IChild> Children { get; set; }
        void Add(IChild child);
    }

    public class Child : IChild
    {
        public int Age { get; set; }

        public Child(int age)
        {
            Age = age;
        }
    }

    public class Parent : IParent
    {
        public IList<IChild> Children { get; set; }

        public Parent()
        {
            Children = new List<IChild>();
        }

        public void Add(IChild child)
        {
            Children.Add(child);
        }
    }

    [TestClass]
    public class Test
    {
        [TestMethod]
        public void ResolveInstanceWithMultipleMethodCallsTest()
        {
            // Arrange
            var container = new UnityContainer();
            container.LoadConfiguration();

            // Act
            var parent = container.Resolve<IParent>();

            // Assert
            Assert.AreEqual(2, parent.Children.Count);
        }
    }

Additional context

The above test used this configuration:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Unity.Configuration" />
  </configSections>

  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
  </startup>

  <unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
    <namespace name="UnityTest" />
    <assembly name="UnityTest" />
    <container>
      <register type="IParent" mapTo="Parent">
        <method name="Add">
          <param name="child" dependencyName="Child1" />
        </method>
        <method name="Add">
          <param name="child" dependencyName="Child2" />
        </method>
      </register>
      <register name="Child1" type="IChild" mapTo="Child">
        <constructor>
          <param name="age" value="12" />
        </constructor>
      </register>
      <register name="Child2" type="IChild" mapTo="Child">
        <constructor>
          <param name="age" value="6" />
        </constructor>
      </register>
    </container>
  </unity>
</configuration>
ENikS commented 5 years ago

Duplicate #143