Context in services and repositories

1

In the company I work with we use asp.net mvc divided into 2 projects:

  • web - (base)
  • service - (business rules)

In several places I've noticed other programmers using the direct context (which extends DbContext) in the services and I've already seen also using a context where it contains all the registered repositories:

Context Example (DbContext)

var contexto = new DatabaseContext();
contexto.Produtos....
contexto.Pessoas....

Example repositories

contexto.ProdutosRepositorio...
contexto.PessoasRepositorio...

Now come the doubts:

  • Why and in which cases to use each?
  • In the second example why not use a generic repository? is not it more practical?

Enjoying ...

  • Is it a bad practice to instantiate another and make use of it?
asked by anonymous 29.08.2014 / 20:41

3 answers

1
  

Why and in which cases should I use each?

I believe that using DbContext directly, you end up getting caught / stuck technology, being a disadvantage if you need to change in the future.

Another disadvantage of using DbContext directly in the service is that your service involves business rules, already DbContext is related to a technology. So if you change the technology, you'll need to tweak the code where your business rules are, that's not cool.

  

In the second example why not use a generic repository? is not it more practical?

Repository isolates domain (business-related) objects from access code details and mapping these objects to the database. That is, it adds a layer of separation between the data access and domain layers.

In the case of the project you are developing, you would have to create another project in your solution to implement this design pattern . I believe it is more practical and recommendable to use this pattern.

  

Is it bad practice to instantiate another service and make use of it?

No , each service has its responsibility and in many cases one service may need another service.

Example: In a scenario where a Customer takes a draw on your account, we assume that the Customer has a service called CustomerService.

public class ClienteServico
{
   //...
   public void RealizarSaque()
   {
      //Nesse ponto pode ser que o cliente não tenha saldo para saque
      //e você precisa de um serviço de um objeto ContaCorrenteServiço
      //por exemplo para retornar/verificar o saldo antes do saque
   }
   //...
}

Following this reasoning, I would recommend that you also work with dependency injection and control inversion of repositories / services, according to Thiago Custodio's response (but not only in the controllers, also in their services that will depend on queries to repositories, etc. .).

    
01.09.2014 / 22:50
0

The ideal would be to inject the repositories / services into the controllers. In this way, you can automate tests without actually hitting the bank.

link

    
29.08.2014 / 21:10
0

In my opinion the second way is correct!

Forget DI does not make up!

As for the generic repository is exactly what the name says ... you can use a generic or repository for each template, adapt it according to the process and evolution of your project.

If you choose to use the generic repository in the future when creating a specified repository you need to refactor.

    
01.09.2014 / 14:22