我应该如何为多对多关系设计类?
假设我正在为一家制药公司开发软件,其中每个 '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中的何处进行映射?
此链接讨论了类设计问题,但没有回答 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尽管与业务问题无关,但机器实际上确实与阶段有关联,最好通过集合来表达。 如果您使用 O/R 映射器,我认为最简单的解决方案是在 Machine 上实现 Stage 集合,但不要公开公开它。 这可能会在以后提供其他优点,例如公开 Count 属性来表示机器使用的阶段数。 我的解决方案是这样的:
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:
你说过
这是否意味着您所需要的只是从阶段到机器的一对多关系? 在这种情况下,你所拥有的就足够了。
You've said that
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.
如果执行以下操作:
您最终将得到代表相同机器数据的两个不同对象。 相反,您可以拥有一个机器列表或一个 MachineFactory,它为您提供相同的对象引用(给定机器 ID),因此:
或
(抱歉,如果“工厂”在这里是错误的术语。基本上,您可以使用一个静态方法来创建每个机器 ID 都有一个单例。)
这就是 OOP 术语中多对多所需的全部内容,因为您提到不需要从机器遍历到其父阶段。 为了简单起见,单独的 StageMachine 类有助于使您的类与关系数据库结构保持一致,或者允许在阶段和机器之间更轻松地进行双向遍历,而无需在 Machine 和 Stage 类中维护冗余列表。
If you do the following:
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:
or
(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.