简体   繁体   中英

C# Method argument of Type IRepository<BaseType> doesnt accept implementation inheriting from BaseType

I have a generic service that performs the same actions against any given request/response combination. The only time anything specific happens is when it comes to persistence. I'm trying to inject a specific implementation of an repository based off one of the Types (TRequest) in use. So far I have the below, but Ninject and integration tests failing to find any repository that matches what its looking for, a Repository of Type EntityBase. I thought it would be fine as my specific entities inherit from EntityBase...

Error CS1503 Argument 1: cannot convert from 'Repositories.IRepository<Models.PersonModel>' to 'Repositories.IRepository<Models.EntityBase>'

EDIT: I know why im getting the current error, its pretty explicit. Any ideas how I can achieve this?

PersonModel.cs

public class PersonModel : EntityBase
{
    blah blah
}

IRepository.cs

public interface IRepository<T> 
{
    T Get(int id);
    void Add(T entity);
    void Delete(int id);
}

PersonRepository.cs

public class PersonRepository : IRepository<PersonModel> 
{
    blah blah
}

Service.cs

public class Service<TRequest, TResponse> : IService<TRequest, TResponse>
{
    private readonly IRepository<EntityBase> _repository;

    public Service(IRepository<EntityBase> repository)
    {
        _respository = repository
    }

    public TResponse Call(TRequest request)
    {
        var requestEntity = MapperFactory.GetMapperFor(typeof(TRequest)).Map(request); 

        _repository.Add(requestEntity)

        blah blah blah return            
    }
}

IoCModule.cs

Bind<IRepository<PersonModel>>().To<PersonRepository>();

This is because even though PersonModel inherits from EntityBase, this does not mean that IRepository<PersonModel> inherits from IRepository<EntityBase> .

One possible way to deal with this is to have a third generic type:

public class Service<TRequest, TResponse, T> : IService<TRequest, TResponse, T>
{
    private readonly IRepository<T> _repository;
}

Or another way is to specify the type of the service:

public class PersonService<TRequest, TResponse> : IService<TRequest, TResponse>
{
    private readonly IRepository<PersonModel> _repository;
}

Because your IRepository< T > uses T as a parameter (input), you cannot make IRepository< T> covariant which would thus allow assigning IRepository< PersonModel> to IRepository< EntityBase>. So IoC cannot work here.

You will need to write unit tests specific to each implementation for IRepository< T>, as opposed to relying on a generic test for IRepository< EntityBase>.

因为Repositories.IRepository<Models.PersonModel>Repositories.IRepository<Models.EntityBase>实际上是不同的类型。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM