0

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

Arnold
  • 89
  • 1
  • 11
  • show us how do you get `myNewContactService` so we can see that it's an instance of `MyNewContactService` – vasily.sib Mar 25 '20 at 10:16
  • Also, please check your posted code. There is no implementation for `IContcatsService.GetContact(..)` in your `MyNewContactService` – vasily.sib Mar 25 '20 at 10:17
  • I guess, that you should make `MyNewContactService` non generic, e.g remove `T` type parameter. It isn't clear, why do you need it, because it already implements `IContcatsService` interface – Pavel Anikhouski Mar 25 '20 at 10:19
  • I've updated the thread with service registration and proper name. @PavelAnikhouski Event when I remove it and register this way: services.AddScoped, MyNewContactService(); it's still return null on casting – Arnold Mar 25 '20 at 10:49
  • Why do you need to perform this registration `services.AddScoped, MyNewContactService>();` Maybe just `services.AddScoped, MyNewContactService>();`, without type argument? – Pavel Anikhouski Mar 25 '20 at 10:53
  • You commented before I managed to update the comment :) Even with this change I'm still getting: Cannot implicitly convert type 'IContcatsService' to 'IContcatsService' – Arnold Mar 25 '20 at 10:57
  • ICont**cats**Service ? – vasily.sib Mar 25 '20 at 11:03
  • @vasily.sib I fixed the text, It's doesn't fix the issue:( – Arnold Mar 25 '20 at 11:10
  • I guess you have some gramatic error somewhere in your code, cuz that code that you provide is 100% working. We need a [MRE](https://stackoverflow.com/help/minimal-reproducible-example) to get further. – vasily.sib Mar 25 '20 at 11:16
  • @vasily.sib the fail on casting and not NONexisting class. I will create blank project on github and share here – Arnold Mar 25 '20 at 11:25
  • @vasily.sib please see demo repo: https://github.com/arnoldsimha/genericRepo-issue .if you run home controller you will get the error – Arnold Mar 25 '20 at 12:00

0 Answers0