Support for local/non-dependency parameters.

Feb 17, 2008 at 12:21 AM
Ah I wrong saying that there are no support in Unity (I know there is in OB) for injecting parameters that aren't registered as mapped types nor instances in the container?

From what I can see (how can I have realized that so lately?) there is no way (through code or config) to provide values for parameters (ctor, prop setter or method).

Given the following class:

public class Person
  public Person(string firstName, string lastName) {...}

How could I create such a class through the container?

Dependencies aren't just only interface-based. All other containers I know about (Windows+MicroKernel, StrucutreMap, Spring.NET, ...) support such a simple scenario.

Have I just missed something real basic here? Maybe I'm also missing coffee... ;)
Feb 17, 2008 at 2:07 AM
Edited Feb 17, 2008 at 2:09 AM
agreed. this is almost retardedly simple with OB

IParameter parameter = new ValueParameter<string>(val);
ConstructorPolicy policy = new ConstructorPolicy(constructorInfoInstance, parameter);
builder.Policies.Set<ICreationPolicy>(policy, typeof (MyObject), null);

MyObject myObject = (MyObject)builder.BuildUp(locator, typeof(MyObject), null, null);

This is a basic requirement for containers. Francois is right on the money here. So if he's missing it as well then maybe I had better drive over to his house and take him to get some turkish ground with me :)
Feb 17, 2008 at 4:17 PM
Great point! I haven't dug into it yet, but Unity needs to be able to support the idea of parameters and properties like Windsor. Here is a simple config example from Windsor:

<component service="IDatabase" type="SqlDatabase">

Of course this would need to be handled programmatically as well.

It would also be nice if there was an extensibility mechanism on this. I created a dependency injection tool and stole the idea of expression builders in ASP.NET 2.0 to give me more flexibility in the configuration area:

<add interfaceType="IDatabase" concreteType="SqlDatabase">
        value="${ConnectionString:MyConnectionString}" />

I talk about it more, here.

The key though is that we need support for these simple parameters, both via constructor and property injection.




David Hayden
Microsoft MVP C#
Feb 17, 2008 at 4:28 PM
Once you can specifify a type + value + typeconverter, you get all the flexibility you need.

Of course, access to the IUnityContainer is also required as it is now to resolve those dependencies on registered types & instances.
Mar 5, 2008 at 6:37 PM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.