Implementing the soft delete procedure

As mentioned in the Deleting resources section of Chapter 5, Web Service Stack in ASP.NET Core, the soft delete technique is a widespread deletion practice in real-world applications. Furthermore, it is quite uncommon to physically delete entities from our data source.

Deleted data may be relevant for historical purposes and for performing analysis and reports. The soft delete implementation involves all the projects we have seen in the previous three chapters. In order to proceed, let's add a new IsInactive field to our Item entity inside the Catalog.Domain project:

namespace Catalog.Domain.Entities
{
public class Item
{
...
public bool IsInactive { get; set; }
}
}

Since we changed the schema of the Item entity, we need to perform another EF Core migration by executing the following commands inside the Catalog.API project:

dotnet ef migrations add Added_IsInactive_field
dotnet ef database update

The result of the execution of the aforementioned command will generate a new migration file inside the Migrations folder and the application of the newly created migration in the database specified in the connection string of the Startup class.

Furthermore, as we saw in Chapter 8, Building the Data Access Layer, if we want to connect to the local Docker SQL Server instance, we can specify the following connection string in the appsettings.json file:

{
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"DataSource": {
"ConnectionString": "Server=localhost,1433;Initial Catalog=Store;User Id=catalog_srv;Password=P@ssw0rd"
}

}

The preceding connection string provides the connection to the local instance of SQL Server. In order to run the instance, it is necessary to follow the command we saw in Chapter 8. The following runs the docker instance using the name sql1:

docker run -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=<YOUR_SA_PASSWORD>" -p 1433:1433 --name sql1 -d mcr.microsoft.com/mssql/server:2017-latest

Furthermore, we can log in into the container using the following command:

docker exec -it sql1 "bash"

and finally, we can execute the sqlcmd in order to log in into the SQL server instance:

/opt/mssql-tools/bin/sqlcmd -S localhost -U SA -P '<YOUR_SA_PASSWORD>'

1> CREATE LOGIN catalog_srv WITH PASSWORD = 'P@ssw0rd';
2> CREATE DATABASE Store;
3> GO
1> USE Store;
2> CREATE USER catalog_srv;
3> GO
1> EXEC sp_addrolemember N'db_owner', N'catalog_srv';
2> GO

The code above creates the Store database with a login user called catalog_srv. Later in the book, we will see how to automate this process using the tools provided by Docker. Now, the database schema is updated, and we are able to continue by including the IsInactive field in the repository and the service layer of the application. 

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

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