一个请求和多个请求在角度上的性能差异

时间:2018-11-16 06:32:27

标签: c# angular performance typescript asp.net-core-webapi

我有一个角度为6的项目。我有一个带有20个组合框和一些文本框的表单,如下图所示。因此,我在页面加载时绑定组合框项目。可以通过以下两种方法进行此操作。

MY FORM IMAGE

第一种方法:

.ts文件

onInit(){
   let countries= this.http.get("getCountries");
   let cities= this.http.get("getCities");
   let states= this.http.get("getStates");
   let universities= this.http.get("getUniversities");
   let elementarySchools= this.http.get("getElementarySchools");
   let studentTypes= this.http.get("getStudentTypes");
   let universityTypes= this.http.get("getUniversityTypes");
   let genders= this.http.get("getGenders");
   let diseaseTypes= this.http.get("getDiseaseTypes");
   let accessTypes= this.http.get("getAccessTypes");
   let appealTypes= this.http.get("getAppealTypes");
   let computerBrands= this.http.get("getComputerBrands");
   let carTypes= this.http.get("getCarTypes");
   let positions= this.http.get("getPositions");
   let workingTypes= this.http.get("getWorkingTypes");
   let travelTypes= this.http.get("getTravelTypes");
   let feedbacks= this.http.get("getFeedbacks");
   let contactTypes= this.http.get("getContactTypes");
   let maritalStatus= this.http.get("getMaritalStatus");
}

ApiController.cs

public List<string> getCountries()
 { return new List<string>{"sgh","shd","dfh"}; }
public List<string> getCities()
 { return new List<string>{"agg","dfh","dfj"}; }
public List<string> getStates()
 { return new List<string>{"ghf","ghj","jhk"}; }
public List<string> getUniversities()
 { return new List<string>{"sdh","dfj","hgj"}; }
public List<string> getElementarySchools()
 { return new List<string>{"jhg","fdh","fsg"}; }
public List<string> getStudentTypes()
 { return new List<string>{"asf","sdf","fdh"}; }
public List<string> getUniversityTypes()
 { return new List<string>{"fdh","asd","tyr"}; }
public List<string> getGenders()
 { return new List<string>{"ewt","ret","uer"}; }
public List<string> getDiseaseTypes()
 { return new List<string>{"asd","zxc","vbn"}; }
public List<string> getAccessTypes()
 { return new List<string>{"wet","wtt","yrx"}; }
public List<string> getAppealTypes()
 { return new List<string>{"asd","zxc","vbn"}; }
public List<string> getComputerBrands()
 { return new List<string>{"asd","zxc","dfh"}; }
public List<string> getCarTypes()
 { return new List<string>{"asd","zxc","sdf"}; }
public List<string> getPositions()
 { return new List<string>{"asd","zxc","bfg"}; }
public List<string> getWorkingTypes()
 { return new List<string>{"dfh","zxc","dhj"}; }
public List<string> getTravelTypes()
 { return new List<string>{"hgj","zxc","jfd"}; }
public List<string> getFeedbacks()
 { return new List<string>{"khg","zxc","vbn"}; }
public List<string> getContactTypes()
 { return new List<string>{"cvn","zxc","gsh"}; }
public List<string> getMaritalStatus()
 { return new List<string>{"dfh","zxc","vbn"}; }

第二种方法:

.ts文件

onInit(){
  this.http.get("getAllComboboxBindingLists");
}

ApiController.cs

public List<List<string>> getAllComboboxBindingLists()
{
   var resultList = new List<List<string>>();
   resultList.Add(new List<string>{"sgh","shd","dfh"});
   resultList.Add(new List<string>{"dfg","fgh","dhh"});
   resultList.Add(new List<string>{"hjf","gfh","sdf"});
   resultList.Add(new List<string>{"sjf","ghj","dfj"});
   resultList.Add(new List<string>{"agg","fdg","fgj"});
   resultList.Add(new List<string>{"nfd","ghj","fdg"});
   resultList.Add(new List<string>{"ghj","dfh","dfj"});
   resultList.Add(new List<string>{"ghk","hjk","ghj"});
   resultList.Add(new List<string>{"agg","yuı","gfh"});
   resultList.Add(new List<string>{"ret","yuı","yuı"});
   resultList.Add(new List<string>{"ghj","dfh","dfj"});
   resultList.Add(new List<string>{"fjj","gfh","tyu"});
   resultList.Add(new List<string>{"set","dfg","dfj"});
   resultList.Add(new List<string>{"yru","tuı","try"});
   resultList.Add(new List<string>{"tru","dfh","hjg"});
   resultList.Add(new List<string>{"agg","wry","dfj"});
   resultList.Add(new List<string>{"ghj","hjk","dfj"});
   resultList.Add(new List<string>{"dfg","fgj","dfj"});
   resultList.Add(new List<string>{"yjg","yır","rjf"});
   resultList.Add(new List<string>{"rwy","tru","ıtu"});
   return resultList;
}

第一种方法的优点和缺点

  • 优势:我们可以在其他页面中使用方法。因为每种方法都有自己的方法。例如,您可以在任何地方调用“ getUniversityTypes”。
  • 缺点:我们向服务器发送了20个请求

第二种方法的优点和缺点

  • 优势:我们仅向服务器发送一个请求
  • 缺点:我们无法使用其他页面上的方法。因为在controller.cs中,只有一种方法可以获取所有值。因此,您不能仅以“ getUniversityTypes”为例

我的问题

这两种方法的性能差异多少?因为,我的应用程序将被数百万人使用,并且将有数百个这样的页面。这种性能差异对我来说非常重要。 (我认为,第一种方法会因为20次请求而对服务器感到厌倦)

1 个答案:

答案 0 :(得分:0)

这取决于您的要求,如果您确定需要经常将所有列表类型汇总在一起(通过查看图片,我将推荐第二个列表类型,因为您一起需要全部列表类型),然后使用第二个方法并对其进行修改接受列表类型的参数和字典的书面类型,类似于下面的方法,第一种方法是

public Dictionary<string,List<string>> getAllComboboxBindingLists(string listTypes)
{
   var resultList = new  Dictionary<string,List<string>>>();
   resultList.Add('carTypes',new List<string>{"sgh","shd","dfh"});
   resultList.Add('studentTypes',new List<string>{"dfg","fgh","dhh"});
   if(listTypes != null)//here listtypes will be a semicolon spllitted string like 'type1;type2'
   {
    var splittedList=listTypes.split(';');
     resultList=resultList.where(r=> splittedList.Contains(r.key))
   }
   return resultList;
}

然后在用户界面上,您可以轻松地通过键来区分列表,如果需要获取特定的列表类型,则可以将其作为参数进行发送