我知道什么事情正在发生以及为什么它会抛出一个错误(它没有找到GetBrokenRules方法,因为它的列表)但我在这里发布这个问题的原因是要求更好的设计,有人可以在这里指导我吗?
我正在设施类(List .... / Building / Floor)
错误:
错误3'System.Collections.Generic.List'不包含'GetBrokenRules'的定义,也没有扩展方法'GetBrokenRules'接受类型为'System.Collections.Generic.List'的第一个参数'(是你错过了使用指令或程序集引用?)
>>>上的错误其他如果(Campus.GetBrokenRules()。Count> 0)
有没有更好的方法来设计我的 GetBrokenRules()?
ICampus,IBuilding,IFloor包含以下内容
public interface ICampus
{
List<BrokenBusinessRule> GetBrokenRules();
int Id { get; }
string Name { get; }
}
public interface IFacilities
{
List<BrokenBusinessRule> GetBrokenRules();
List<ICampus> Campus { get; }
List<IBuilding> Building { get; }
List<IFloor> Floor { get; }
}
public class Facilities : IFacilities
{
private List<ICampus> _campus;
private List<IBuilding> _building;
private List<IFloor> _floor;
public List<ICampus> Campus
{
get { return _campus; }
}
public List<IBuilding> Building
{
get { return _building; }
}
public List<IFloor> Floor
{
get { return _floor; }
}
public Facilities(List<ICampus> campus, List<IBuilding> building, List<IFloor> floor)
{
_campus = campus;
_building = building;
_floor = floor;
}
public List<BrokenBusinessRule> GetBrokenRules()
{
List<BrokenBusinessRule> brokenRules = new List<BrokenBusinessRule>();
if (Campus == null)
brokenRules.Add(new BrokenBusinessRule("Facility Campus", "Must have at least one Campus"));
else if (Campus.GetBrokenRules().Count > 0)
{
AddToBrokenRulesList(brokenRules, Campus.GetBrokenRules());
}
if (Building == null)
brokenRules.Add(new BrokenBusinessRule("Facility Building", "Must have at least one Building"));
else if (Building.GetBrokenRules().Count > 0)
{
AddToBrokenRulesList(brokenRules, Building.GetBrokenRules());
}
if (Floor == null)
brokenRules.Add(new BrokenBusinessRule("Facility Floor", "Must have at least one Floor"));
else if (Floor.GetBrokenRules().Count > 0)
{
AddToBrokenRulesList(brokenRules, Floor.GetBrokenRules());
}
}
}
答案 0 :(得分:2)
public List<BrokenBusinessRule> GetBrokenRules()
{
var brokenRules = new List<BrokenBusinessRule>();
// null is not possible because Campus is supplied in the constructor
if (!Campus.Any())
brokenRules.Add(new BrokenBusinessRule("Facility Campus", "Must have at least one Campus"));
else
{
foreach(var campus in Campus)
{
brokenRules.AddRange(campus.GetBrokenRules());
}
}
if (!Building,Any())
brokenRules.Add(new BrokenBusinessRule("Facility Building", "Must have at least one Building"));
else
{
foreach(var building in Building)
{
brokenRules.AddRange(building.GetBrokenRules());
}
}
if (!Floor.Any())
brokenRules.Add(new BrokenBusinessRule("Facility Floor", "Must have at least one Floor"));
else
{
foreach (var floor in Floor)
{
brokenRules.AddRange(floor.GetBrokenRules());
}
}
return brokenRules;
}
就重新设计而言,我首先要摆脱ICampus,IBuilding和IFloor接口以及针对类的编程。我将创建一个声明GetBrokenRules
行为的接口,并让业务类实现它。除此之外,在我看来,校园有建筑物,建筑物有楼层,所以我会这样设计,而不是将这些课程收集到设施课程中。
答案 1 :(得分:0)
我会在构造函数中执行null检查,并将IEnumerables作为参数而不是IList。您可以在IEnumerables上调用ToList(),如果它为null,则可以抛出异常。
我创建了一个带有GetBrokenRules方法的IFacility接口,该方法返回IEnumerable
public interface IFacility
{
IEnumerable<BrokenRules> GetBrokenRules();
}
public static class Utils
{
public static IEnumerable<BrokenRules> GetRules(this IEnumerable<IFacility> facilities)
{
return facilities.SelectMany(x => x.GetBrokenRules());
}
}
然后有建筑物,楼层,校园等实施IFacility接口。
答案 2 :(得分:0)
您对ICampus,IBuilding和IFloor的定义在哪里?这些似乎是IFacilities不可或缺的一部分。此外,您在对象列表上调用GetBrokenRules(),而不是在对象本身上调用。
我将假设ICampus,IBuilding和IFloor已正确定义,而且目前唯一的问题是您在列表中调用该方法。您需要在List中枚举并在每个项目上调用该方法。类似的东西:
Campus.ForEach(c => AddToBrokenRulesList(brokenRules, c.GetBrokenRules()));
(对于.ForEach,您可能需要nVentive Umbrella extensions,我不记得。但是还有其他方法可以对List进行枚举,因此并不重要。)
答案 3 :(得分:0)
GetBrokenRules()不接受任何参数,并且它不是扩展方法,所以它不知道它应该处理什么列表。比这更困难的是,您要处理的所有列表都被定义为不同接口类型的列表,因此没有共性。
处理此问题的一种方法是定义一个新接口,该接口定义IBuilding,ICampus和IFloor的公共处理,并让每个接口都继承自这个新接口(称之为IFacility)。然后,您可以定义GetBrokenRules以获取List的参数,并将其传递给List。 GetBrokenRules可以调用IFacility中定义的方法,这可能足以让你传递当前的问题。
但是,您必须将列表作为参数传递,而不是将它们传递给您(Campus.GetBrokenRules())。如果可以使用扩展方法,则可以使用此语法,但在通用集合上定义扩展方法时可能会很棘手。我现在建议不要这样做。