Вы находитесь на странице: 1из 41

Developing Great Web

APIs Architectures w/
ASP.NET Core 2.1
Chris Woodruff

Developer Advocate at JetBrains

www.chriswoodruff.com

www.twitter.com/cwoodruff

www.linkedin.com/in/chriswoodruff
What do you and do
wrong?
Calling Data Access functionality (EF Core)
from Controllers

Entity
Controllers Framework
Core
Lots of Code that you
don’t UNDERSTAND
AKA
“Spaghetti Code”
Coupling your Data
Access (EF Core) to your
Domain
Not thinking
about Unit or
Integration
testing

1 April 2019 PRESENTATION TITLE | COMPANY NAME 8


What does
ASP.NET Core
help us with?
HTTP Quiz
RESOURCE VERB EXPECTED OUTCOME RESPONSE CODE

/Products GET A list of all products in the system 200 OK


/Products?Color=green GET A list of all products in the system 200 OK
where the color is green
/Products POST Creation of a new product 201 Created

/Products/81 GET Product with an ID of 81 200 OK


/Products/881 GET Some error message 404 Not Found

/Products/81 PUT Update of the product with ID of 81 204 No Content


/Products/81 PUT Update of the product with ID of 81 304 Not Modified

/Products/81 DELETE Deletion of the product with ID of 81 204 No Content


Workflow for an HTTP REST Request
HTTP Request Method HTTP Path Parameters

GET | POST | ... /api/albums/create ?Date=27 Aug 2018&username=...

Correct Method? Path Correct? Parameters Valid?


YES YES
NO YES NO YES YES NO

400 Method Allowed? 404 Authorized? 400

NO NO

405 401
200
Always use Asynchronous Development

Await

Async
Look at the
Architecture
How I build my
APIs
API

Domain

Data Tests
How we start!
Documenting your
API
API Layer
API Layer
Like the UI of an ASP.NET
MVC (or any web platform
and MVC pattern), the API
endpoints should not know
about the Domain
knowledge or Data Access
API Layer
Should interact with Model API
consumers using
ViewModels to ensure the
greatest flexibility
ViewModel
Domain Layer
Domain Layer
Contains both my Entity Model API
models and my
ViewModels for the
solution.
ViewModel
Domain Layer
Contains all my interfaces
for Data Retrieval so I can
keep a well defined
standard for my data
access.
Domain Layer
Allows me to use
Dependency Injection for
Repositories
Domain Layer
Think of the Domain Layer as the Supervisor!

ViewModels Supervisor Entity Models


AKA the black box
Data Layer
Data Layer
Where all the heavy data
lifting happens
Data Layer
I have everything defined
based on Interfaces from
the Domain Layer
Data Layer
I use Entity Framework
Core 2.1 but I can use
anything that is .NET
Standard 2.0
Data Layer
I also Mock up a data layer
for testing
Caching
Caching for APIs
Response Caching
In-Memory Caching
Distributed Caching
Response Caching
Directs Caching Attribute on Controller or Action
on the consumer [ResponseCache(Duration = 60)]
side
Response Header:
Cache-Control: public,max-age=60
HTTP 1.1 Caching [ResponseCache(Location =
specification ResponseCacheLocation.None, NoStore = true)]

Response Header:
Cache-Control: no-store,no-cache
Pragma: no-cache
In-Memory Caching
Directs Caching on
the consumer side
Distributed Caching
Distributed caches can improve the performance and scalability of
ASP.NET Core apps, especially when hosted in the cloud or a server
farm.
1. Cached data is coherent on all web servers. Users don't see different
results depending on which web server handles their request
2. Cached data survives web server restarts and deployments.
Individual web servers can be removed or added without impacting
the cache
3. The source data store has fewer requests made to it (than with
multiple in-memory caches or no cache at all)
Distributed
Caching
Local Redis Cache
SQL Server Cache
Azure Redis Cache
Amazon ElastiCache
Google Cloud Memcache
Testing
Tests
• I have 2 types of tests: Integration and Unit
• Integration for API endpoint testing and Controllers
• Unit Testing to cover the Repositories
Demo
Thank You
All Code located on GitHub
• https://github.com/cwoodruff/ChinookASPNETCoreAPINTier

Article on the Subject


• http://bit.ly/ASPNETCoreAPI

Contact Me
• Chris.Woodruff@JetBrains.com

Follow Me
• @cwoodruff

Вам также может понравиться