L11 - ASP.NET Core Web API and EF Core
L11 - ASP.NET Core Web API and EF Core
Development
L11.1 SEHS4701
ASP.NET Core Web API
• Web API:
– Helps create REST-style APIs
– Enables external systems to use the business logics
implemented in your application
– Uses URLs in requests and helps obtain results in
the JSON format
– Is ideal for mobile application integration
L11.2 SEHS4701
ASP.NET Core Web API
L11.4 SEHS4701
ASP.NET Core Web API
L11.6 SEHS4701
ASP.NET Core Web API
L11.7 SEHS4701
ASP.NET Core Web API
L11.8 SEHS4701
ASP.NET Core Web API
Middleware
• Middleware is software that's assembled into an app
pipeline to handle requests and responses. Each
component:
– Chooses whether to pass the request to the next component in
the pipeline.
– Can perform work before and after the next component in the
pipeline.
• Request delegates are used to build the request
pipeline. The request delegates handle each HTTP
request.
L11.9 SEHS4701
ASP.NET Core Web API
Configuring Middleware
• You configure pipeline middleware using the Configure method,
provided by the Startup class.
– Services include:
• Serving static files
• MVC routing and operations
• Custom services
– Service lifetime:
• AddSingletonService<IService, Service>()
Singleton service used for the lifetime of the Application
• AddScopedService<IService, Service>()
Singleton service used for the duration of one HTTP request
• AddTransientService<IService, Service>()
Created with each request for service, possibly many times per HTTP request
L11.10 SEHS4701
ASP.NET Core Web API
L11.11 SEHS4701
ASP.NET Core Web API
Startup
– ConfigureServices method
• Registers a service interface and implementing class for dependency injection
using one of the AddService methods, described in the previous slide
• Each AddService adds a service to the Dependency Injection Services
container.
– Configure method
• Creates the application’s pipeline with app.UseXXX() invocations.
• app.Run( some write method )
• An app.UseXXX invocation need not pass a message down the pipeline.
• App.Run executes only if all app.UseXXX() middleware pass along the
request message.
• Essentially, the pipeline is the sequence of app.UseXXX() methods in
StartupConfigure()
L11.12 SEHS4701
Entity Framework
• An ORM manages details between set of objects and the underlying relational
database.
• However, Entity Framework can handle all data related interactions between
the application and the database.
L11.13 SEHS4701
Entity Framework in an application
Entity Framework in an application
User Interface
Business Layer
Persistence
Framework
(Entity Framework)
Database
L11.14 SEHS4701
Entity Framework Workflows
L11.15 SEHS4701
Entity Framework Architecture
• LINQ to Entities: Used to write queries against the object model. Returns entities
defined in the conceptual model.
• Entity SQL: It is a storage independent query language.
• Object Services: Main entry point for accessing data from the database and returning it
back.
• Entity Client Data Provider:
• Communicates with the ADO.NET data provider.
• Converts LINQ-to-Entities or Entity SQL to SQL query.
• ADO.NET Data Provider: Communicates with the database using ADO.NET.
L11.16 SEHS4701
Entity Framework Architecture
LINQ to Entity
EDM Entities SQL
Database
L11.17 SEHS4701
Database First
Database-First Approach
Generate Domain Classes based on the database
L11.18 SEHS4701
Database First
L11.19 SEHS4701
Dealing with Database Changes
L11.20 SEHS4701
Code First
Domain
Entity Framework Database
Classes
Code-First Approach
Migrations
• Adding a new tables
• Seeding the database with SQL queries
• Modifying existing classes
• Adding new property
L11.21 SEHS4701
SEHS4701 Advanced Information Systems
Development
The End
L11.22 SEHS4701