Search code examples
aspnetboilerplateasp.net-boilerplate

How to pass object in method to call service API in ASP.NET Boilerplate?


I have defined a method defined in AppService file, Signature of method is public PagedResultDto<FetchData> GetSearchData(FetchDataInput searchInput). I'm calling this method from Angular code, but service-proxoes.ts file has generated method in which I need to pass all the parameters one by one.

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 file:

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

So I have to call the getSearchData method by the following way.

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

So I have to pass all the parameters, but if let's say there are 100 parameters, it will be error prone and not good programming style. So it has to take a class object that's it. So is there any way to do the this?


Solution

  • You can create a object to store those parametter like this:

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

    ...And in the getSearchData, tried to serialize the model and pass it to your api:

    _$SearchModelForm= _modalManager.getModal().find('form[name=SearchForm]');
    var searchModel = _$SearchModelForm.serializeFormToObject();
    this._dataService.getSearchData(searchModel);