Skip to content
esra75Public

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

1 Commit

Folders and files

Repository files navigation

NotesApi

An ASP.NET Core Web API that performs the same CRUD operations on one Notes table in three ways, so the data-access approaches can be compared side by side:

Route Approach Where the SQL comes from
/api/notes/direct Controller uses the EF Core DbContext directly Generated by EF Core
/api/notes/ef Controller → INoteRepository → EfNoteRepository Generated by EF Core
/api/notes/ado Controller → INoteRepository → AdoNoteRepository Parameterized ADO.NET

The two repository routes share one controller base class and differ only in which implementation they receive. Both implementations are registered in dependency injection under different keys ("ef" and "ado").

Requirements

  1. Create, read, update and delete notes through each of the three routes, with identical behaviour.
  2. All three routes work on the same table: a note saved through one route is visible through the others.
  3. Unknown IDs return 404; invalid input (empty title or body, title over 200 characters) returns 400.
  4. All SQL is parameterized; no user input is ever concatenated into SQL text.
  5. Database connections are always closed, including when an error occurs.
  6. JSON responses are compressed (Brotli or gzip) when the client asks for it.

Tech stack

.NET 10 · ASP.NET Core (controllers) · EF Core 10 · ADO.NET (Microsoft.Data.SqlClient) · keyed dependency injection · response compression · SQL Server 2022 in Docker · OpenAPI + Scalar · dotnet user-secrets

Project layout

NotesApi/
├── dotnet-tools.json
└── NotesApi.Api/
    ├── Data/              Note entity, NoteRequest, NotesDbContext
    ├── Repositories/      INoteRepository, EfNoteRepository, AdoNoteRepository
    ├── Controllers/       NotesDirectController, NotesRepositoryControllers (base + EF + ADO)
    ├── Migrations/        InitialCreate (creates the Notes table)
    └── Program.cs         keyed registrations and compression

SQL Server prerequisites

The application requires a reachable SQL Server instance and a connection string stored with dotnet user-secrets. The container commands below require Docker and an x86-64 Linux environment. Microsoft does not support SQL Server containers under Apple Silicon emulation; --platform linux/amd64 does not remove that limitation. See SQL Server container requirements. On an ARM Mac, the API can connect to SQL Server hosted on a supported machine instead.

Running locally

  1. Start SQL Server (the password must have 8+ characters with upper case, lower case and digits):
    docker run -d --name sql --platform linux/amd64 -e ACCEPT_EULA=Y -e 'MSSQL_SA_PASSWORD=<your-password>' \
      -p 127.0.0.1:1433:1433 mcr.microsoft.com/mssql/server:2022-latest
  2. Restore the EF tool, store the connection string and create the table:
    dotnet tool restore
    cd NotesApi.Api
    dotnet user-secrets set "ConnectionStrings:Notes" "Server=localhost,1433;Database=NotesDb;User Id=sa;Password=<your-password>;TrustServerCertificate=True"
    dotnet ef database update
  3. Run it and open http://localhost:5067/scalar/v1:
    dotnet run

Endpoints

The same five operations exist under each of /api/notes/direct, /api/notes/ef and /api/notes/ado:

Method Route Success Errors
GET {base} 200, list
GET {base}/{id} 200, note 404
POST {base} 201, note + Location 400
PUT {base}/{id} 204 400, 404
DELETE {base}/{id} 204 404

Automated tests

dotnet test NotesApi.slnx

The tests use ASP.NET Core's test host and EF Core's in-memory provider; SQL Server is not needed to run them. They verify HTTP routing, validation and application behavior. They do not verify SQL Server's SQL execution, relational constraints or concurrency behavior. The EF routes are exercised with shared records; ADO.NET request validation is tested, but its SQL operations still require live-database testing.

Test plan

Manual acceptance checks, run in order against a freshly migrated database. Each request is in NotesApi.Api/NotesApi.Api.http.

# Check Steps Expected result
1 Create (ADO.NET) POST /api/notes/ado 201; body has "id": 1
2 Read (EF repository) GET /api/notes/ef 200; the note from step 1
3 Read (direct EF) GET /api/notes/direct/1 200; same note
4 Update (EF) PUT /api/notes/ef/1, then GET /api/notes/ado/1 204, then the updated title
5 Delete (ADO.NET) DELETE /api/notes/ado/1, then GET /api/notes/ef/1 204, then 404
6 Validation POST with an empty title to each route 400 on all three
7 Unknown ID PUT /api/notes/ado/999 and PUT /api/notes/ef/999 with a valid body 404 on both
8 Compression curl -s -D - -o /dev/null -H "Accept-Encoding: br" http://localhost:5067/api/notes/ef Response header Content-Encoding: br

Credits

Inspired by skcore/dotnetcore. This is an independent rewrite, not a copy of that code.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages