Please, help!!!

Dec 21, 2009 at 4:40 PM

Hello everybody! I'm new to IoC technology and I have a question.

In Spring.Net  I have some config file like this:

 

<object id="someid"
    type="Spring.Objects.Factory.Config.PropertyRetrievingFactoryObject, Spring.Core">
    <property name="StaticProperty">
      <value>MyAssembly.Test.TestType, MyAssembly.Test</value>
    </property>
  </object>

<object name="someName"
    type="MyAssembly.Test.TestType2, MyAssembly.Test" singleton="false">
    <constructor-arg index="0" ref="someid"/>
  </object>

I can pass the value of  TestType in constructor of TestType2, but how can i do that in Unity?

For example, I have the folllowing classes:

class SomeReallyCoolStuff
    {
        private string _test1 = String.Empty;

        public SomeReallyCoolStuff(string test1)
      {
          _test1 = test1;
      }

        public void TestMethod()
        {
            Console.WriteLine(_test1);
        }
    }

 class TestClass1
    {
        private string field1 = "1";
        private string field2 = "2" ;
        public TestClass1()
        {
        }

        public TestClass1(string f1)
        {
            field1 = f1;
            
        }
        [Dependency("test")]
        public string Field1
        {
            get { return field1; }
        }

        public string Field2
        {
            get { return field2; }
        }

        public void MethodStub()
        {
            Console.WriteLine(String.Format("fiel1 = {0}, field2 = {1}",field1,field2));
        }
    }

and Unity.config file:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

  <configSections>

    <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration" />
  </configSections>
  <unity>
    <typeAliases>
      <!-- Lifetime manager types -->
   
      <typeAlias alias="MyFirstType"
                    type="MyUnityTest.TestClass1, MyUnityTest" />
      <typeAlias alias="MySecondType"
                      type="MyUnityTest.SomeReallyCoolStuff, MyUnityTest" />
      
    </typeAliases>
    <containers>
      <container >
        <types>
          <type type="MyFirstType">
            <typeConfig extensionType="Microsoft.Practices.Unity.Configuration.TypeInjectionElement, Microsoft.Practices.Unity.Configuration">
              <constructor>
                <param name="filed1" parameterType="System.String">
                  <value value="Test1" />
                </param>
             </constructor>
            </typeConfig>
          </type>

          <type type="MySecondType">
            <typeConfig extensionType="Microsoft.Practices.Unity.Configuration.TypeInjectionElement, Microsoft.Practices.Unity.Configuration">
              <constructor>
                          
                <param name="tet1" parameterType="System.String">
                  <dependency name="test"/>
                </param>
             
               </constructor>
            </typeConfig>
          </type>
        </types>
     </container>
    </containers>
  </unity>
</configuration>

But it throws exceprion:

Resolution of the dependency failed, type = "MyUnityTest.SomeReallyCoolStuff", name = "". Exception message is: The current build operation (build key Build Key[MyUnityTest.SomeReallyCoolStuff, null]) failed: The parameter test1 could not be resolved when attempting to call constructor MyUnityTest.SomeReallyCoolStuff(System.String test1). (Strategy type BuildPlanStrategy, index 3)

Please, help!!!

 

 

Dec 27, 2009 at 11:50 PM

Your problem is in the configuration for the test1 parameter:

                <param name="tet1" parameterType="System.String">
                  <dependency name="test"/>
                </param>
             

First off, you spelled the parameter name wrong (no big deal in Unity 1.2, will be an error in Unity 2.0). Second, for the value, you said <dependency> - this means, effectively, for the container to try and resolve an object registered as a string with the name "test". You have no such registration, so it fails (since Unity won't just stick in a default value for a string).

Fixing this depends on what you want to happen. If you just want a specific string, use <value value="your string here"/> instead of dependency. you could also use a registered instance for a string and that name to get it working as well. However, your Unity example doesn't seem to correspond to the Spring example you posted. Is the Unity example what you're actually trying to do, or do you want to do something else and tried to simplify the example?