Разница в производительности одного запроса и нескольких запросов в угловых - PullRequest
0 голосов
/ 16 ноября 2018

У меня угловой проект 6. И у меня есть форма с 20 combobox и текстовым полем, как на картинке ниже. Итак, я связываю элементы списка при загрузке страницы. Есть два подхода для этого, как показано ниже.

ИЗОБРАЖЕНИЕ В МОЕЙ ФОРМЕ

ПЕРВЫЙ ПОДХОД:

.ts file

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 file

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 голосов
/ 16 ноября 2018

Это зависит от ваших требований, если вы уверены, что вам нужно будет часто собирать все типы списков (посмотрев на изображение, я порекомендую второй, так как вам нужны все вместе), затем используйте второй метод и измените его. принять параметр типа param и записанный тип в словарь, как показано ниже, в противном случае используйте первый подход

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

затем в вашем пользовательском интерфейсе вы можете легко дифференцировать список по ключу и, если нужно, получить конкретный тип списка, чем отправить его в качестве параметра

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...