Implementing relationship in C#

Asked

Viewed 125 times

3

I have the following classes:

inserir a descrição da imagem aqui

The code is like this:

public class Mae
{
   public string Nome {get; set;}
   public List<Filho> Filhos {get; set;}
}
public class Filho
{
   public string Nome {get; set;}
}

In this case, it is possible to obtain all the sons of bitches, but it is not possible to get the mother of children.

Ex.:

var mae = new Mae();
mae.Nome = "Maria";
mae.Filho = new List<Filho>();

var filho = new Filho();
filho.Nome = "Joãozinho"

mae.Filho.Add(filho);

filho.Mae // O objetivo é acessar a mãe...

How could you do that?

  • 1

    What’s the idea? Using Entity Framework?

  • No @Ciganomorrisonmendez , I’m creating the classes on hand same. But I wanted the operation similar to the Entity Framework, because I found it very beautiful!

2 answers

5


The only way is to have a reference to the mother:

public class Mae {
   public string Nome {get; set;}
   public List<Filho> Filhos {get; set;} = new List<Filho>(); //C# 6
}
public class Filho {
   public string Nome {get; set;}
   public Mae Mae {get; set;}
}

You can create some abstractions. You can create constructs or other auxiliary methods that automate the process a little. If every child has to have a mother, it can guarantee this in the builder and can even already make the inclusion of the child in the mother.

public class Filho {
   public Filho(String nome, Mae mae) {
       Nome = nome;
       Mae = mae;
       mae.Filho.Add(this);
   }
   public string Nome {get; set;}
   public Mae Mae {get; set;}
}

var filho = new Filho("João", mae);

I put in the Github for future reference.

See about builders.

  • but so, in raising the child I have to point out who the mother is, correct?

  • Yes, I edited it to make it clearer. You can even do it a little differently, but you can’t escape it. It even makes sense to do so. If by chance the Son can be without a mother, he has to make some adaptation to allow this.

  • 1

    In the model described in your question, @Jedaiasrodrigues, the definition of dependency is clear - there cannot be a Filho without the property Mae. In this case the way bigown specified is correct.

  • @Jedaiasrodrigues stay tuned to the constructors. If you want to guarantee the initial state of the object. I could even use contract to ensure that the state will always be in order. But this is more advanced and few do. You could do some other things. If the hand cannot be changed, then take the set; property. Could do the same for name, if it makes sense for your model.

  • thanks @bigown this was just an adaptation of an example so I could understand the concept... But one last question. When creating a Son I would have a second office of the Mother in memory, or would be used the same object Mother instantiated before?

  • If you do as it is on the last line (whereas the variable mae is already an instance of Mae, as your example, nothing new will be created. Of course, using var filho = new Filho("João", new Mae {Nome = "Maria"}); will create a new instance of Mae, and worse, as it will not keep in any variable, it will be inaccessible directly (it can be accessed through your child).

Show 1 more comment

1

Add a reference to Mae in the child. If it is a dependency, set as parameter in the child constructor.

Following example:

public class Mae
{
   public string Nome {get; set;}
   public List<Filho> Filhos {get; set;}
}

public class Filho
{
   public Filho(Mae mae)
   {
        this.Mae = mae;
   }

   public string Nome {get; set;}
   public Mae Mae {get; set;}
}

Browser other questions tagged

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