104
CHAPTER 5 | Designing and Developing Multi-Container and Microservice-Based .NET Applications
Figure 6-6. Creating an ASP.NET Core Web API project in Visual Studio 2019
To create an ASP.NET Core Web API Project, first select an ASP.NET Core Web Application and then
select the API type. After creating the project, you can implement your MVC controllers as you would
in
any other Web API project, using the Entity Framework API or other API. In a new Web API project,
you can see that the only dependency you have in that microservice is on ASP.NET Core itself.
Internally, within the
Microsoft.AspNetCore.All
dependency, it is referencing Entity Framework and
many other .NET NuGet
packages, as shown in Figure 6-7.
105
CHAPTER 5 | Designing and Developing Multi-Container and Microservice-Based .NET Applications
Figure 6-7. Dependencies in a simple CRUD Web API microservice
The API project includes references to Microsoft.AspNetCore.App NuGet package, that includes
references to all essential packages. It could include some other packages as well.
Implementing CRUD Web API services with Entity Framework Core
Entity Framework (EF) Core is a lightweight,
extensible, and cross-platform version of the popular
Entity Framework data access technology. EF Core is an object-relational mapper (ORM) that enables
.NET developers to work with a database using .NET objects.
The catalog microservice uses EF and the SQL Server provider because its database is running in a
container with the SQL Server for Linux Docker image. However, the database could be deployed into
106
CHAPTER 5 | Designing and Developing Multi-Container and Microservice-Based .NET Applications
any SQL Server, such as Windows on-premises or Azure SQL DB. The only
thing you would need to
change is the connection string in the ASP.NET Web API microservice.
The data model
With EF Core, data access is performed by using a model. A model is made up of (domain model)
entity classes and a derived context (DbContext) that represents a session with the database,
allowing
you to query and save data. You can generate a model from an existing database, manually code a
model
to match your database, or use EF migrations technique to create a database from your model,
using the code-first approach (that makes it easy to evolve the database as your model changes over
time). For the catalog microservice, the last approach has been used. You can see an example of the
CatalogItem entity class
in the following code example, which is a simple Plain Old Class Object
(
POCO
) entity class.
Dostları ilə paylaş: