3

is there a way using the Unity framework to pass an integer as an argument into the constructor or a resolved object?

Pseudo code..

IService svc = Container.Resolve<ConcreteService>()

in this case Concrete service will be something like this...

public class ConcreteService
{
    public ConcreteService(int val)
    {
    }
}

Also I need to do this in xml configuration as opposed to doing it in code.

Thanks in advance.

Remotec
  • 10,304
  • 25
  • 105
  • 147
  • Possible dupe: [Can I pass constructor parameters to Unity’s Resolve() method?](http://stackoverflow.com/questions/787001/can-i-pass-constructor-parameters-to-unitys-resolve-method) – gehho Apr 27 '10 at 09:48
  • That question is about passing in an object, which I think that unity will detect this and create appropriate objects to pass in. I want to pass in a value type which is slightly different. – Remotec Apr 27 '10 at 09:54
  • 2
    No. Have a look at the second link in the accepted answer. This describes how *any* arguments can be passed to a constructor for a certain type. In your example, you would have to use it like this: `Container.Resolve(new ParameterOverrides { { "val" }, { 42 } });` – gehho Apr 27 '10 at 11:06

1 Answers1

5

Hope I understood you right

   public class ConcreteService {

        public int Val { get; set; }

        public ConcreteService(int val) {
            Val = val;
        }
    }

Now let's configure unity.

        var container = new UnityContainer();

        container.RegisterType<ConcreteService>();
        container.Configure<InjectedMembers>().ConfigureInjectionFor<ConcreteService>(new InjectionConstructor(1));

        container.RegisterType<ConcreteService>("for42");
        container.Configure<InjectedMembers>().ConfigureInjectionFor<ConcreteService>("for42",
                                                                                      new InjectionConstructor(42));
        container.RegisterType<ConcreteService>("for31");
        container.Configure<InjectedMembers>().ConfigureInjectionFor<ConcreteService>("for31",
                                                                                      new InjectionConstructor(31));

        Debug.WriteLine(container.Resolve<ConcreteService>().Val); //1
        Debug.WriteLine(container.Resolve<ConcreteService>("for42").Val); //42
        Debug.WriteLine(container.Resolve<ConcreteService>("for31").Val); //31

Equvivalent configuration for "for42" is

Unity 1.4

<type type="ConcreteService"  name="for42">
          <typeConfig extensionType="Microsoft.Practices.Unity.Configuration.TypeInjectionElement,
                                     Microsoft.Practices.Unity.Configuration">
            <constructor>
              <param name="val" parameterType="int">
                <value value="42"/>
              </param>
            </constructor>           
          </typeConfig>
        </type>

Unity 2.0

It's much the same but without redundant typeConfig node

<type type="ConcreteService"  name="for42">
        <constructor>
              <param name="val" parameterType="int">
                <value value="42"/>
              </param>
            </constructor>           
        </type>
er-v
  • 4,405
  • 3
  • 30
  • 37