API lessons 01–12 · refresher and exam
Two parts. Part 1 is a quick read of the key points from the first twelve lessons. Part 2 is a 40-question exam with scoring at the end. Each lesson title links to the full lesson if something needs more than a reminder.
Skip to the exam ↓Part 1 · Refresher
01 · HTTP and REST full lesson
- The web is request, then response. The client always speaks first.
- Request: method, path, headers, body. Response: status, headers, body.
- Stateless: the server remembers nothing between requests, so every request carries what it needs. That is the login puzzle.
| Code | Meaning |
|---|---|
| 2xx | Fine (200 OK, 201 Created) |
| 3xx | Look elsewhere (301 moved) |
| 4xx | Client's mistake: 400 broken request, 401 not logged in, 403 not allowed, 404 not found |
| 5xx | Server broke (500) |
- REST: the URL names the thing (noun), the method says what to do.
GETread,POSTcreate,PUTreplace the whole thing,PATCHchange a part,DELETEremove. - On a 4xx check what you sent. On a 5xx check the server logs.
02 · Solution and project planning full lesson
- Solution = house, project = room. Same solution only if the projects would share code.
- Two steps:
dotnet new ...builds the project,dotnet sln addputs it on the solution. Skipping the second is the classic miss. - Code needed by several projects goes in its own library, not a big
CommonorUtils. - ProjectReference is a key and it only goes one way. Circles can never be built.
Directory.Build.propsshares settings (target framework,Nullable).Directory.Packages.propsshares package versions.
03 · .NET project types full lesson
| Template | Runs? | Use |
|---|---|---|
classlib | No | Shared code |
console | Once, then stops | One-off or timed tool |
worker | Keeps running | Background jobs |
webapi | Waits for requests | Answers with data (JSON) |
mvc / razor | Waits for requests | Server builds whole pages |
mstest etc. | Only when testing | Checks another project |
Publish a library as a NuGet package only when someone outside your solution needs it.
04 · Dependency injection basics full lesson
- Interface = contract: what something can do, not how.
- Dependency = anything a class needs to do its job.
- A class should receive its dependencies through the constructor, not choose them with
new. - One place (
Program.cs) decides which implementation. Switching to a different one, or a fake for tests, touches only that place.
05 · Lifetimes: singleton full lesson
- DRY: don't repeat yourself. Moving shared code into one class fixes the code, but not how many times it runs.
- The DI container builds objects by a rule.
AddSingleton= built once, the same instance for everyone for the life of the app. - The lifetime is how often the container builds a fresh one. Singleton fits settings that never change.
06 · State and sharing full lesson
- Member variable: a variable inside a class that belongs to one object. Not a global.
- Stateless: nothing changes after it is built (
PriceCalculator,AppOptions), like a poster. Stateful: member variables change (a cart), like a whiteboard.
A stateful object is never a singleton. One shared cart merges two customers.
07 · Lifetimes: transient full lesson
- Transient: a brand-new instance every time something asks. Nothing is shared.
- A class's own lifetime does not protect it. A transient class that depends on a singleton still shares that singleton's state.
- Ask of the whole chain: does anything below hold state that must not leak between two pieces of work?
08 · Lifetimes: scoped full lesson
- Scope: a bubble around one piece of work. In a web API, one scope is one HTTP request. ASP.NET Core opens and closes it for you.
- Scoped: one instance per scope. Classes in the same request share it, the next request gets a new one.
- Typical scoped things: the order being processed, the logged-in user, the delivery address.
- In a console app nobody opens scopes, so scoped quietly behaves like a singleton.
| Holds... | Lifetime |
|---|---|
| Settings that never change | Singleton |
| State that must never be shared | Transient |
| State of one request, shared inside it | Scoped |
09 · JSON full lesson
- Blocks: object
{ }, array[ ], values (text, number,true/false,null). Double quotes only, no trailing comma, no comments, no date type. - Serialize = C# object to JSON. Deserialize = JSON to C# object. ASP.NET Core does both.
- With web settings: names become camelCase, only public properties are written,
nullis written asnull. - Class → object, list → array (empty is
[]), dictionary → object (keys unchanged),GuidandDateTime→ text, enum → number. - Unknown JSON keys are ignored, missing ones keep the default, broken JSON is a
400.
10 · async and await full lesson
- I/O (files, other services, the database) is slow. Blocking on it wastes a thread, and a server can run out of threads while the CPU is idle.
awaitfrees the thread during the wait. It does not make one request faster.- Rule: for I/O call the async version and
awaitit.Task<T>is a receipt for aTthat will be ready later, plainTaskis for nothing returned. - The method becomes
asyncand ends inAsync, and that spreads up to the endpoint.
Avoid .Result, .Wait(), Task.Run as a fix, and async void.
11 · SQLite full lesson
- A real SQL database in one file. No server, nothing to install. Back it up by copying the file.
- Fits tests (in memory), prototypes and small low-traffic sites. Limit: one writer at a time.
CREATE TABLE,INSERT,SELECT ... WHERE. A primary key withAUTOINCREMENTis picked by the database.- Connection string tells the app where the database is:
Data Source=bookshop.db.
12 · Unit testing basics full lesson
- Compiling does not prove the code is right. Tests check behaviour and are an investment that pays back as the project grows.
- A unit test checks one small piece, fast, on its own. An integration test checks real parts together.
- Real code in
src/, tests intests/, named<Project>.Testsand referencing the project. - MSTest:
[TestClass],[TestMethod],Assert.AreEqual,Assert.Throws<T>. - Arrange, Act, Assert. Name it
Method_Input_ExpectedResult. Break the code once to see the test fail. - One class, public methods only, one idea per test, and think like a QA (normal, edge, bad input).
- The books service is the course's one integration-test exception: it is tested against a real database kept in memory. Tests do not replace QA.
Part 2 · Exam
Results
Answer the questions to see how you did.
By lesson
By difficulty