clean architecture in mvc and web api combine using dapper how to add reference
infrastructure , application , domain
To implement Clean Architecture in an ASP.NET Core project combining MVC and
Web API using Dapper, you need to reference different layers (Infrastructure,
Application, Domain) in your main projects (MVC and Web API). This allows each
project to follow separation of concerns while remaining decoupled.
Here's how you can add references between projects and structure the solution:
Step-by-Step Process for Adding References to Infrastructure, Application, and
Domain
1. Create Solution Structure
1. Create the Solution:
o In Visual Studio, create a new solution for your project.
o Add the following class library projects to represent each layer:
▪ Domain: Holds business logic entities and interfaces.
▪ Application: Contains use cases, services, and DTOs.
▪ Infrastructure: Contains database/repository implementations
(Dapper).
▪ Presentation:
▪ Web API: A project for Web API controllers.
▪ MVC: A project for MVC controllers and views.
Example:
2. Add Project References
Domain → Application → Infrastructure → Web API/MVC
Each project will reference the appropriate layers to ensure dependencies follow the
flow of Clean Architecture.
1. Reference Domain in Application:
o Right-click on the Application project in Visual Studio.
o Choose Add > Project Reference....
o Select the Domain project and click OK.
2. Reference Application in Infrastructure:
o Right-click on the Infrastructure project.
o Choose Add > Project Reference....
o Select the Application project and click OK.
3. Reference Application and Infrastructure in Web API and MVC Projects:
o For both WebApi and MvcApp projects:
▪ Right-click on each project.
▪ Choose Add > Project Reference....
▪ Select Application and Infrastructure projects and click OK.
3. Implement Dependency Injection in Web API and MVC
Both the WebApi and MvcApp projects should configure Dependency Injection (DI)
in the Program.cs or Startup.cs files to use services and repositories from the
Application and Infrastructure layers.
Here's how you configure DI in WebApi and MvcApp:
WebApi - Program.cs
Step 4: Add Dependencies and Configure Dapper in Infrastructure Layer
In Infrastructure project, add Dapper and any required database packages (e.g.,
Microsoft.Data.SqlClient) via NuGet.
1. Install Dapper:
o Right-click on the Infrastructure project in Visual Studio.
o Choose Manage NuGet Packages....
o Search for Dapper and install it.
2. Configure Repositories in the Infrastructure Layer:
Step 5: Test the Setup
1. Run the Web API Project:
o Open Swagger UI or Postman to test API endpoints.
o The Web API controllers will call the application layer services.
2. Run the MVC Project:
o Open the browser and test the MVC controllers and views.
o The MVC controllers will interact with the application layer in the
same way.
Conclusion
By following this approach, you set up a clean and scalable architecture in your
solution, where the Web API and MVC projects can leverage common business
logic (services) and repositories (Dapper). The Infrastructure, Application, and
Domain layers are decoupled, allowing for flexibility, maintainability, and
testability.