Entity Framework Model and Domain Class

1

The Entity Framework notation is "very polluted" if you take into account how I am used to defining the Domain classes, I wanted to know if it is correct to have an EF template class to represent the database data and one to represent the domain class.

EF model class example:

User Class

public class UsuarioDominio
{        
    public string Matricula { get; set; }
    public string Nome { get; set; }                
    public string Login { get; set; }
    public virtual List<Atividade> AtividadesResponsavel { get; set; }
}

Class Activity

public class Atividade
{        
    public int ID { get; set; }
    public string Titulo { get; set; }
    public string Descricao { get; set; }
    public DateTime DataInicio { get; set; }
    public string ResponsavelID { get; set; }
    public virtual UsuarioDominio Responsavel { get; set; }
    public string Status { get; set; }
}

Normally used class example:

User Class

public class UsuarioDominio
{        
    public string Matricula { get; set; }
    public string Nome { get; set; }                
    public string Login { get; set; }
}

Class Activity

public class Atividade
{        
    public int ID { get; set; }
    public string Titulo { get; set; }
    public string Descricao { get; set; }
    public DateTime DataInicio { get; set; }
    public UsuarioDominio Responsavel { get; set; }
    public char Status { get; set; }
}

From what I understood from the EF the class which will be the "template for the table" should have the same properties as the table as FK's (ResponsavelID), for example. In addition to being necessary to add in the other class the virtual List since it has the relationship of 1: N.

I'm still learning and maybe I'm talking nonsense, but what I understand is more or less how it works (I even find it redundant to have the Respondent ID and the virtual Respondent UserName).

The question is, do I use the Domain class as I am accustomed to, use only the EF model or can I use both? Also, is there the possibility of using only the domain and mapping the Map of each class so that it does not pollute the code of the Domain class?

    
asked by anonymous 29.07.2016 / 15:47

2 answers

1
  

..., I wanted to know if it's OK to have an EF template class to represent the database and one to represent the domain class.

It is incorrect . Using two distinct classes, you will bring to the system:

  • Object ambiguity: Model contains not only the data and relationships between other entities, but also the validation rules, both in the presentation and on the server. By creating two classes, the work is not only useless as it can create distinct behaviors in the data flow.
  • Over-Engineering : There will be an unnecessary level of complexity that only serves to increase compile time, linking, and memory usage;
  • Anti-default: Microsoft does not use this type of architecture in the default project template that it publishes. There is no reason for you to use either.
  

I'm still learning and maybe I'm talking nonsense, but what I understand is more or less how it works (I even find it redundant to have the Respondent ID and the virtual Respondent UserName).

It is not. The idea of setting the foreign key data field allows you to define the data type of the column, which does not necessarily have to be int .

  

The question is, do I use the Domain class as I am accustomed to, use only the EF model or can I use both?

Use only the Model of the Entity Framework.

  

Taking advantage of this, is it possible to use only the domain name and map the map of each class so that it does not pollute the domain class code?

I do not understand your concept of "pollution". The decoration of properties, classes and methods is a powerful tool to enrich the behavior of your application in a simple and succinct way.

To exist, there is the possibility, but there is no reason for it, but to generate prolixity in its development.

    
29.07.2016 / 16:58
1

You can use both, but you would have to be converting one to the other and this would generate unnecessary work.

Yes, you can use the mappings separately. An example would be: link

I do not know if it's because I'm used to Hibernate from Java, but I find it much more practical all together in one place.

    
29.07.2016 / 16:29