我应该如何为多对多关系设计类?

发布于 2024-07-26 17:44:42 字数 2393 浏览 4 评论 0原文

假设我正在为一家制药公司开发软件,其中每个 'ProductionLine' 有多个 'Stages' 并且每个 Stage

现在 有多个 'Machines'假设我正在维护三个表来记录阶段及其机器(将生产线留在一边以方便讨论)。

(1) 阶段(代表任何生产线可能拥有的所有可能阶段的基本数据)

(2) 机器(代表生产工厂可能拥有的所有可能机器的基本数据)

(3) 阶段机器(代表分配给一个生产线的机器数量) stage)

请注意,一个stage可以有多台机器,一台机器可以是多个stage的一部分。 但是 Machine 类不应该有阶段列表,因为根据业务问题域,它是不相关的。

我设计了以下类:

public class Stage
    {
        private int _stageId;
        public int StageID
        {
            get { return _stageId; }
            set { _stageId = value; }
        }

        private string _stageName;
        public string StageName
        {
            get { return _stageName; }
            set { _stageName = value; }
        }

        private List<Machine> myVar;        
        public List<Machine> Machines
        {
            get { return myVar; }
            set { myVar = value; }
        }

        public static bool Save(Stage stage)
        {
            //save code goes here...
        }
    }


public class Machine
    {
        private int _machineId;
        public int MachineID
        {
            get { return _machineId; }
            set { _machineId = value; }
        }

        private string _machineName;
        public string MachineName
        {
            get { return _machineName; }
            set { _machineName = value; }
        }

        public Machine()
        {
        }

        public Machine(int id, string name)
        {
            _machineId = id;
            _machineName = name;
        }
    }

现在我面临着一个困境:

(1)当我创建一个阶段时,我必须从所有机器中选择一些机器并保存数据。 我应该如何在我的代码中处理这个问题,因为那么我应该能够编写以下代码:

Stage s = new Stage();
            s.Machines.Add(new Machine(1, "Machine#1"));
            s.Machines.Add(new Machine(2, "Machine#2"));
            s.Machines.Add(new Machine(3, "Machine#3"));

            Stage.Save(s);

(2)我应该如何在我的代码中维护这种多对多关系? 我应该创建一个名为 'StageMachine' 的第三个类吗? 如果这样做,在创建 Stage 对象时应该如何保存机器?

谁能给我一个解决方案吗?

*** 另外一个问题是,当检索Stage的机器时,我应该如何以及在nTier中的何处进行映射?

对于需要引用其他类的类,C# 中的良好设计模式是什么?

此链接讨论了类设计问题,但没有回答 NTier 设计中我的 Stage 对象的 Machines 的保存和检索机制。

Suppose I am developing a software for a pharmaceutical company where each 'ProductionLine' has multiple 'Stages' and each Stage has multiple 'Machines'

Now suppose I am maintaining three tables to record Stages and its Machines (leave the ProductionLine away for the shake of the discussion).

(1) Stage (Basic data which represents all possible Stages any production line can have)

(2) Machine (Basic data which represents all possible machines the production-factory can have)

(3) StageMachines (Represents a number of machines assigned for a stage)

Please note that, a stage can have multiple machines and a machine can be a part of multiple stages. But a Machine class shouldn't have a list of Stages, coz it is irrelevant accoring to the bussiness problem domain.

I have the following classes designed:

public class Stage
    {
        private int _stageId;
        public int StageID
        {
            get { return _stageId; }
            set { _stageId = value; }
        }

        private string _stageName;
        public string StageName
        {
            get { return _stageName; }
            set { _stageName = value; }
        }

        private List<Machine> myVar;        
        public List<Machine> Machines
        {
            get { return myVar; }
            set { myVar = value; }
        }

        public static bool Save(Stage stage)
        {
            //save code goes here...
        }
    }


public class Machine
    {
        private int _machineId;
        public int MachineID
        {
            get { return _machineId; }
            set { _machineId = value; }
        }

        private string _machineName;
        public string MachineName
        {
            get { return _machineName; }
            set { _machineName = value; }
        }

        public Machine()
        {
        }

        public Machine(int id, string name)
        {
            _machineId = id;
            _machineName = name;
        }
    }

Now I am facing a dillemma:

(1) When I am creating a Stage, I have to choose some Machines from all machines and save the data. How should I handle this in my code, coz then I should be able to write the following code:

Stage s = new Stage();
            s.Machines.Add(new Machine(1, "Machine#1"));
            s.Machines.Add(new Machine(2, "Machine#2"));
            s.Machines.Add(new Machine(3, "Machine#3"));

            Stage.Save(s);

(2) How should I maintain this many-to-many relationship in my code? Should I create a third class named 'StageMachine'? If I do so, how should I save the machines in when I am creating an Stage object?

Can anyone give me a solution?

*** An additional question is, when retrieving the machines of a Stage, how and where in the nTier I should do the mapping?

What is a good design pattern in C# for classes that need to reference other classes?

This link discusses the class design problem but don't answer the Saving and Retrieving mechanism of Machines of my Stage object in the NTier design.

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

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

发布评论

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

评论(3

谜兔 2024-08-02 17:44:42

尽管与业务问题无关,但机器实际上确实与阶段有关联,最好通过集合来表达。 如果您使用 O/R 映射器,我认为最简单的解决方案是在 Machine 上实现 Stage 集合,但不要公开公开它。 这可能会在以后提供其他优点,例如公开 Count 属性来表示机器使用的阶段数。 我的解决方案是这样的:

public class Stage
{
    private List<Machine> _machines = new List<Machine>();

    public IEnumerable<Machine>
    {
        get { return _machines; }
    }

    public void AddMachine(Machine machine)
    {
        _machines.Add(machine);
        machine.AddStage(this);
    }

    public void RemoveMachine(Machine machine)
    {
        _machines.Remove(machine);
        machine.RemoveStage(this);
    }

    // etc.
}

public class Machine
{
    private List<Stage> _stages = new List<Stage>();

    internal void AddStage(Stage stage)
    {
        _stages.Add(stage);
    }

    internal void RemoveStage(Stage stage)
    {
        _stage.Remove(stage);
    }

    // etc.
}

Although its irrelevant to the business problem, a Machine does in fact have an association to a Stage that is best expressed by a collection. If you're using an O/R mapper, I think the easiest solution is to implement the Stage collection on Machine but don't expose it publicly. This may offer other advantages later, on such as exposing the Count property to represent how many stages a machine is used on. My solution would be something like:

public class Stage
{
    private List<Machine> _machines = new List<Machine>();

    public IEnumerable<Machine>
    {
        get { return _machines; }
    }

    public void AddMachine(Machine machine)
    {
        _machines.Add(machine);
        machine.AddStage(this);
    }

    public void RemoveMachine(Machine machine)
    {
        _machines.Remove(machine);
        machine.RemoveStage(this);
    }

    // etc.
}

public class Machine
{
    private List<Stage> _stages = new List<Stage>();

    internal void AddStage(Stage stage)
    {
        _stages.Add(stage);
    }

    internal void RemoveStage(Stage stage)
    {
        _stage.Remove(stage);
    }

    // etc.
}
尝蛊 2024-08-02 17:44:42

你说过

... Machine 类不应该有阶段列表,因为根据业务问题域,它是不相关的。

这是否意味着您所需要的只是从阶段到机器的一对多关系? 在这种情况下,你所拥有的就足够了。

You've said that

... a Machine class shouldn't have a list of Stages, coz it is irrelevant accoring to the bussiness problem domain.

Does that not mean all you need is a 1-to-many relationship from stage to machine? In that case what you've got would be enough.

魔法唧唧 2024-08-02 17:44:42

如果执行以下操作:

s1.Add(new Machine(1, "Machine#1");
s2.Add(new Machine(1, "Machine#1");

您最终将得到代表相同机器数据的两个不同对象。 相反,您可以拥有一个机器列表或一个 MachineFactory,它为您提供相同的对象引用(给定机器 ID),因此:

Machines.Add(New Machine(1, "Machine#1");
s1.Add(Machines[1]);
s2.Add(Machines[1]);

s1.Add(MachineFactory.GetOrCreate(1)); // maintains its own Machines[] internally

(抱歉,如果“工厂”在这里是错误的术语。基本上,您可以使用一个静态方法来创建每个机器 ID 都有一个单例。)

这就是 OOP 术语中多对多所需的全部内容,因为您提到不需要从机器遍历到其父阶段。 为了简单起见,单独的 StageMachine 类有助于使您的类与关系数据库结构保持一致,或者允许在阶段和机器之间更轻松地进行双向遍历,而无需在 Machine 和 Stage 类中维护冗余列表。

If you do the following:

s1.Add(new Machine(1, "Machine#1");
s2.Add(new Machine(1, "Machine#1");

you will end up with two different objects representing the same machine data. Instead, you can have a List of machines or a MachineFactory that provides you with the same object reference, given a machine ID, so:

Machines.Add(New Machine(1, "Machine#1");
s1.Add(Machines[1]);
s2.Add(Machines[1]);

or

s1.Add(MachineFactory.GetOrCreate(1)); // maintains its own Machines[] internally

(Sorry if "factory" is the wrong term here. Basically you can have a static method that creates a singleton for each machine ID.)

That's all you need for many-to-many in OOP terms, since you mentioned that you don't need to traverse from a Machine to its parent Stage. A separate StageMachine class would be useful to keep your classes aligned with the relational database structure for simplicity, or to allow easier bi-directional traversal between stages and machines without needing to maintain redundant lists in both the Machine and Stage classes.

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