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 have interface
public interface ISendMessage
{
void Send(string contact, string message);
}
class
public class EmailService : ISendMessage
{
public void Send(string contact, string message)
{
// do
}
}
class
public class MessageService<T> where T : ISendMessage
{
}
I try use it via constructor injection in other class
public MyService(MessageService<ISendMessage> messageService)
{
}
How I inject MessageService<EmailService> instead of MessageService<ISendMessage>?
I try do it via app.config
<alias alias="MessageService'1" type="MyNamespace.MessageService'1, MyAssembly" />
<alias alias="EmailMessageService'1" type="MyNamespace.MessageService'1[[MyNamespace.EmailService, MyAssembly]], MyAssembly" />
I get error
The type name or alias MessageService'1 could not be resolved. Please check your configuration file and verify this type name.
And how I can pass in MessageService<T> implement paramenter MessageService<EmailService>?
Thanks
Update
I modified my class to the following:
public class MessageService<T> where T : ISendMessage
{
private T service;
[Dependency]
public T Service
{
get { return service; }
set { service = value; }
}
}
and use configuration
<alias alias="ISendMessage" type="MyNamespace.ISendMessage, MyAssembly" />
<alias alias="EmailService" type="MyNamespace.EmailService, MyAssembly" />
<register type="ISendMessage" mapTo="EmailService">
</register>
It works :-)