Repository Pattern - Usage Doubt

Asked

Viewed 164 times

2

Good night, you guys.

I was developing a software and started thinking about what would be the best way to implement the Repository Pattern.

I have to return to the data controller of cities and states, for this I have two models in my application.

public class Estado
    {
        public int Id { get; set; }
        public string Nome { get; set; }
        public string Uf { get; set; }
        public virtual ICollection<Cidade> Cidades { get; set; }
    }

   public class Cidade
   {
          public int Id { get; private set; }
          public string Nome { get; set; }
          public string Uf { get; set; }
          public int IdEstado { get; set; }
          public virtual Estado Estado { get; set; }
      }

Then I use MVC , because I access the services and services, the repositories... I started thinking then, would it be better to use an Addressservice and add within it the States and Cities ? Or Create Statessrvice and Citiesrvice ? Or even use Addresservice, but inside also have an Addresscorepository that would return me States and Cities ?

I would like to know from you, how you see the best approach and why.

Thank you!

  • 1

    Do you use any OR/M for data access? If you are, I recommend that you do not use repostiory Pattern as an abstraction layer and use OR/M itself as abstraction. So you don’t lose important properties of your OR/m

  • I use the Entity, how this abstraction would work ?

  • I think Eduardo’s idea is to directly access the Entity from the controllers, with no repository or services to guide the repository. Many people who use MVC defend this architecture.

  • Exactly, use the Entity directly in your services, so you can better use the EF resources without the limitation given by the repositories or daos

  • But this way I get some types of logic centralized in my controller, without being able to reuse in other platforms (a mobile for example). ).

1 answer

1


It is possible and common to use one service per entity. However, an aggregate root service is also used. That is, for each set of relations you elect a root, or main class, and create services for each of these sets.

I think this might be useful: DDD-Introduction.

In your case, then, I think I’d create a EndereçoService that would address everything related to this set, accessing the State and City repositories and others.

  • I try to do this when given, however, in this example quoted, I would have a User and the user would have a list of addresses. For me to search all user-free addresses through a user service would not be cool, hence I create services for them, which serve only search...

Browser other questions tagged

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