Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

How to Structure a .NET MVC Application

DZone's Guide to

How to Structure a .NET MVC Application

Many if not all development teams have had to restructure/refactor their code at some point. Read on to see how one team tackled this issue.

· Web Dev Zone ·
Free Resource

Deploying code to production can be filled with uncertainty. Reduce the risks, and deploy earlier and more often. Download this free guide to learn more. Brought to you in partnership with Rollbar.

In this article, I will be discussing the .NET MVC package structure which my team designed for a project after many design discussions.

The scope of this package structure is not limited to MVC — you can implement it in ASP.NET (C#, VB) projects, as well.

Problem Statement

Like most .NET MVC applications, we had two layers in our project: Controller and Repository.

  • Controller: Used to validate input and apply business validations.

  • Repository: Used to insert, fetch, update, and delete from DB and also to perform minor data manipulations while performing DB operations.

Over a period of time, our business logic got scattered between these two layers and our controllers became really code heavy, i.e. it became very difficult to change/fix things inside controllers.

The following diagram depicts our old package structure:

Image title

Solution

After facing a lot of issues with maintaining our application, we realized it would be better if we introduced one more layer between the controllers and repositories for handling business logic.

Controller would then be used for input validation and to delegate requests, and th repository would be only be used for performing DB opeartions.


Image title




We then started using this component/business layer for doing all of our business validations, the complete business logic was shifted in this layer and was used for throwing custom exceptions.

You must be wondering how it look with multiple controllers.

For multiple controllers it would look as follows :

Image title

The above package structure follows the Facade Pattern closely.

I have added a project on my public GitHub account that follows the above package structure.

You can download the code and have a look at it.

https://github.com/shivi146/Helpdesk

In the above project, I have also implemented Log4Net and Dependency Injection.

Deploying code to production can be filled with uncertainty. Reduce the risks, and deploy earlier and more often. Download this free guide to learn more. Brought to you in partnership with Rollbar.

Topics:
.net ,package structure ,web dev ,tutorial ,mvc architecture

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}