DEV Community

mitchelln11
mitchelln11

Posted on

How to Display Junction Tables in .NET Core MVC 3.1

What is the best way to going about shared data with .NET Core? I understand that between the MVC model, you should keep information separated into their related areas.

Ex:
I've been working on a web app that builds Hiker profiles. It also connects to the National Park Service Rest API that populates the National Parks in the US.

All previous teaching tells me to break that down into the following:

Hikers:
-HikerController
-Hiker Model
-Hiker Views (Create, Details, Edit, Delete)

Parks:
-ParkController
-Park Model
-Park Views (Create, Details, Edit, Delete)

That all works great, but what happens when I want to create a Wishlist?

That would need information from the Hikers AND Parks models.

What is the best way to display that info on a view?
I've come across:
-Partial Views
-View Components
-Junction Table MVC (Don't know if there's a name for this), which would have the following

-Wishlist Controller
-Wishlist Model
-Wishlist Views (Create, Details, Edit, Delete)

I've tried, unsuccessfully, on the Partial Views and View Components. The last option seems plausible, but it also seems like overkill. It might fall in line with clean architecture, but then what's the point in View Components or Partial Views.

Thanks

Top comments (6)

Collapse
 
katnel20 profile image
Katie Nelson

What you are missing are the service and repository layers. Don't go directly to the database from your controllers, go through a service layer and have that service work with the database though a repository layer. Then, multiple controllers can share that service.

Collapse
 
mitchelln11 profile image
mitchelln11

So, then would I connect my service layer to my controllers through dependency injection? Just like the SignInManager?

private readonly SignInManager<IdentityUser> _signInManager;

public HikerController(SignInManager<IdentityUser> signInManager)
{
     _signInManager = signInManager;
 }

What actually goes into the repository layers? Methods to call specific database info?

Collapse
 
katnel20 profile image
Katie Nelson

Yes, through DI. The repository layer has the LINQ queries that interact with Entity Framework. The service layer can map the query data with your view models.

Thread Thread
 
mitchelln11 profile image
mitchelln11 • Edited

This is insightful, thank you. I will give it a go, and see what happens!

Is this still technically considered MVC? Seems like it would be MVCSR

Thread Thread
 
mitchelln11 profile image
mitchelln11

Do the default Data and Areas folders already represent the repository and services layers?

My guess would be that the Data would represent the repository layer, and the Area folder would represent the Service layer. Does that seem about right?

Collapse
 
mitchelln11 profile image
mitchelln11

So if I switch from MVC only to a Service-Repository pattern, how to I connect to my database? More specifically, where does my DbSet go? From the start, it's in my ApplicationDbContext class under my Data folder. Would I have to move those at all, or do I just use DI from the DbContext?

private readonly ApplicationDbContext _context;
public ParkController(ApplicationDbContext context)
{
            _context = context;

 }