Entity Framework Template and Domain Class

Asked

Viewed 179 times

1

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

Example of RU model class:

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; }
}

Activity class

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; }
}

Class example used normally:

User class

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

Activity class

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 EF the class that will be the "model for the table" must have the same properties as the table as FK’s (Responsavelid), for example. Besides 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 from what I understand is more or less how it works (I even find it redundant to have the int Responsavelid and the virtual User Domain Responsavel).

The question is, do I use the Domain class as I’m used to, use only the EF model or is it possible to use both ? Also taking advantage, there is the possibility of using only the domain and mapping by Map of each class so that does not pollute the code of the Domain class ?

2 answers

1

1


..., wanted to know if it is correct to have an EF model class to represent the database data and one to represent the domain class.

IS incorrect. Using two distinct classes, you bring to the system:

  • Ambiguity of objects: the Model contains not only data and relationships between other entities, but also validation rules, both in the presentation and on the server. By creating two classes, work is not only useless but can create distinct behaviors in the data stream.
  • Over-Engineering: there will be an extra level of unnecessary complexity, which only serves to increase build time, linkage and memory usage;
  • Anti-standard: Microsoft does not use this type of architecture in the standard model of projects published by it. There is no reason for you to use either.

I’m still learning and maybe I’m talking nonsense, but from what I understand is more or less how it works (I even find it redundant to have the int Responsavelid and the virtual User Domain Responsavel).

It is not. The idea of defining the foreign key data field allows you to define which type of column data need not be necessarily int.

The question is, do I use the Domain class as I’m used to, use only the EF model or is it possible to use both?

Use only the Model of the Entity Framework.

Also taking advantage, there is the possibility of using only the domain and mapping by Map of each class so that does not pollute the code of the Domain class?

I don’t understand your concept of "pollution". The decoration of properties, classes and methods is a powerful tool to enrich the behavior of your application simply and succinctly.

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

  • Thanks for the help, I did not answer before, because I was on holiday kkk. There is only one part that I still don’t understand very well, I use the virtual public to access the properties of the associated table from there, right ? Because as I said putting the Responsavelid I am setting the foreign key, will see an int if I access it, while in virtual access the "object" itself, correct? In this case, I need to have both since it is necessary I define the foreign key or just leaving the virtual and mapping as foreing key an attribute is also valid?

Browser other questions tagged

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