user11528353
user11528353

Reputation:

ASP.NET Using one controller from another

I am creating an mvc project, for simplification i have two entitys: Movies and MoviesGenre.

I want to display a list of genres and the amount of movies each of them contains.

Now i have a problem with the design. I am not sure who is responsible for it. I solved that by creating a method in MovieController that returns the amount of movies by genre id and created a method on the MoviesGenreController that select all the genres and uses the MovieController(By instantiating an object) method to get their count.

That doesn't seems like good design to me. Which controller is responsible for this? Do I maybe need to create an extra controller for this logic? Thanks.

Upvotes: 1

Views: 55

Answers (1)

bre_dev
bre_dev

Reputation: 572

You need a data layer project which will manage the access of each controller to the underlying database. I would suggest the following design:

  • create a library project (DataLayer) project which connects to the database. Potential methods exposed:
  • List GetAllGenres();
  • List GetMoviesByGenre()

You can either inject the DataLayer as a service or just simply allocate a new object in each controller ctor. This is more like a personal preference... The DI approach is more flexible a more in line with the DotNetCore architecture.

Both MovieController and MovieGenreController should use the methods from the DataLayer.

Upvotes: 1

Related Questions