Correct Statement Classes Model MVC Ninject

6

I started using OO a short time ago and in all projects that I see on the net, I see the following way of declaration.

public class Trabalhador
{
    public int Id { get; set; }
    public string Nome { get; set; }
    public virtual ICollection Dependente { get; set; }
    public virtual ICollection Dependente { get; set; }
    public virtual ICollection LicencaMedica{ get; set; }
}

public class Dependente
{
    public int ID { get; set; }
    public string Nome { get; set; }
    public virtual Trabalhador Trabalhador { get; set; }
}

public class Ferias
{
    public int ID { get; set; }
    public string Nome { get; set; }
    public virtual Trabalhador Trabalhador { get; set; }
}

public class LicencaMedica
{
    public int ID { get; set; }
    public string Nome { get; set; }
    public virtual Trabalhador Trabalhador { get; set; }
}

In the meantime I am working on a project together with other developers, and one of them rightly argues that my classes should not have the "virtual" as navigation attribute, he argues that the classes Dependent, Fairs and License by Since they already have an ICollection in Worker, they should not have any keys, because they will all be accessible from the working class, is this correct from the point of view of OO? I explained my opinion and said that this was wrong, because it would be kind of creating a Root class and everything would pass through it without need, rather than overloading a class with a lot of responsibility, his approach would be as follows:

public class Trabalhador
{
    public int Id { get; set; }
    public string Nome { get; set; }
    public virtual ICollection Dependente { get; set; }
    public virtual ICollection Dependente { get; set; }
    public virtual ICollection LicencaMedica{ get; set; }
}

public class Dependente
{
    public int? ID { get; set; }
    public string Nome { get; set; }
}

public class Ferias
{
    public int? ID { get; set; }
    public string Nome { get; set; }
}

public class LicencaMedica
{
    public int? ID { get; set; }
    public string Nome { get; set; }
}

Is this correct from the point of view of OO? For me to have access to a child class object should I necessarily pass through the parent class? According to him the Ninject which accuses error in the system, because the fact of instantiating the parent class and then instantiating the daughter class according to it would loop in the system, so it starts all the navigation properties of the child classes.

(We are not using Entity, we are using ADO with SP in the Bank). I tried to be as specific as possible, what is the problem with each approach?

    
asked by anonymous 19.05.2014 / 01:55

1 answer

5

Not that the approach is wrong, but it is expensive, computationally speaking.

Since the properties of the foreign keys in the relations of 1 to N causes the application to have to necessarily load the entire object, which, even with caches and everything else, causes the application to slow down. For performance, your colleague's proposal sucks.

Having the foreign key properties in the class does not contradict the OO proposal because an entity class maps a database table, that is, all columns. It is wrong when a column is not mapped just because it is foreign key (the object does not canonically represent a record of a table or database collection). In addition, the object can be partially loaded when there is no need to load related entities without any loss of information. For example: if I only want the name of Trabalhador , I do not need to load its dependents. All information about the worker is in the object of class Trabalhador .

Regarding the use of virtual , it is important to clarify: virtual is used for classes that can be derived. That is, if for example a Dependente is derived for other classes (I think of Conjuge or Irmao ), the object would no longer accept the object or the collection. The solution architect would have to make sure that all entity classes are immutable, which honestly does not help development.

By the way, the modeling is not canonical. I propose below one more modeling within the best standards used today for development in :

public class Trabalhador
{
    // Usar apenas 'Id' como nome da propriedade pode gerar confusão na montagem
    // de sentenças comparativas, onde não esteja claro qual objeto está sendo manipulado.
    [Key]
    public int TrabalhadorId { get; set; }
    [Required]
    public string Nome { get; set; }

    public virtual ICollection<Dependente> Dependentes { get; set; }
    // Não é preciso duas ICollection iguais, então comentei a segunda.
    // ICollections são tipadas, com o nome da propriedade no plural.
    // public virtual ICollection<Dependente> Dependentes { get; set; }
    public virtual ICollection<LicencaMedica> LicencasMedicas { get; set; }
}

public class Dependente
{
    [Key]
    public int DependenteId { get; set; }
    // Ter uma chave estrangeira aqui não é errado porque cada propriedade
    // que não seja 'virtual' possui uma coluna correspondente em uma tabela no banco
    // de dados.
    public int TrabalhadorId { get; set; }

    [Required]
    public string Nome { get; set; }
    public virtual Trabalhador Trabalhador { get; set; }
}

public class Ferias
{
    [Key]
    public int FeriasId { get; set; }
    public int TrabalhadorId { get; set; }

    [Required]
    public string Nome { get; set; }
    public virtual Trabalhador Trabalhador { get; set; }
}

public class LicencaMedica
{
    [Key]
    public int LicencaMedicaId { get; set; }
    public int TrabalhadorId { get; set; }

    [Required]
    public string Nome { get; set; }
    public virtual Trabalhador Trabalhador { get; set; }
}
    
19.05.2014 / 03:12