如何定义ICollection的扩展方法< T>其中T:IMyInterface在方法定义中未指定T. [英] How to Define Extension Method for ICollection<T> where T : IMyInterface without Specifying T in the Method Definition

查看:226
本文介绍了如何定义ICollection的扩展方法< T>其中T:IMyInterface在方法定义中未指定T.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景:我想在DTO被映射到实体这一点上勾住业务规则。



IEntityDto是所有可以直接映射到实体的DTO实现的接口。



单个实例正常工作:

  public static TEntity MapTo< TEntity>(this IEntityDto dto)
{
...运行不需要数据库访问的业务规则...
返回AutoMapper.Mapper.Map< TEntity>(dto);
}

我也想以同样的方式扩展ICollection:

  public static ICollection< TEntity> MapToCollection< TEntity>(这个ICollection< IEntityDto> dtos)
{
...运行不需要数据库访问的业务规则...
返回AutoMapper.Mapper.Map< ICollection< TEntity>>(DTOS);
}

不幸的是,MapToCollection不会显示在上下文菜单中,或者在应用于ICollection IEntityDto。



我错过了什么让它工作?我需要扩展ICollection,其中T是IEntityDto?

  public static ICollection< TEntity> MapToCollection< TDto,TEntity>(这个ICollection< TDto> dtos)其中TDto:IEntityDto 
{
...做工作并返回...
}



以上的工作,但我希望能从集合中推断T.

谢谢!

解决方案

您有效需要一个签名为

的方法

  public static ICollection< TEntity> MapToCollection< TEntity,TEntityDto>(
this ICollection< TEntityDto> dtos)
其中TEntityDto:IEntityDto

...但这会迫使你指定两个 类型的参数,我明白你不想这样做。



你可以做什么,而不是两个跳跃,例如

  public static class DtoExtensions 
{
public static CollectionMapper< TEntityDto> Map(此ICollection< TEntityDto> dtos)
其中TEntityDto:IEntityDto
{
返回新的CollectionMapper< TEntityDto>(dtos);
}
}

公共类CollectionMapper< TEntityDto>其中TEntityDto:IEntityDto
{
私有只读ICollection< TEntityDto> DTOS;

public CollectionMapper(ICollection< TEntityDto> dtos)
{
this.dtos = dtos;
}

public ICollection< TEntity>到< TEntity>()
{
//商业规则...
返回AutoMapper.Mapper.Map< ICollection< TEntity>>(dtos);


您现在可以使用:

  var result = collection.Map()。至< FooEntity>(); 

Map 调用推断 TEntityDto ,并在调用中指定 TEntity


Background: I want to hook in business rules at the point that DTOs are being mapped to entities. I figured encapsulating the mapping into an extension method would be a good route.

IEntityDto is the interface that all DTOs that can be directly mapped to entities implement.

The single instance works fine:

public static TEntity MapTo<TEntity>(this IEntityDto dto)
{
    ... Run Business Rules that don't require db access ...
    return AutoMapper.Mapper.Map<TEntity>(dto);
}

I'd like to also extend ICollection the same way:

public static ICollection<TEntity> MapToCollection<TEntity>(this ICollection<IEntityDto> dtos)
{
    ... Run Business Rules that don't require db access ...
    return AutoMapper.Mapper.Map<ICollection<TEntity>>(dtos);
}

Unfortunately, MapToCollection does not show up on the context menu or compile when applied to an ICollection of IEntityDto.

What am I missing to get this to work? Do I need to just extend ICollection where T is IEntityDto? I'd prefer to not have to include the DTO type when calling the extension method.

public static ICollection<TEntity>MapToCollection<TDto,TEntity>(this ICollection<TDto> dtos) where TDto: IEntityDto
{
    ... Do Work and Return ...
}

The above works, but I was hoping to infer T from the collection.

Thanks!

解决方案

You effectively need a method with a signature of

public static ICollection<TEntity> MapToCollection<TEntity, TEntityDto>(
    this ICollection<TEntityDto> dtos)
    where TEntityDto : IEntityDto

... but that would force you to specify both type arguments, which I understand you don't want to do.

What you can do instead is go in two hops, e.g.

public static class DtoExtensions
{
    public static CollectionMapper<TEntityDto> Map(this ICollection<TEntityDto> dtos)
        where TEntityDto : IEntityDto
    {
        return new CollectionMapper<TEntityDto>(dtos);
    }
}

public class CollectionMapper<TEntityDto> where TEntityDto : IEntityDto
{
    private readonly ICollection<TEntityDto> dtos;

    public CollectionMapper(ICollection<TEntityDto> dtos)
    {
        this.dtos = dtos;
    }

    public ICollection<TEntity> To<TEntity>()
    {
        // Business rules...
        return AutoMapper.Mapper.Map<ICollection<TEntity>>(dtos);
    }
}

You can now use:

var result = collection.Map().To<FooEntity>();

The Map call infers TEntityDto, and you specify TEntity in the To call.

这篇关于如何定义ICollection的扩展方法&lt; T&gt;其中T:IMyInterface在方法定义中未指定T.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆