SEHS4701 Advanced Information Systems
Development
ASP.NET Core and Web API
with
Entity Framework
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
Characteristics of a RESTful Service:
– Can be called to retrieve business information from the
server
– Can create, update, and delete information in a database
through HTTP operations
– Uses URLs to uniquely identify the entity that it operates on
– Uses HTTP verbs to identify the operation that the
application needs to perform. The HTTP verbs include:
• GET
• POST
• PUT
• DELETE
L11.3 SEHS4701
ASP.NET Core Web API
Characteristics of routing in Web API:
• You can use API controller names and a naming
convention for actions to route Web API requests
• Alternatively you can use the following attributes to
control the mapping of HTTP requests (HTTP
Verb+URL) to actions in the controller:
– The HttpGet, HttpPut, HttpPost, or HttpDelete attributes
– The AcceptVerbs attribute
– The ActionName attribute
L11.4 SEHS4701
ASP.NET Core Web API
Data Return Formats
• Web API can return data in JSON or XML
formats
• Web API uses the media formatter to:
– Format or serialize the information that a Web API
REST service returns
– Control the media type in the HTTP header
– Format all content that the server renders to client
systems
L11.5 SEHS4701
ASP.NET Core Web API
• Asp.Net Core provides a framework for building and
executing both Console and Web Applications
• The framework provides a host, responsible for startup
and lifetime management.
– Generic Host – host non-web apps
• Windows services and executables
– Web Host – suitable for hosting web applications
• Create instance with IWebHostBuilder
– Primary focus is web applications
– It provides a pluggable hosting environment that supports:
• Kestral, IIS, Apache, Nginx
L11.6 SEHS4701
ASP.NET Core Web API
Web Application Hosting Options
L11.7 SEHS4701
ASP.NET Core Web API
Asp.Net Core Pipeline
• Provides an application pipeline that supports pluggable
services
• Pipeline services are delivered via a Dependency
Injection Container
• The pipeline is configured with one or more components.
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
Middleware provided by the framework - partial list
• Authentications
• Cookie Policy
• CORS
• Diagnostics
• HTTPS Redirection
• MVC
• Routing
• Session
• Static Files
• URL Rewriting
• WebSocket
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
• Entity Framework is an open source ORM
framework for .NET applications.
• An ORM manages details between set of objects and the underlying relational
database.
• Alternatively traditional ADO.NET classes can be used:
• SqlConnection
• SqlCommand
• SqlDataReader
• SqlDataAdapter
• 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
Database First Code First Model First
• Allows reverse • Allows creating and • Create an UML diagram
engineering a model updating of database using a Visual Designer
from existing tables based on domain and then let the
database classes. workflow generate the
• EF generates domain • EF generates database database.
classes. tables.
L11.15 SEHS4701
Entity Framework Architecture
• EDM (Entity Data Model) :
• Storage Model : Includes tables, views, stored procedures, relationships and keys.
• Conceptual Model : Contains model classes and their representation.
• Mapping Model: Information about how conceptual model is mapped to the storage model.
• 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
Conceptual Model Object Services
Mapping Entity Client Data Provider
Storage Model ADO.NET Data Provider
Database
L11.17 SEHS4701
Database First
Database Context and
Entity Framework
Entity classes
Database-First Approach
Generate Domain Classes based on the database
L11.18 SEHS4701
Database First
Edmx Designer Representation
L11.19 SEHS4701
Dealing with Database Changes
• Adding a new table
• Adding a new column
• Modifying a table column
• Modify column type
• Importing stored procedures
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
• Modifying existing property
• Overriding conventions using Data Annotations
L11.21 SEHS4701
SEHS4701 Advanced Information Systems
Development
The End
L11.22 SEHS4701