Reputation: 10764
I am trying to add a dependency to a class property using Unity Configuration and also the types I am trying to inject are generic. I had a "Google" about and found I need to use the '1 syntax. I am following a tutorial by David Hayden on the Validation Application block BUT, instead of programmatically registering the type I am setting it up through the config file.
<typeAliases>
<!-- Lifetime manager types -->
<typeAlias alias="singleton" type="Microsoft.Practices.Unity.ContainerControlledLifetimeManager,Microsoft.Practices.Unity" />
<typeAlias alias="external" type="Microsoft.Practices.Unity.ExternallyControlledLifetimeManager,Microsoft.Practices.Unity" />
<typeAlias alias="IValidator`1" type="MySerivice.IValidator`1,MyService" />
<typeAlias alias="VABValidator`1" type="MySerivice.VABValidator`1,MyService" />
<typeAlias alias="MyService" type="MySerivice.MyService,MyService" />
</typeAliases>
Then I register the mappings and properties here:
<types>
<type type="MyService">
<typeConfig extensionType="Microsoft.Practices.Unity.Configuration.TypeInjectionElement Microsoft.Practices.Unity.Configuration">
<property name="Validator" propertyType="IValidator`1"/>
</typeConfig>
</type>
<type type="IValidator`1" mapTo="VABValidator`1">
<lifetime type="singleton" />
</type>
</types>
Inside the MyService I have the following Property:
private IValidator<RegExpressionObject> validator;
[Dependency]
public IValidator<RegExpressionObject> Validator
{
get { return validator; }
set { validator = value; }
}
When I run this however I get the following exception which is really making me scratch my head:
System.InvalidOperationException: The property Validator on type MyService is of type IValidator'1, and cannot be injected with a value of type IValidator'1
Any help is greatly appreciated.
Upvotes: 2
Views: 1511
Reputation: 489
I believe you're looking for this syntax:
<typeAlias alias="VABValidator`1"
type="MySerivice.VABValidator`1
[[Assembly.Namespace.RegExpressionObject,Assembly]],MyService"/>
That will specify the type of your generic parameter and allow you to inject it.
This functionality is not very well documented :)
Upvotes: 2