Get up to 80 % extra points for free! More info:

Lesson 1 - E-shop in ASP.NET Core MVC - Solution Structure

Welcome, intermediate and advanced programmers, to the E-shop in ASP.NET Core 2.X MVC course. It's designed for everyone who already knows the basics of this technology at least within the range of our ASP.NET Core MVC Basics course and want to move to the next level. In this on-line course, we're going to create a fully-featured e-shop using the following technologies:

  • Entity Framework Core - an ORM with the Code First approach
  • Repository - a design pattern for data manipulation
  • ASP.NET Identity - a user and role management framework, providing authentication and authorization
  • Extension methods - we'll have a look at extending the functionality of existing classes
  • Generic classes - these can save a lot of typing and I believe you'll get to like them
  • AutoMapper - helps with class mapping
  • JavaScript - a few code lines will be on the client side too
  • AJAX and many others ...

This course will prepare you perfectly for C# .NET jobs, since most of the jobs require, besides C# itself, a good knowledge of the ASP.NET web framework. Experience with its successor, Core, is a valuable advantage. Of course, you can also use the knowledge in your personal business activities.

Basic requirements of the e-shop

Since the e-shop will be really functional and usable, let's have a moment to think about the basic requirements:

  • Users and roles management - As in most web applications, there's a need to distinguish ordinary visitors of the e-shop from administrators who have more functionality available. Additionally, we still have to let unregistered users (the users without login credentials) able to shop as well.
  • Products and categories management - We'll display products clearly (including their images) with the option of add and editing. We'll support categorizing products, searching, filtering, and sorting a product list. We'll allow users to write product reviews.
  • Ordering system - We'll lead the user through the purchase process; from placing the product in the shopping cart, through entering the billing information and selecting the transport type to completing the order, including sending the confirmation email.
  • Data integrity - We'll ensure the integrity and validity of data. No changes can ever affect the transactions which have been already made in the e-shop.
  • Responsive web design - We'll display the website correctly even on smaller screens.

Application design

Let's demonstrate the desired design of the e-shop on the following images:

Product list from a selected category - Complete e-shop in ASP.NET Core MVC Product list from a selected category

Registration form - Complete e-shop in ASP.NET Core MVC Registration form

PDF invoice - Complete e-shop in ASP.NET Core MVC PDF invoice

Let's begin...

In Visual Studio, let's create a new ASP.NET Core Web Application (I'll be working in the Visual Studio 2017 Community Edition - I strongly recommend using at least version 15.7+). This tutorial is written for Core 2.1. If you don't have this version installed, you can download the current SDK here: https://www.microsoft.com/…load/windows. Choose Authentication Individual User Accounts.

Creating a project - Complete e-shop in ASP.NET Core MVC

Figure 4 - Creating a project

Solution structure

Real commercial projects consist of a lot of classes so it certainly wouldn't be enough to divide them into a simple models-controllers-views structure. To have dozens of files in one folder would be unorganized, at least. The code would be unmanageable, and from my own experience you'd get to the point where you'd write similar methods over and over again, instead of parameterizing an existing one, because you simply wouldn't even know that there already is a similar method somewhere in the project. And that's the beginning of the end :) How to solve this?

Projects

Let's divide the whole solution into multiple layers. Each layer will be a separate project. The e-shop will be moderately complicated, so the following layers will be enough:

  • Data layer - will take care of storing and retrieving data. The other parts of the solution will use its methods (and in our case, its classes as well) without being depended on where the data is actually stored and in what way. We'll implement data manipulation methods into this layer as repositories.
  • Bussiness layer - will contain almost all the logic of our solution divided into managers by the entities which the methods belong to. Next, we'll add some auxiliary classes, ViewModels, and a "mapper" that will help us with casting the classes from one to another.
  • Application layer - This is the project we've just created (the only one we got yet). It'll contain mostly the controllers and the views (most of the models will be in the business layer), and of course other parts that relate to the web app as well - CSS styles, scripts, images etc.

Add 2 more projects to your solution (e.g. by right-clicking the solution and choosing Add -> New Project -> (Visual C#/.NET Core) -> Class Library (.NET Core)). Name the projects EShop.Business and EShop.Data.

Setting up references

In order to be able to use objects from other projects in the main project, we need to set up references. The easiest way is to use the local context menu. In Solution Explorer, right-click the Business project and choose Add -> Reference. In the left part, select Projects and check the Data project. In the same way, set up a reference from the Application project to the Business project. Because our application will still be within a sustainable range, we won't use any DTO or DAO models in the business layer and we'll set up a reference from the Application project to the Data project. For even larger applications, the Application layer would be completely unaware of the data; however, we'll get by our logical structure just fine.

Folders

In the individual projects, we'll organize the code files into folders (and classes into corresponding namespaces). The resulting structure of the finished project can be seen in Figure 5. The purpose of the folders can usually be deduced from their name. You don't have to create folders right now, we'll create them gradually as needed.

Solution structure - Complete e-shop in ASP.NET Core MVC

Figure 5 - Suggested solution structure

That would be all for today. We have now an idea of how the structure of our solution will look like and why. In the next lesson, E-shop in ASP.NET Core MVC - Identity, database, and layout, we'll have a look at the ASP.NET Core Identity configuration.


 

Did you have a problem with anything? Download the sample application below and compare it with your project, you will find the error easily.

Download

By downloading the following file, you agree to the license terms

Downloaded 67x (970.28 kB)
Application includes source codes in language C#

 

All articles in this section
Complete e-shop in ASP.NET Core MVC
Skip article
(not recommended)
E-shop in ASP.NET Core MVC - Identity, database, and layout
Article has been written for you by Martin Petrovaj
Avatar
User rating:
9 votes
Activities