© Eric Vogel 2021
E. VogelBeginning Entity Framework Core 5https://doi.org/10.1007/978-1-4842-6882-7_2

2. Project Structure

Eric Vogel1  
(1)
Okemos, MI, USA
 

Creating the structure of your application is an important step. In this chapter, I will detail how to use a multilayered architecture to separate concerns in the application. This will make your application easier to maintain and extend.

Core Project

The Core project is where the commonly used code shared across the entire application will reside. This is where the entity classes used by Entity Framework will live. This is also where common interfaces and utility code will exist.

To get started, open the solution you created in Chapter 1. Then add a new .NET Standard C# class library project named “EFCore5WebApp.Core” to the solution as seen in Figure 2-1.
../images/499766_1_En_2_Chapter/499766_1_En_2_Fig1_HTML.jpg
Figure 2-1

New Class Library

On the next screen, name your project “EFCore5WebApp.Core” as seen in Figure 2-2.
../images/499766_1_En_2_Chapter/499766_1_En_2_Fig2_HTML.jpg
Figure 2-2

Naming the Core Project

Next, create a new NUnit Test Project (.NET Core) template project named “EFCore5WebApp.Core.Tests”. This project will be where we’ll create any needed unit tests for shared application code. You can easily find the correct template by searching for “NUnit” in the template dialog as seen in Figure 2-3.
../images/499766_1_En_2_Chapter/499766_1_En_2_Fig3_HTML.jpg
Figure 2-3

Adding a NUnit Project

After adding the Core.Tests project, add a project reference to the Core project to it.

Unit Testing Overview

NUnit is a commonly used unit testing framework. Unit tests have become popular with the whole Test-Driven Development (TDD) movement, in which you create unit tests first and then write the code to make the tests pass. Later in the book, we’ll be testing that our Entity Framework Core 5 code works correctly before integrating it into our web project.

A unit test tests a small set of code, usually at the method level. You test the happy path as well as potential edge cases to make sure your code is solid before it becomes integrated into the rest of the solution.

Data Access Layer Project

Next, we’ll create the data access layer project, or DAL project for short. Create a new .NET Core C# class library as seen in Figure 2-4 called “EFCore5WebApp.DAL”.
../images/499766_1_En_2_Chapter/499766_1_En_2_Fig4_HTML.jpg
Figure 2-4

New .NET Core Class Library

Then add a project reference to the Core project to the DAL project. After that, add a NUnit Test Project to the solution named “EFCore5WebApp.DAL.Tests”. Add references to the DAL and Core projects to the DAL.Tests project. After that, add Core project reference to the DAL and DAL.Tests projects. The DAL.Tests project will be where we test all of our Entity Framework Core 5 code before putting it to use in the web project.

Next, we’ll move the EF Core 5 NuGet packages into the DAL projects. Right-click the solution node and click the Manage NuGet Packages for Solution option. Then select the Microsoft.EntityFramewrokCore.SqlServer project and select the DAL and DAL.Tests projects and click the Install button as seen in Figure 2-5.
../images/499766_1_En_2_Chapter/499766_1_En_2_Fig5_HTML.jpg
Figure 2-5

Move EF Core 5 Packages to the DAL Project

Main Project References

Lastly, we need to add project references to the ASP.NET Core MVC application project named “EFCore5WebApp”. Add project references to the Core and DAL projects to the root “EFCore5WebApp” project. This will ensure we can use the services and entities from our ASP.NET Core web app.

Summary

In this chapter, we’ve set ourselves up for success by creating a multilayered project structure. The Core project contains our entities that will be used by Entity Framework in the DAL project. The DAL project is where our Entity Framework Core 5 code will reside. Our web project will make use of the Core and DAL projects. Your project structure should now look like Figure 2-6.
../images/499766_1_En_2_Chapter/499766_1_En_2_Fig6_HTML.jpg
Figure 2-6

Project Structure

By separating your projects by application concerns, the project will be easier to maintain and extend over time. We’ve also architected the solution with unit testing support. This will speed up development time by making it easier to safely make changes to the application without worrying about introducing new side effect bugs, and functionality will be regression tested during development time.

In the next chapter, I’ll cover how to create the entity classes that will later be used by Entity Framework Core 5 in Chapter 4. Entity classes are used by Entity Framework Core 5 to map to tables in your database.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.129.19