如何在方法中传递对象来调用ASP.NET Boilerplate中的服务API?

时间:2017-09-11 05:23:31

标签: aspnetboilerplate asp.net-boilerplate

我已定义了AppService文件中定义的方法,方法签名为public PagedResultDto<FetchData> GetSearchData(FetchDataInput searchInput)。我从Angular代码调用此方法,但是service-proxoes.ts文件已生成方法,我需要逐个传递所有参数。

public class FetchDataInput: PagedAndSortedInputDto, IShouldNormalize
{
    public int DataLevel { get; set; }
    public int DataType { get; set; }
    public string DataCode { get; set; }
    public string DescLong { get; set; }
    public string LanguageCode { get; set; }
    public string DataParent { get; set; }

    public void Normalize()
    {
        if (string.IsNullOrEmpty(Sorting))
        {
            Sorting = "DataCode";
        }
    }

}

Service-Proxies.ts文件:

    getSearchData(dataLevel: number, dataType: number, dataCode: string, descLong: string, languageCode: string, dataParent: string, sorting: string, maxResultCount: number, skipCount: number): Observable<PagedResultDtoOfFetchData> {

所以我必须通过以下方式调用getSearchData方法。

this._dataService.getSearchData(AppConsts.dataLevelType, undefined, undefined,
        undefined, this.currentLanguage.name, undefined, undefined, undefined, undefined).subscribe((result) => {
           //result.items;
        });

所以我必须传递所有参数,但如果让我们说有100个参数,那么它将容易出错并且不是很好的编程风格。所以它必须采用一个类对象。有没有办法做到这一点?

1 个答案:

答案 0 :(得分:0)

您可以创建一个对象来存储这些参数:

self

...在getSearchData中,尝试序列化模型并将其传递给api:

public class FetchDataInput: PagedAndSortedInputDto, IShouldNormalize
{
    public SearchModel searchModel{get; set;}

    public void Normalize()
    {           
        if (string.IsNullOrEmpty(Sorting))
        {
            Sorting = "DataCode";
        }
    }

}

public class SearchModel
{
        public int DataLevel { get; set; }
        public int DataType { get; set; }
        public string DataCode { get; set; }
        public string DescLong { get; set; }
        public string LanguageCode { get; set; }
        public string DataParent { get; set; }
}