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.

3
  • Possible dupe: Can I pass constructor parameters to Unity’s Resolve() method? Commented Apr 27, 2010 at 9: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. Commented Apr 27, 2010 at 9: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<IService>(new ParameterOverrides<ConcreteService> { { "val" }, { 42 } }); Commented Apr 27, 2010 at 11:06

1 Answer 1

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>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.