合并结构相似的方法以避免重复

时间:2017-07-02 02:45:05

标签: c# .net visual-studio-2017

如何以更实用的方式组合/合并/缩短这些方法以避免重复?

public Task<List<UsageSummaryModel>> GetBilledUsageSummary(int accountId, string billingRunId)
        {
            var url = string.Format("{0}{1}/{2}/usage", BaseUrl, accountId, billingRunId);
            return GetAsyncHandlerForRestApi<List<UsageSummaryModel>>(url);
        }

        public Task<GetSampleInvoiceRunIdResponse> GetUnBilledUsageSampleRunId(int accountId)
        {
            var url = string.Format("{0}{1}/sample?usageonly=true", BaseUrl, accountId);
            return GetAsyncHandlerForRestApi<GetSampleInvoiceRunIdResponse>(url);
        }

        public Task<List<UsageSummaryModel>> GetUnBilledUsageSummary(int accountId, int sampleRunId)
        {
            var url = string.Format("{0}{1}/sample/{2}/usage", BaseUrl, accountId, sampleRunId);
            return GetAsyncHandlerForRestApi<List<UsageSummaryModel>>(url);
        }


        public Task<List<CallDetailsRecordModel>> GetBilledCallDetailRecords(int accountId, int billingRunId, int cdrSummaryId, int page, int pageSize)
        {
            var url = string.Empty;

            if (page == -1 && pageSize == -1)//load all records
            {
                url = string.Format("{0}{1}/{2}/usage/{3}", BaseUrl, accountId, billingRunId, cdrSummaryId);
            }
            else
            {
                url = string.Format("{0}{1}/{2}/usage/{3}?page={4}&pageSize={5}", BaseUrl, accountId, billingRunId, cdrSummaryId, page, pageSize);
            }

            return GetAsyncHandlerForRestApi<List<CallDetailsRecordModel>>(url);
        }

        public Task<List<CallDetailsRecordModel>> GetUnBilledCallDetailRecords(int accountId, int sampleRunId, int cdrSummaryId, int page, int pageSize)
        {
            var url = string.Empty;

            if (page == -1 && pageSize == -1)//load all records
            {
                url = string.Format("{0}/{1}/SAMPLE/{2}/usage/{3}", BaseUrl, accountId, sampleRunId, cdrSummaryId);
            }
            else
            {
                url = string.Format("{0}/{1}/SAMPLE/{2}/usage/{3}?page={4}&pageSize={5}", BaseUrl, accountId, sampleRunId, cdrSummaryId, page, pageSize);
            }

            return GetAsyncHandlerForRestApi<List<CallDetailsRecordModel>>(url);
        }

1 个答案:

答案 0 :(得分:2)

您可以创建和重载方法,该方法将泛型类型,url格式和param对象数组作为参数:

public static Task<T> GetAsyncHandler(string urlFormat, params object[] args)
{
    string url = string.Format(urlFormat, args);
    return Task<T>.Run( () => { return GetAsyncHandlerForRestApi(url)});
}

这允许您使用单个重载方法来生成处理程序。

例如,您可以定义在类AsyncHandler<T>中定义GetAsyncHandlerForRestApi方法:

public class AsyncHandler<T>
{
    private string url;

    public AsyncHandler(string url)
    {
        this.url = url;
    }
    public static AsyncHandler<T> GetAsyncHandlerForRestApi(string url)
    {
        return new AsyncHandler<T>(url);
    }
    public static Task<T> GetAsyncHandler(string urlFormat, params object[] args)
    {
        string url = string.Format(urlFormat, args);
        return Task<T>.Run( () => { return GetAsyncHandlerForRestApi(url)});
    }
}

然后您可以在任何地方调用此方法:

 string endPointFormat = BaseUrl + "{0}{1}/sample?usageonly=true";
 Task<List<UsageSummaryModel>> result = AsyncHandler<List<UsageSummaryModel>>.GetAsyncHandler(endPointFormat, accountId);

如果你仍然需要单独的方法,你也可以这样定义它们:

public Task<List<UsageSummaryModel>> GetBilledUsageSummary(int accountId, string billingRunId)
    {
        string endPointFormat= BaseUrl + "{0}{1}/{2}/usage";
        return AsyncHandler<List<UsageSummaryModel>>.GetAsyncHandler(endPointFormat, accountId, billingRunId);
    }

    public Task<List<UsageSummaryModel>> GetUnBilledUsageSampleRunId(int accountId)
    {
        string endPointFormat = BaseUrl + "{0}{1}/sample?usageonly=true";
        return AsyncHandler<List<UsageSummaryModel>>.GetAsyncHandler(endPointFormat, accountId);
    }

作为在string urlFormat, params object[] args中使用GetAsyncHandler作为参数的替代方法,您可以定义一个类来构建端点,例如EndPointBuilder

您可以使用Factory设计模式在方法内为您构建终点网址,例如public string BuildUrl(),然后将EndPointBuilder传递给GetAsyncHandler

例如:

public static Task<T> GetAsyncHandler(EndPointBuilder builder)
{
    string url = BuildUrl.BuildUrl();
    return Task<T>.Run( () => { return GetAsyncHandlerForRestApi(url)});
}

您可以设计EndPointBuilder但是您希望构建服务电话所需的网址。