Xamarin Forms dependency service does not work with generics?

2

I need to generate an instance of a generic class with Xamarim forms using dependcy service (or a DI framework). With the code below I can generate a problemless instance:

  

Interface

namespace PrismNinjectApp1.Application.Interfaces
{
    public interface IUserService { }
}
     

Concrete class

[assembly: Dependency(typeof(PrismNinjectApp1.Application.DomainServices.UserService))]
namespace PrismNinjectApp1.Application.DomainServices
{
    public class UserService : IUserService
    {
        public UserService() { }
    }
}
     

View Model

namespace PrismNinjectApp1.ViewModels
{
    public class MainPageViewModel : BindableBase, INavigationAware
    {
        private readonly IUserService _userService;

        public MainPageViewModel()
        {
            _userService = DependencyService.Get<IUserService>();
        }
        //Implementation of INavigationAware interface (I'm using Prism)
    }
}

But if you use the code below the value returned for the _myInterface variable is always null

  

Interface

namespace PrismNinjectApp1.test
{
    public interface IMyInterface<T> where T : class { }
}
     

Concrete class

[assembly: Dependency(typeof(PrismNinjectApp1.test.MyInterface<>))]
namespace PrismNinjectApp1.test
{
    public class MyInterface<T> : IMyInterface<T> where T : class { }
}
     

View Model

namespace PrismNinjectApp1.ViewModels
{
    public class MainPageViewModel : BindableBase, INavigationAware
    {
        private readonly IMyInterface<Users> _myInterface;

        public MainPageViewModel()
        {
            _myInterface = DependencyService.Get<IMyInterface<Users>>(); //Gets NULL value
        }
        //Implementation of INavigationAware interface (I'm using Prism)
    }
}
     

Users class is a domain entity

public class Users
    {
        [PrimaryKey, AutoIncrement]
        public int Id { get; set; }
        [MaxLength(100)]
        public string Name { get; set; }
    }

Is it possible to generate this instance of the object with dependency service?

I'm trying to make this implementation to use generic services and repositories.

Version of Xamarin Forms: 2.3.4.247

    
asked by anonymous 21.06.2017 / 23:45

0 answers