Skip to content

Repository files navigation

ASP.NET Core Dependency Injection & CRUD API

This project is part of my Backend .NET learning journey.

In this project, I explored one of the most important concepts in ASP.NET Core—Dependency Injection (DI). Alongside understanding service lifetimes (Singleton, Scoped, and Transient), I also built a Minimal API that performs CRUD operations on an in-memory product store.


📌 Project Overview

Dependency Injection is one of the core features of ASP.NET Core.

Instead of manually creating objects, the framework creates and manages them for us. This project demonstrates how different service lifetimes behave during an HTTP request while also implementing a simple REST API for managing products.


🎯 Why I Built This Project

After learning controllers and routing, I wanted to understand how ASP.NET Core manages object creation behind the scenes.

Rather than only reading about Dependency Injection, I built a practical project that lets me observe how Singleton, Scoped, and Transient services behave in real requests.

To reinforce the concepts, I also created a Product API using Minimal APIs.


🚀 What I Built

  • ASP.NET Core Minimal API
  • Dependency Injection
  • Singleton Lifetime
  • Scoped Lifetime
  • Transient Lifetime
  • Interface-based service registration
  • Service resolution using RequestServices
  • Multiple middleware pipeline
  • Product CRUD API
  • In-memory product storage
  • Route parameters
  • HTTP GET
  • HTTP POST
  • HTTP PUT
  • HTTP DELETE
  • Status code responses
  • Validation for product creation

📚 What I Learned

Dependency Injection

I learned how ASP.NET Core automatically creates and provides objects whenever they are needed instead of manually creating them using the new keyword.


Service Lifetimes

I understood the difference between:

  • Singleton (one instance for the whole application)
  • Scoped (one instance per request)
  • Transient (new instance every time)

By logging a randomly generated Service ID, I could clearly see how each lifetime behaves.


Middleware

I learned that middleware executes in sequence for every incoming request.

Each middleware requested the same service, allowing me to observe how service lifetimes change across the request pipeline.


Minimal APIs

I became more comfortable building APIs without controllers.

I learned how endpoints are mapped directly inside Program.cs.


CRUD Operations

I practiced creating RESTful endpoints for:

  • Reading products
  • Creating products
  • Updating products
  • Deleting products

using an in-memory collection.


⚡ Challenges I Faced

  • Understanding why service IDs changed every time.
  • Confusion between Singleton, Scoped, and Transient.
  • Forgetting to register services inside Program.cs.
  • Understanding how RequestServices works.
  • Returning proper HTTP status codes.
  • Learning Minimal API syntax.

✅ How I Solved Them

I tested each service lifetime individually by uncommenting one registration at a time.

Watching the Service IDs printed in the console made the behavior much easier to understand.

I also experimented with multiple middleware components to observe how ASP.NET Core creates service instances throughout the request lifecycle.


💡 Key Takeaways

  • Dependency Injection is a fundamental feature of ASP.NET Core.
  • Choosing the correct service lifetime is important.
  • Middleware can access services from the DI container.
  • Minimal APIs provide a simple way to build REST APIs.
  • Practical experiments make abstract concepts much easier to understand.

🛠 Technologies Used

  • C#
  • .NET
  • ASP.NET Core
  • Minimal APIs
  • Dependency Injection
  • REST API
  • Swagger
  • Visual Studio Code

📂 Project Structure

DIProject
│
├── Program.cs
├── IMyService.cs
├── MyService.cs
├── appsettings.json
├── appsettings.Development.json
├── Properties
│   └── launchSettings.json
└── DIProject.csproj

📖 API Endpoints

Method Endpoint Description
GET / Demonstrates Dependency Injection lifecycle
GET /products Get all products
GET /products/{id} Get product by ID
POST /products Create product
PUT /products/{id} Update product
DELETE /products/{id} Delete product

📈 Future Improvements

  • Connect the API to SQL Server.
  • Replace the in-memory list with Entity Framework Core.
  • Add Repository Pattern.
  • Implement Validation using FluentValidation.
  • Add Logging with Serilog.
  • Write Unit Tests using xUnit.
  • Implement JWT Authentication.
  • Dockerize the application.

🔮 Next Learning Goal

Next, I'll learn Entity Framework Core to persist data in SQL Server instead of using an in-memory collection.


⭐ Reflection

Every project in my backend journey helps me understand how ASP.NET Core works behind the scenes.

This project taught me that writing APIs is not only about handling requests—it's also about understanding how the framework manages objects, services, and application architecture. Learning Dependency Injection has given me a much stronger foundation for building scalable backend applications in the future.

About

ASP.NET Core Minimal API project demonstrating Dependency Injection lifetimes (Singleton, Scoped, Transient) alongside CRUD operations using an in-memory product store.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages