Implementing the concept of database inheritance

1

Why is not the PessoaJuridicaId attribute generated?

This is the base class Pessoa :

public class Pessoa
{
    public Pessoa()
    {
        DataCadastro = DateTime.Now;
    }
    public int PessoaId { get; set; }
    public DateTime DataCadastro { get; set; }
    public string Observacao { get; set; }
    public bool Ativo { get; set; }
}

This is the class PessoaJuridica that inherits from class Pessoa :

public class PessoaJuridica: Pessoa
{
    public int PessoaJuridicaId { get; set; }
    public string RazaoSocial { get; set; }
    public string NomeComercial { get; set; }
    public string CNPJ { get; set; }
    public string IE { get; set; }
    public string IM { get; set; }
    public string CCM { get; set; }
    public DateTime? DataAbertura { get; set; }
}

This is the Repository class:

public void Salvar(PessoaJuridica pessoaJuridica)
{
    _repositorio.PessoaJuridica.Add(pessoaJuridica);
    _repositorio.SaveChanges();
}
    
asked by anonymous 01.08.2017 / 14:04

1 answer

6

I was just going to comment, but it was long and I think deep down the error is conceptual.

Where is it written that it should be self-increasing? What is the criterion for generating an auto-increment identifier? This will not happen alone. Alone the Entity Framework can only do with the first field, in the case it did with PessoaID which is the first already inherited. In fact it did right, if you really want inheritance.

I find it strange to have different identifiers in both tables and have an inheritance relationship. This only occurs when there is some kind of composition, association, or aggregation , not inheritance. If it is inheritance then legal person is the same as person, they do not exist separately, why should they have different identifiers? I think EF made it right, your model is adding something unnecessary: PessoaJuridicaId .

In general I prefer to do at most a logical inheritance, but to have only the tables of subtypes with the data replicated, which is what happens concretely in memory when we use inheritance in a language, the object is unique, not there are two of the base class and the derivative.

To tell you the truth, this model of physically having a base table and its separate derivatives generates a composition, not a de facto inheritance. Inheritance is a logical thing to reuse code, not to reuse data. In general having this composition of tables does not bring any benefit and brings even harm, so you do not have to use it. Of course there may be some case that might be worth it. But you need to conceptualize right, understand all implications. You can not do it by doing, doing it because it "worked", you have to do it right.

I'm not much of a fan of database inheritance because almost everyone does it wrong. I myself do not know if I know how to do it right, but I know most do it wrong :) Object orientation is already too complicated in memory and people find it not in database anymore yet. You know the story of the drowning person who thinks he can swim. Those who do not know how to swim do not die because they do not try. Who knows how to swim even does not die because he knows. The problem is when people think they can swim and do not know .

You have some questions about it:

01.08.2017 / 14:41