Value Datatype construction parameters

Apr 30, 2008 at 8:52 PM
I'm looking at Unity for the first time and i was looking through the documentation and noticed no examples showing the ability to inject value types (strings, int, etc). Is this possible? or do I HAVE to make these values types properties? Below is a rather simple example:

TestMethod
public void UnityTestingController() {
IUnityContainer carController = new UnityContainer()
.RegisterType<IVehicle, Yamaha>();
//TODO: Figure out how to inject or add the name of the car as well.
Car newCar = carController.Resolve<Car>();
}

public class Car
{
IVehicle _brandOfCar;

public Car(IVehicle brandOfCar, string nameOfCar) {
_brandOfCar = brandOfCar;
}
}

public interface IVehicle
{
int NumberOfWheels { get; set; }
}

public class Yamaha : IVehicle
{
public Yamaha() {

}

private int _numberOfWheels = 0;

public int NumberOfWheels {
get { return _numberOfWheels; }
set { _numberOfWheels = value; }
}
}
Apr 30, 2008 at 8:56 PM
you can do for constructor


<type type="NameServiceConfiguration" mapTo="NameServiceConfiguration">
<lifetime type="singleton" />
<typeConfig extensionType = "Microsoft.Practices.Unity.Configuration.TypeInjectionElement,
Microsoft.Practices.Unity.Configuration" >
<constructor>
<param name="env" parameterType="System.String">
<value value ="QA"/>
</param>
<param name="iparam" parameterType="System.Int32">
<value value="1000000"/>
</param>
</constructor>
</typeConfig>
</type>


but Ints are not working. I have posted Question for that. String works I have used in other example.


Apr 30, 2008 at 9:00 PM
you will have to include something like this in Unity config file.



<type type="IVehicle" mapTo="Yamaha">
<lifetime type="singleton" />
<typeConfig extensionType = "Microsoft.Practices.Unity.Configuration.TypeInjectionElement,
Microsoft.Practices.Unity.Configuration" >
<constructor>
<param name="NumberOfWheels" parameterType="System.Int32">
<value value="2"/>
</param>
</constructor>
</typeConfig>
</type>
Apr 30, 2008 at 9:04 PM


mrudang wrote:
you can do for constructor


<type type="NameServiceConfiguration" mapTo="NameServiceConfiguration">
<lifetime type="singleton" />
<typeConfig extensionType = "Microsoft.Practices.Unity.Configuration.TypeInjectionElement,
Microsoft.Practices.Unity.Configuration" >
<constructor>
<param name="env" parameterType="System.String">
<value value ="QA"/>
</param>
<param name="iparam" parameterType="System.Int32">
<value value="1000000"/>
</param>
</constructor>
</typeConfig>
</type>


but Ints are not working. I have posted Question for that. String works I have used in other example.





Interesting, so this can't be done dynamically yet? You have to use a config file?
Apr 30, 2008 at 9:23 PM
I am not aware you can do dynamically.

one way I would suggest

Resolve<T>() takes params.
e.g.

Car newCar = carController.Resolve<Car>(10); or something

Apr 30, 2008 at 9:24 PM
This is not supported, I am just suggeting if Unity can do this in future.
Apr 30, 2008 at 9:30 PM


mrudang wrote:
This is not supported, I am just suggeting if Unity can do this in future.



Ah... appreciate the answer! Thanks.
Apr 30, 2008 at 11:35 PM

mrudang wrote:
This is not supported, I am just suggeting if Unity can do this in future.



No true. You CAN do this dynamically, depending on your definition of dynamic.

Basically, you currently can't set this per buildup, but you can set it for your class via the API.

For example:
 public void UnityTestingController() {
   IUnityContainer carController = new UnityContainer()
       .RegisterType<IVehicle, Yamaha>()
       .Configure<InjectedMembers>()
           .ConfigureInjectionFor<Yamaha>(
               new InjectionConstructor("Motorcycle"))
       .Container;
 
       IVehicle newCar = carController.Resolve<IVehicle>();
}
 

Assuming you had a constructor on Yamaha that took a string.
Apr 30, 2008 at 11:50 PM


ctavares wrote:

mrudang wrote:
This is not supported, I am just suggeting if Unity can do this in future.



No true. You CAN do this dynamically, depending on your definition of dynamic.

Basically, you currently can't set this per buildup, but you can set it for your class via the API.

For example:
 public void UnityTestingController() {
   IUnityContainer carController = new UnityContainer()
       .RegisterType<IVehicle, Yamaha>()
       .Configure<InjectedMembers>()
           .ConfigureInjectionFor<Yamaha>(
               new InjectionConstructor("Motorcycle"))
       .Container;
 
       IVehicle newCar = carController.Resolve<IVehicle>();
}
 

Assuming you had a constructor on Yamaha that took a string.



Oh sweet!
May 1, 2008 at 3:05 PM
I was just saying cannot specify constructor params in Resolve.
It would be clean to specify in Resolve