I have a simple question (I hope) which I can't find an answer anywhere. I want to implement a generic (a little bit complicated) service. Github repo: https://github.com/arnoldsimha/genericRepo-issue This is my generic Interface:
public interface IContactsService<T> where T : Contact
{
Task<T> GetContact(string email);
}
This is the contact:
public class Contact
{
public string Name { get; set; }
public string Email { get; set; }
}
Now I've created a new Service that needs to implement this interface. I've extended Contact:
public class AppContact : Contact
{
public string Id { get; set; }
}
And service:
public class MyNewContactService<T> : IContactsService<AppContact>
{
public async Task<AppContact> GetContact(string email)
{
}
}
This is how I register it:
services.AddScoped<IContactsService<AppContact>, MyNewContactService<AppContact>>();
So far so good. I've created another service that implements the same interface. May I ask why when I'm trying to cast MyNewContactService as IContactsService I'm getting null?
myNewContactService as IContactsService<AppContact>
I'm trying to add an option to select the implementation according to a specific condition.
Thank you