简体   繁体   English

C#将通用委托传递给构造函数

[英]C# Pass a generic delegate to constructor

I'm new to C# and I try to find a way to pass a generic delegate to a constructor. 我是C#的新手,我试图找到一种将通用委托传递给构造函数的方法。

When calling a show endpoint, if there is pagination, the endpoint should return a Page instance. 调用show端点时,如果存在分页,则端点应返回Page实例。 When creating the Page instance, the delegate method should be passed as one of constructor parameters. 创建Page实例时,委托方法应作为构造函数参数之一传递。 ShowsEndpoints will host many endpoints and many of these endpoints use pagination. ShowsEndpoints将托管许多端点,并且其中许多端点使用分页。 I want to use only one class to handle all endpoints. 我只想使用一个类来处理所有端点。

public partial class ShowsEndpoints : Endpoint
{
    public Func<Task<IEnumerable<object>>> FetchPopularShowsAsyncDelegate;
    public Func<Task<IEnumerable<object>>> FetchTrendingShowsAsyncDelegate;
    public Func<Task<IEnumerable<object>>> FetchMostWatchedShowsAsyncDelegate;

    public async Task<IEnumerable<object>> FetchPopularShowsAsync(int page = 1, int limit = 20)
    {
        return await SendAsync(new PopularShowsRequest(page, limit));
    }

    public async Task<IEnumerable<object>> FetchTrendingShowsAsync(int page = 1, int limit = 20)
    {
        return await SendAsync(new TrendingShowsRequest(page, limit));
    }

    public async Task<IEnumerable<object>> FetchMostWatchedShowsAsync(int page = 1, int limit = 20)
    {
        return await SendAsync(new MostWatchedShowsRequest(page, limit));
    }
}

Page class which will call the delegate to fetch the next page if there is a next page. 页面类,如果存在下一页,它将调用委托以获取下一页。

public class Page<TItem, TDelegate>
{
    private TDelegate _delegateCommand;

    public int CurrentPage { get; }
    public int TotalPages { get; }
    public int PageItemsCount { get; }
    public int TotalItemsCount { get; }
    public IEnumerable<TItem> Items { get; }

    public Page(int page, int totalPages, int itemsCount, int totalItemsCount, IEnumerable<TItem> items, TDelegate delegateCommand)
    {
        CurrentPage = page;
        TotalPages = totalPages;
        PageItemsCount = itemsCount;
        TotalItemsCount = totalItemsCount;
        Items = items;
        _delegateCommand = delegateCommand;
    }

    public bool HasNext()
    {
        return CurrentPage <= TotalPages;
    }

    public bool HasPrevious()
    {
        return CurrentPage >= 1;
    }

    public Page<TItem, TDelegate> Next()
    {
        // call _delegateCommand to fetch the next page items
    }

    public Page<TItem, TDelegate> Previous()
    {
        // call _delegateCommand to fetch the previous page items
    }
}

Is it possible ? 可能吗 ?

Thank you. 谢谢。

EDIT 编辑

I have updated my classes code in order to understand better my question. 我已经更新了班级代码,以更好地理解我的问题。 I have 3 endpoints associated with their delegates and I want the Page class to use any of these 3 delegates with the same code to avoid duplication. 我有3个与它们的委托相关联的端点,并且我希望Page类使用这3个委托中的任何一个使用相同的代码来避免重复。 So _delegateCommand can be FetchPopularShowsAsyncDelegate , FetchTrendingShowsAsyncDelegate or FetchMostWatchedShowsAsyncDelegate . 因此_delegateCommand可以是FetchPopularShowsAsyncDelegateFetchTrendingShowsAsyncDelegateFetchMostWatchedShowsAsyncDelegate

I'm not sure if I've got the purpose of _delegateCommand right - I guess it should be the function returning IEnumerable<TItem> . 我不确定我_delegateCommand正确使用_delegateCommand的目的-我想应该是返回IEnumerable<TItem> If so, then you can declare it precisely like that, and then you won't have to introduce another generic parameter: 如果是这样,那么您可以像这样精确地声明它,那么您就不必引入另一个通用参数:

public class Page<TItem>
{
    private Func<IEnumerable<TItem>>_delegateCommand;

    public int CurrentPage { get; }
    public int TotalPages { get; }
    public int PageItemsCount { get; }
    public int TotalItemsCount { get; }
    public IEnumerable<TItem> Items { get; }

    public Page(int page, int totalPages, int itemsCount,
                int totalItemsCount, IEnumerable<TItem> items,
                Func<IEnumerable<TItem>> delegateCommand)
    {
        CurrentPage = page;
        TotalPages = totalPages;
        PageItemsCount = itemsCount;
        TotalItemsCount = totalItemsCount;
        Items = items;
        _delegateCommand = delegateCommand;
    }

    public bool HasNext()
    {
        return CurrentPage <= TotalPages;
    }

    public bool HasPrevious()
    {
        return CurrentPage >= 1;
    }
}

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

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