What to do when a model has N responsibilities

Asked

Viewed 65 times

7

My system exists a class that is currently a common class for several situations, below some of the other models to exemplify

public class Servico
{
   public int Id {get;set;}
   public string Nome {get;set;}
   public decimal? Valor {get;set;}
}
public class Coleta : Servico
{
    //dados da colheita
}
public class Faturamento
{
    public ICollection<Servico> Servicos {get;set;}
}

Then this Service may be registered as

var servico = new Servico("Coleta de sangue",5.00);

Service is a model common, only for no repetition of records and for the user to choose in a select or services, without the need to re-register

But the Collection is also a service, which may or may not be carried out in the company, if it is carried out within the company, it will have its other fields...

But how can I stop when I’m making multiple collections, my Service Records List doesn’t get huge? That is, the model Servico is just a simple reference, already the Coleta, is a more complete model, that there may be actions within it

[Edit]

A step-by-step example of the process.

  1. Registration of the service (Blood collection)
  2. Referenced in a work order
  3. If done in the company, the TELA is its own, with other fields, etc, represented by the Coleta class
  4. Go to billing as a Service

What I’m doing, I leave in a constant the name of the Blood Collection Service, I do not let the user edit, and when I submit a service order, I submit a class of Coleta.cs if done in the company (has view one checkbox whether it is done there or by third parties)

Could also have inside the class Coleta.cs, one ServicoId, then select it, when doing the Collection, however it is boring and bad for the user, since it is the Blood Collection and not another service, also, if it has many services, it is bad to be looking for.

  • This seems to be Entity Framework code. If you put tags, I can try to answer.

  • @Ciganomorrisonmendez yes, can also be valid in the modeling of db same, already edited

2 answers

3


In the Entity Framework, in the case of inheritances, the table is created with the name of the ancestral class, with all derived class fields, and a more named column of discriminator. The filling of this extra column is done by the framework.

To deal specifically with a Service or Collection, create a DbSet<> for each:

public DbSet<Servico> Servicos { get; set; }
public DbSet<Coleta> Coletas { get; set; }

If you extract the sentence generated from any IQueryable<Coleta>, will see that selects, inserts, updates and deletes reference Servicos, and not Coletas.

Now, if you don’t want to use two DbSet<>, no problem: the operator OfType<> solves which class you’re looking to select:

var coletasDeServicos = db.Servicos.OfType<Coleta>().Where(...).ToList();

1

That will depend on your business rule.

When will you add Collections your list of services you can perform this treatment.

For example, you can only have 10 collections in the list, so you treat the method you use to add. When adding check if there are many collections and make the desired decision (can be delete the older collections or stop including).

Now if you work in the database, you can add, but when consulting you can consult only a certain amount. Procedure known to paging.

  • It is not well pagination, doubt ta more for modeling and business rule...

Browser other questions tagged

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