0
I am having to make a relationship between classes N:N but storing objects in memory, follows the following relationship:
I did the implementation as follows and I would like to know if it is correct or not.
Chemical class
public class Chemical
{
public int ID { get; set; }
public string ChemicalName { get; set; }
public string ChemicalFormula { get; set; }
public Decimal MW { get; set; }
public int VFId { get; set; } // Visual_Flow_ID
public Boolean ProjectChemical { get; set; }
public Chemical()
{
}
Stream class
public class Stream
{
public int ID { get; set; }
public string Name { get; set; }
public virtual IList<StreamComposition> Chemicals { get; set; }
public Stream()
{
this.Chemicals = new List<StreamComposition>();
}
public void AddChemical(StreamComposition sc)
{
this.Chemicals.Add(sc);
Console.WriteLine("Adding Chemical: " + sc.Chemical.ChemicalName +
" to the Stream: => " + sc.Stream.Name + "\n");
}
}
Streamcomposition class
public class StreamComposition
{
public virtual Stream Stream { get; set; }
public virtual Chemical Chemical { get; set; }
public Decimal MolePerCent { get; set; }
public StreamComposition()
{
}
}
Populating the objects
Creation of Stream
Stream Stream1 = new Stream() { ID = 1, Name = "SHD4", };
Creation of the 3 Chemicals objects'
Chemical ch1 = new Chemical() // Criando uma 'Chemical'
{
ID = 1,
ChemicalName = "Hidrogenio",
ChemicalFormula = "H",
MW = 2,
VFId = 2,
ProjectChemical = true
};
Chemical ch2 = new Chemical() // Criando outra 'Chemical'
{
ID = 2,
ChemicalName = "Ferro",
ChemicalFormula = "Fe",
MW = 4,
VFId = 8,
ProjectChemical = false,
};
Chemical ch3 = new Chemical()
{
ID = 3,
ChemicalName = "Agua",
ChemicalFormula = "H20",
MW = 9,
VFId = 10,
ProjectChemical = true,
};
Creation of the 3 associative classes
StreamComposition sc1 = new StreamComposition() // Criando StreamComposition
{
Stream = Stream1,
Chemical = ch1,
MolePerCent = 4.0m,
};
StreamComposition sc2 = new StreamComposition() // Criando StreamComposition
{
Stream = Stream1,
Chemical = ch2,
MolePerCent = 8.0m,
};
StreamComposition sc3 = new StreamComposition() // Criando StreamComposition
{
Stream = Stream1,
Chemical = ch3,
MolePerCent = 8.0m,
};
Assigning Chemicals created for Stream 'Stream1'
Stream1.AddChemical(sc1);
Stream1.AddChemical(sc2);
Stream1.AddChemical(sc3);
What would this Stream be on your domain? I was in doubt.
– Gabriel Coletta
Stream (Stream) in the case is a class that will have a list of Chemicals
– Renan Narciso
Did not miss adding in class Chemical the reference to Streamcomposition, thus
public virtual IList<StreamComposition> Chemicals { get; set; }
?– Ricardo Pontual
Is it an academic exercise? Because the framework already has a Stream class, look for Memorystream.
– Gabriel Coletta
@Gabrielcoletta I think in this case is not a data stream, by the properties of the class, should be a chemical flow
– Ricardo Pontual
Maybe the translation is not good, but Lacked add list references in Chemical and Stream Classes, as @Ricardopunctual said
– Marco Vinicius Soares Dalalba