ASP.NET MVC is a presentation framework. MVC stands for Model-View-Controller. Model gives a notion that it is related to data access. However, it is just a data abstraction for the view. It can be thought of a ViewModel.

Apart from MVC, a large ASP.NET application deals with services. It also deals with data access. We need a good project structure. In the post, we build a simple Hello world project. With the assumption that it is a complex application. We retrieve a greeting from the database and display it in the UI.

Data Access Layer

Data access layer has three assemblies:

  1. HelloWorldApp.Entities
  2. HelloWorldApp.Repositories
  3. HelloWorldApp.Repositories.Contracts

Greeting class

We define an entity, Greeting.

IGreetingRepository interface

We have repository classes for data access. For each repository class, we create a corresponding interface.

GreetingRepository class

Our repository class implements the interface defined earlier.

Business Logic Layer

The business logic layer has two assemblies:

  1. HelloWorldApp.Services
  2. HelloWorldApp.Services.Contracts

We do not need the service layer, especially for simple data access. But if the data from relation DB is combined with data from other services, we need service classes.

IGreetingService interface

For each service class, we define an interface.

GreetingService class

Our service class implements the interface defined earlier.

Presentation Layer

The presentation layer has two assemblies:

  1. HelloWorldApp.Models
  2. HelloWorldApp

GreetingModel class

We define a Model for our view. This model looks similar to the entity defined earlier. Entity classes have database mapping information embedded in them. Models are more simple. They don’t need to look like the database table. Optionally, some models have model binder. Model binders take information from a HTTP request and convert it into models or objects.

HomeController class

Our controller class has the Index action method. The action gets a greeting model from the service class. It passes the model to the view.

Index view

The view accepts a GreetingModel and knows how to render it in HTML.

Design idea

We organize the project structure of large applications into layers. Each layer interacts with the layer beneath it. MVC is the topmost layer. Service layers interact with external APIs and other data access layers.

Each layer has one or more assemblies. We usually split the interface and implementation into separate assemblies. This aids unit testing. Unit tests reference only the interface assemblies. We did not cover Dependency injection. The unit tests receive the actual classes to test using Dependency injection. Pass mocks for all other classes.

Download the complete source code. HelloWorldApp

Project structure for a large ASP.NET MVC application
Tagged on:

Leave a Reply

Your email address will not be published. Required fields are marked *