方法中的类耦合警告
本文关键字:耦合 警告 方法 | 更新日期: 2023-09-27 17:58:57
我收到了下面的代码分析警告消息,我不知道该如何解决这个问题。请告知。
CA1506避免过度类耦合
"RetailerController.RetailerFormSummary(string)"与来自13个不同命名空间的33个不同类型耦合。重写或重构该方法以减少其类耦合,或者考虑将该方法移动到与其紧密耦合的其他类型之一。等级耦合高于40表示可维护性差,等级耦合在40和30之间表示可维护程度中等,等级耦合低于30表示可维护度良好。OnePlaceWebApp.Web RetailerController.cs 564
RetailerFormSummary(字符串)的方法如下:
public ViewResult RetailerFormSummary(string retailerId)
{
AlertService alertService = new AlertService();
PageInfo winnersPageInfo = new PageInfo();
RetailerFormModelSummary model = new RetailerFormModelSummary();
winnersPageInfo.ItemsPerPage = _configurationUtility.SummaryWinnersCount;
winnersPageInfo.PageIndex = 0;
model.CurrencyFormat = _configurationUtility.CurrencyFormat;
model.DateFormat = _configurationUtility.DateFormat;
model.DatePickerFormat = _configurationUtility.DatePickerFormat;
model.Reps = _userService.GetReps(_userService.GetCurrentWebAppUserId());
model.UsesDistricts = _configurationService.GetUsesDistricts();
RetailerDto retailer = _retailerService.SelectRetailer(retailerId);
if (retailer != null)
{
RetailerJurisdictionDto retailerJurisdiction = _retailerSummaryService.SelectRetailerJurisdiction(retailer.Id);
JurisdictionDto territory = null;
JurisdictionDto district = null;
JurisdictionDto region = null;
DateTime? lastVisit = _retailerSummaryService.SelectLastVisitUtcDate(retailer.Id);
if (retailerJurisdiction != null)
{
territory = _jurisdictionService.SelectJurisdictionUsingIdAndType(retailerJurisdiction.JurisdictionId, JurisdictionTypes.Territory);
district = _jurisdictionService.SelectJurisdictionUsingIdAndType(retailerJurisdiction.JurisdictionId, JurisdictionTypes.District);
region = _jurisdictionService.SelectJurisdictionUsingIdAndType(retailerJurisdiction.JurisdictionId, JurisdictionTypes.Region);
}
model.Address1 = retailer.Street1;
if (retailer.Street2 != null)
{
model.Address2 = retailer.Street2;
}
if (retailer.CorporateAccount != null)
{
model.CorporateAccount = FormatService.GetTwoPartName(retailer.CorporateAccount.Number, retailer.CorporateAccount.Name, " - ");
}
model.CorporateAccountName = _termsService.SelectSingularTermValue(TermTypes.CorporateAccount);
model.City = retailer.City;
if (district != null)
{
model.District = FormatService.GetTwoPartName(district.Number, district.Name, " - ");
}
if (lastVisit.HasValue)
{
model.LastVisit = lastVisit.Value.DayOfWeek.ToString() + " " + lastVisit.Value.ToLocalTime().ToString(model.DateFormat, CultureInfo.CurrentCulture);
}
model.Note = _retailerSummaryService.SelectRetailerComment(retailer.Id);
model.Phone = FormatService.FormatPhoneNumber(retailer.Phone);
if (region != null)
{
model.Region = FormatService.GetTwoPartName(region.Number, region.Name, " - ");
}
model.RegionTerm = _termsService.SelectSingularTermValue(TermTypes.Region);
model.RetailerId = retailer.Id.ToString(CultureInfo.CurrentCulture);
model.RetailerName = retailer.BusinessName;
ViewBag.Title = retailer.BusinessName;
model.RetailerNumber = retailer.DisplayNumber;
model.RetailerAlerts = alertService.GetRetailerAlertsStringArray(retailer.RetailerAlerts);
model.Route = _routeManagerService.GetRouteStringForRetailer(retailerId);
model.Tasks = _retailerSummaryService.SelectRetailerTasks(retailer.Id);
if (territory != null)
{
model.Territory = FormatService.GetTwoPartName(territory.Number, territory.Name, " - ");
}
model.TerritoryTerm = _termsService.SelectSingularTermValue(TermTypes.Territory);
model.WeeklySales = _retailerSummaryService.SelectWeeklySales(retailerId);
model.Winners = _retailerWinnersService.SelectWinners(retailerId, WinnersSortTypes.Date, winnersPageInfo);
model.ZipCode = retailer.ZipCode;
}
return View("RetailerFormSummary/RetailerFormSummary", model);
}
此警告告诉您,在方法中实例化依赖的具体对象可能会导致可维护性问题。
以下是一些例子:
AlertService alertService = new AlertService();
PageInfo winnersPageInfo = new PageInfo();
RetailerFormModelSummary model = new RetailerFormModelSummary();
RetailerJurisdictionDto retailerJurisdiction = _retailerSummaryService.SelectRetailerJurisdiction(retailer.Id);
JurisdictionDto territory = null;
JurisdictionDto district = null;
JurisdictionDto region = null;
此方法有太多的责任,并且拥有太多的依赖项。您肯定应该查找依赖反转原则,也许还应该查找控制反转框架来帮助进行依赖注入。这将有助于减少类/方法中的耦合。
口头禅是"依赖抽象"。
此外,单一可赔偿原则也适用于此。