简体   繁体   中英

Register generic interface with non generic class

I have a generic interface in my application:

public interface IBaseMapper<DTO, Entity> 
    where DTO : class, new()
    where Entity : class, new()
{
    List<DTO> EntityToDTO_Transform(List<Entity> requestEntity);
    Entity DTOEntity_Transform(DTO requestDTO);
}

Which is being implemented by a non-generic class:

public class RequestMapper : IBaseMapper<RequestDTO,REQUEST>
{
    public REQUEST DTOEntity_Transform(RequestDTO requestDTO)
    {
        return Mapper.Map<RequestDTO, REQUEST>(requestDTO);
    }

    public List<RequestDTO> EntityToDTO_Transform(List<REQUEST> requestEntity)
    {
        return Mapper.Map<List<REQUEST>, List<RequestDTO>>(requestEntity);
    }
}

I am using Unity as IoC container for dependency injection.

How do I resolve dependencies?

var container = new UnityContainer();

container.RegisterType(typeof(IBaseMapper<>), typeof(RequestMapper); // ???

config.DependencyResolver = new UnityDependencyResolver(container);

Any thoughts?

Because RequestMapper is non-generic, you need to specify its closed-generic abstraction when making the mapping, as follows:

container.RegisterType(
    typeof(IBaseMapper<RequestDTO,REQUEST>), 
    typeof(RequestMapper));

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