Conceptual doubt. Entity Framework, BLL and OOP

Asked

Viewed 129 times

1

People,

I request urgent help...

I have a system that is running "right". Done in C# (Winform) and SQL Server (no Entityframework). However, due to studies and the need to expand the system (adding new features and improvement in other parts) I am redoing the entire system.

I think I knew OOP and that the system was object oriented. Great innocence on my part.

I’m studying OOP strongly and I realized the atrocities I committed in my system, so I reset it. However, as I’m taking it one step at a time and trying to do everything right from the start so I don’t have to redo it again.

Here is my doubt. I will try to exemplify.

My BLL layer has:

public class PessoaFisica
{
    public string Nome { get; private set; }
    public Enum.Sexo Sexo { get; private set; }
    public DateTime? DataNascimento { get; private set; }
    public int? Idade { get; private set; }
    public Enum.EstadoCivil EstadoCivil { get; private set; }
    public string CPF { get; private set; }
    public string DocumentoIdentificacao { get; private set; }
    public string Nacionalidade { get; private set; }
    public string NacionalidadeComplemento { get; private set; }
    public Enum.Estados NaturalidadeEstado { get; private set; }
    public string NaturalidadeCidade { get; private set; }
    public string Profissao { get; private set; }
    public Endereco Endereco { get; private set; }
}

And some classes that inherit from Personal Physics, as an example, follows a:

public class Genitor : PessoaFisica
{
    public Enum.SituacaoGenitor SituacaoGenior { get; private set; }
    public Enum.Estados FalecimentoEstado { get; private set; }
    public string FalecimentoCidade { get; private set; }
    public string FalecimentoData { get; private set; }
    public string TempoDesaparecimento { get; private set; }
    public bool ApresentaDocIdentificacao { get; private set; } = true;
    public bool NecessidadeTestemunha { get; private set; } = false;
    public bool Assina { get; private set; } = true;
    public bool NecessidadeInterprete { get; private set; } = false;
}

Okay. So far... I don’t think you have much trouble. These classes are reflections of my canvases.

I am using Entity Framework (Code First) for database communication (SQL Server).

For example, I have the following entity in the RU:

public partial class Genitor
{
    public int IdGenitor { get; set; }
    public string Tipo { get; set; }
    public string Nome { get; set; }
    public Nullable<System.DateTime> Nascimento { get; set; }
    public Nullable<int> Idade { get; set; }
    public string Profissao { get; set; }
    public string Nacionalidade { get; set; }
    public string NaturalidadeEstado { get; set; }
    public string NaturalidadeCidade { get; set; }
    public string Falecido { get; set; }
    public string DataFalecimento { get; set; }
    public string FalecimentoEstado { get; set; }
    public string FalecimentoCidade { get; set; }
    public Nullable<int> IdUsuario { get; set; }
    public string Situacao { get; set; }
    public string DocIdentificacao { get; set; }
    public string Endereco { get; set; }
    public string Cidade { get; set; }
    public string Estado { get; set; }
    public string TempoDesaparecimento { get; set; }
    public string ApresentaDocIdentificacao { get; set; }
    public string NecessidadeTestemunha { get; set; }
    public string Assina { get; set; }
    public string NecessidadeInterprete { get; set; }
    public virtual Usuario Usuario { get; set; }
}

This is my question:

Is it right to have two classes for the "same" thing? How so same thing? I explain: Two classes (one in BLL and one in EF) for the Genitor.

In the class that is in BLL, I have specific types of data (Enum, etc) that are "impossible" in EF.

So how to do it? Create a layer between the BLL and DAL to "map" it? Explaining to the EF entity how the BLL class works? I don’t think so... because it would cause a big coupling.

Or just use the EF (DAL) entities for the data... that is, create the Genitor object (EF entity) when you click on the button to save the data and have the EF persist with this data?

I don’t know if I made myself understood correctly... but I have this question.

I couldn’t find examples of systems to see how this is solved.

From now on, thank you for your help.

  • 1

    Young, Enum is considered to be int in the Entity framework. I don’t see any "impossible" data in EF.

  • @jbueno, thanks for the answer. I know an Enum converts to int, but doesn’t that get weird? Being Male and Female... not 0 and 1... the reading of the database data is not impaired?

1 answer

0

I’m not particularly fond of EF, but to answer your question, if the classes are exactly the same, there is a problem, because it shows a fragility in its architecture. That is, if tomorrow or later a new field appears, you will have to change in the two classes, correct?

As stated in the comments, I would map to the class that has the Enum and remove the other.

Note: Map via Fluent API to avoid soiling your domain class with infrastructure code (persistence).

Browser other questions tagged

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