设计领域模型 - 需要帮助

发布于 2024-09-07 00:06:29 字数 2717 浏览 3 评论 0原文

我知道发生了什么以及为什么它会抛出错误(它没有找到 GetBrokenRules 方法,因为它的列表),但我在这里发布这个问题的原因是要求更好的设计,有人可以在这里指导我吗?

我正在处理设施类(List..../Building/Floor)

错误:

错误 3 'System.Collections.Generic.List' 不包含 'GetBrokenRules' 的定义,并且没有扩展方法 'GetBrokenRules' 接受第一个参数 找到类型为“System.Collections.Generic.List”的错误(您是否缺少 using 指令或程序集引用?)

可以在 >>> 上 else if (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());
            }       
    }
} 

i know whats is happening and why its throwing an error (it does not find GetBrokenRules method because its List) but the reason i posted this question here is to ask for a better design, can anybody guide me here please?

i am working on Facilities class (List..../Building/Floor)

error:

Error 3 'System.Collections.Generic.List' does not contain a definition for 'GetBrokenRules' and no extension method 'GetBrokenRules' accepting a first argument of type 'System.Collections.Generic.List' could be found (are you missing a using directive or an assembly reference?)

error on >>> else if (Campus.GetBrokenRules().Count > 0)

is there any better way to desing my GetBrokenRules() ?

ICampus, IBuilding, IFloor consists of the following

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

花开半夏魅人心 2024-09-14 00:06:29
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 行为的接口,并让业务类实现该行为。除此之外,在我看来,校园有建筑物,建筑物有楼层,所以我会这样设计,而不是将这些类收集到设施类中。

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

As far as a redesign, I would first get rid of the ICampus, IBuilding, and IFloor interfaces and program against the classes. I would create an interface that declares the GetBrokenRules behavior and have the business classes implement that. Beyond that, it seems to me that a Campus has Buildings and a Building has Floors so I would design it that way instead of collecting these classes into a Facilities class.

荒路情人 2024-09-14 00:06:29

ICampus、IBuilding 和 IFloor 的定义在哪里?这些似乎是 IFacility 不可或缺的一部分。另外,您是在对象列表上调用 GetBrokenRules(),而不是在对象本身上。

我将假设 ICampus、IBuilding 和 IFloor 已正确定义,并且目前唯一的问题是您正在调用列表中的方法。您需要枚举整个列表并在每个项目上调用该方法。类似于:(

Campus.ForEach(c => AddToBrokenRulesList(brokenRules, c.GetBrokenRules()));

您可能需要nVentive Umbrella .ForEach 的扩展,我不记得了,但是还有其他方法可以枚举列表,所以这并不重要。)

Where are your definitions for ICampus, IBuilding and IFloor? Those seem to be integral to IFacilities. Also, you're calling GetBrokenRules() on a List of objects, rather than on the objects themselves.

I'm going to assume that ICampus, IBuilding and IFloor are defined properly and that, for now, the only problem is that you're calling the method on the list. You'll need to enumerate across the List and call that method on each item. Something like:

Campus.ForEach(c => AddToBrokenRulesList(brokenRules, c.GetBrokenRules()));

(You may need the nVentive Umbrella extensions for .ForEach, I don't remember. But there are other ways to enumerate over a List so that's not critical.)

油饼 2024-09-14 00:06:29

GetBrokenRules() 不接受任何参数,并且它不是扩展方法,因此它不知道应该处理什么列表。比这更困难的是,您想要处理的所有列表都被定义为不同接口类型的列表,因此没有通用性。

处理此问题的一种方法是定义一个新接口,该接口定义 IBuilding、ICampus 和 IFloor 的通用处理,并让每个接口都继承自该新接口(称为 IFacility)。然后,您可以定义 GetBrokenRules 以获取 List 参数,并向其传递一个 List。 GetBrokenRules 将能够调用 IFacility 中定义的方法,这可能足以解决您当前的问题。

但是,您必须将列表作为参数传递,而不是像您现在所做的那样(Campus.GetBrokenRules())。如果您可以使用扩展方法,则可以使用此语法,但在泛型集合上定义扩展方法时,这可能会很棘手。我暂时建议不要这样做。

GetBrokenRules() does not take any arguments, and it's not an extension method, so it has no idea what list it's supposed to process. More difficult than that, all the lists you want to process are defined as lists of different interface types, so there's no commonality there.

One way to handle this would be to define a new interface that defines the common processing for IBuilding, ICampus, and IFloor, and have each of those interfaces inherit from this new interface (call it IFacility). You could then define GetBrokenRules to take an argument of List, and pass it a List. GetBrokenRules would be able to call methods defined in IFacility, which might be sufficient to get passed your current problem.

However, you'll have to pass the list as an argument, rather than they way you're doing it now (Campus.GetBrokenRules()). You could use this syntax if you can get an extension method to work, but that can be tricky when defining extension methods on generic collections. I'd advise against that for now.

年少掌心 2024-09-14 00:06:29

我会在构造函数中进行 null 检查,并采用 IEnumerables 作为参数而不是 IList。您可以在 IEnumerables 上调用 ToList() ,它的好处是如果它为 null 则抛出异常。

我将创建一个 IFacility 接口,其中包含返回的 GetBrokenRules 方法和 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 接口。

I would do the null checks in the constructor and take IEnumerables as argument instead of IList. You can call ToList() on the IEnumerables which has the side benefit of throwing an exception if it's null.

I'd create an IFacility interface with a GetBrokenRules method that returns and 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());
    }
}

Then have building, floor, campus, etc implement the IFacility interface.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文