Doubt about relationship implementation N:N using C#

Asked

Viewed 69 times

0

I am having to make a relationship between classes N:N but storing objects in memory, follows the following relationship:

inserir a descrição da imagem aqui

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.

  • Stream (Stream) in the case is a class that will have a list of Chemicals

  • Did not miss adding in class Chemical the reference to Streamcomposition, thus public virtual IList<StreamComposition> Chemicals { get; set; }?

  • Is it an academic exercise? Because the framework already has a Stream class, look for Memorystream.

  • @Gabrielcoletta I think in this case is not a data stream, by the properties of the class, should be a chemical flow

  • 1

    Maybe the translation is not good, but Lacked add list references in Chemical and Stream Classes, as @Ricardopunctual said

Show 1 more comment
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.