glenho123
glenho123

Reputation: 577

VS Solution Configuration for Web API / MVC

I've been working in ASP.Net WebForms for the last 10 years and am trying to move myself into some more current technologies. I therefore am just doing a small Todo list project with the following outcomes.

  1. Setup a central SQL DB
  2. Create an ASP.Net Core Web API project to handle data requests
  3. Have a ASP.Net Core MVC project that has a web interface
  4. Create a PWA application to install on both iOS and Android

I have completed steps 1 and 2. I have got to step 3 and not sure how to configure the MVC app. I have a few questions around how I should set this up.

Please note that I have setup the Web API project with .Net Core 3.1 and have used Entity Framework and so therefore have a DB Context.

  1. Should I house the MVC project in the same solution as the Web API project.
  2. If I do house the MVC project in the same solution should I be calling the DBContext directly (c# not javascript) from the WebAPI (setup as a reference) or should I be calling the Web API controllers directly?
  3. If I do house the MVC project and the web API project in the same solution - when I go to deploy this real world will I be able to separate the 2 projects as different hosts so that my WPA can get to the Web API.
  4. Should I have the MVC project completetly seperated and consume the WebAPI project as an external project.

Thanks 2.

Upvotes: 0

Views: 149

Answers (1)

Vladimir
Vladimir

Reputation: 1694

This can be organized in many ways. I can tell you how I would proceed. (Assuming you are moving to .net core world).

I will try to describe how to organize your solution in separate projects, and I will assume you want to use entity-framework, repository and unit of work patterns for DB access.

I would create the following projects in my solution:

  • one .NET Standard project to hold only models (entities and DTO's) - name it "YourNamespace.Models"

  • one .NET Standard project to hold repository and unit of work contracts/interfaces - name it "YourNamespace.Contracts"

  • one .NET Standard project to be your data access layer - name it "YourNamespace.DAL". This is where you install EF Core, where you would have your entities configurations (if using Fluent API), your migrations would be here also (if needed), your repositories implementations, as well as the unit of work and database context.

  • one .NET Standard project for your services - name it "YourNamespace.Services"

  • one API project for your API controllers/REST services - name it "YourNamespace.TodoAPI"

  • one MVC project for MVC controllers - name it "YourNamespace.TodoMVC"

This structure is nice for using dependency injection for your services and anything else you need. Make sure to select the Multiple Startup Projects option under Set StartUp Projects (right-click on your solution). Select both API and MVC as startup projects.

Something similar is described here

Upvotes: 1

Related Questions