Learning Web Development (3)

Creating Entities

Before I do anything else, I need to create the entity that gets saved to the database. I called this entity “Condition”, but I think the word “Rule” is more appropriate since I need to store not only a condition but also the value that is used by the condition, and maybe some other data as well.

Separate Functionality into Projects

I like to organize different parts of software I write into different projects in Visual Studio: this way each project is responsible for its own set of tasks and I can reference these projects from new projects.

For instance, I can create a website, a console application or a Windows Service and allow these to use the same existing set of core classes, datalayer, et cetera.

Using Repository for Data Access

I will be using the repository pattern for data access: this allows me to switch repositories like, for instance when I need to run a unit test with only in-memory data. The repository can also be uses for caching, abstracting away the data access from the controller/ business layer. For more info on design patterns, check out this book. So, let’s create these objects:

The newly created core entities and repository

And this is what we are going to store in the database:

The TradingAdviceRule class

Configuring the Database

Next, to be able to use the database in the web application we need to configure the database connection in the appsettings.json file which holds the configuration for the website. Finally, we will use these settings in the Web project’s Startup.cs file to make the connection to the database.

First, add this section of code to the appsettings.json file in the web project:

Next, in the Startup.cs class make sure the ConfigureServices method looks something like this:

Deploying the Database

Right now, the database is almost ready for use. The only thing we need to do now is creating an Entity Framework Migration and use that to create the database. I will do this from the command line inside the Data project’s folder. Type:

Make sure you tell EF what the startup (-s) project is 🙂

This operation created the Migrations folder and files inside the Data project. To deploy this database, type:

Oh and… don’t forget to add the EF Core NuGet packages before you run this command 🙂

Piece of cake 😉

You can now verify in the SQL Server Object Explorer in Visual Studio that the database has indeed been created:

Created

However, there’s something not quite “right” with the data types in the database. I can spot at least four issues here I would like to fix..

Anyway, that’s something for future Jos to think about. The current Jos is going to watch an episode of Homeland now..

If you want, leave me a comment saying what you would like to see fixed first in the database.

Leave a Reply

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

Social Media Auto Publish Powered By : XYZScripts.com