0

我在 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";
        }
    }

}

服务代理.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 个参数,那就容易出错并且编程风格不好。所以它必须接受一个类对象。那么有没有办法做到这一点?

4

1 回答 1

0

您可以创建一个对象来存储这些参数,如下所示:

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; }
}

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

_$SearchModelForm= _modalManager.getModal().find('form[name=SearchForm]');
var searchModel = _$SearchModelForm.serializeFormToObject();
this._dataService.getSearchData(searchModel);
于 2017-09-26T01:20:06.677 回答