简体   繁体   English

通用扩展方法重构

[英]Generic extension method refactor

I'm wondering if the following can be refactored in the way I would like it: 我想知道是否可以按照我希望的方式重构以下内容:

[EditorBrowsable(EditorBrowsableState.Never)]
public static class ListExtensions
{
    public static PaginatedList<Y> ToMappedPaginatedList<T, Y>(this PaginatedList<T> source)
    {
        var mappedList = new List<Y>();
        Mapper.Map(source, mappedList);

        return new PaginatedList<Y>(mappedList, source.PageIndex, source.PageSize, source.TotalCount);
    }
}

The Mapper.Map line is using AutoMapper to map properties from an entity to a DTO object. Mapper.Map行使用AutoMapper将属性从实体映射到DTO对象。

This is called like this: 这样称呼:

var list = await _service.GetAllAsync(pageIndex, _pageSize);
var dtoList = list.ToMappedPaginatedList<Farmer, FarmerDTO>();

but I'd like to call it like this: 但我想这样称呼它:

var dtoList = list.ToMappedPaginatedList<FarmerDTO>();

This saves a little bit of typing and you don't always need to be aware of the source list its type. 这样可以节省一些输入时间,您不必总是需要知道源列表的类型。 Unfortunately this code doesn't work and I'm not sure if there's a simple answer. 不幸的是,这段代码无法正常工作,我不确定是否有一个简单的答案。

Anyone got an idea? 有人知道吗?

Thanks in advance. 提前致谢。

Yannick 雅尼克

Either you call a method and specify all the generic arguments or you specify none and let the compiler infer them, there's no support for partial inference. 要么调用一个方法并指定所有通用参数,要么不指定任何通用参数,然后让编译器进行推断,则不支持部分推断。

As such, the only way to get your code to compile is to make ToMappedPaginatedList take 1 generic parameter, instead of two. 因此,编译代码的唯一方法是使ToMappedPaginatedList接受1个通用参数,而不是2个。

If you have access to the PaginatedList class, putting the method in there will enable the syntax you desire since the instance knows what it's own type is. 如果您有权访问PaginatedList类,则将方法放入其中将启用所需的语法,因为实例知道其自身的类型是什么。

I don't recommend the following but it demonstrates a way to take advantage of type inference. 我不推荐以下内容,但是它演示了一种利用类型推断的方法。

You can enable type inference by adding a 2nd "useless" parameter of type Y. If you pass default(FarmerDTO) as the 2nd parameter, a null will be passed as the parameter value but the intended type will be inferred. 您可以通过添加类型Y的第二个“无用”参数来启用类型推断。如果将default(FarmerDTO)作为第二个参数传递,则将传递null作为参数值,但是将推断出预期的类型。

[EditorBrowsable(EditorBrowsableState.Never)]
public static class ListExtensions
{
    public static PaginatedList<Y> ToMappedPaginatedList<T, Y>(this PaginatedList<T> source, Y destinationPlaceholder)
    {
        var mappedList = new List<Y>();
        Mapper.Map(source, mappedList);

        return new PaginatedList<Y>(mappedList, source.PageIndex, source.PageSize, source.TotalCount);
    }
}

Call it like this: 这样称呼它:

var result1 = s.ToMappedPaginatedList(default(FarmerDTO));

Fair warning. 公平警告。 I've never used this because I find the resulting code to be non-obvious as to what it is doing. 我从来没有使用过它,因为我发现生成的代码在做什么方面并不明显。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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