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.
I believe you’re looking for this syntax:
That will specify the type of your generic parameter and allow you to inject it.
This functionality is not very well documented 🙂