【发布时间】:2021-03-29 09:38:21
【问题描述】:
在我目前的课程中,我有以下代码块
int.TryParse(formDataDictionary["CountryId"], out var countryId);
var country = countryId > 0 ? _countriesRepository.GetCountryById(countryId) : null;
var searchedCountryName = country != null ? country.Name : string.Empty;
int.TryParse(formDataDictionary["SubjectId"], out var subjectId);
var subject = subjectId > 0 ? _subjectsRepository.GetFullSubjectDetailsById(subjectId) : null;
var searchedSubjectName = subject != null ? subject.Name : string.Empty;
如您所见,除了使用不同的存储库外,它们几乎相同。
我想将它们放在一个只返回名称字符串的通用方法中,但我不知道如何传入一个 repo 并让它使用特定方法来获取主题或国家/地区。
这是可能的,还是比它的价值更麻烦?
【问题讨论】:
标签: c# .net generics .net-core refactoring