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?
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.