Skip to content

Kaut/AddressService

Repository files navigation

AddressService Microservice

A production-grade .NET 10 RESTful microservice for managing addresses, built with Clean Architecture, SOLID principles, and Domain-Driven Design.

Project Structure

AddressService/
├── AddressService.Domain/              # Core domain layer
│   ├── Entities/                       # Address entity
│   ├── Enums/                          # AddressType enum
│   └── Interfaces/                     # IAddressRepository, IUnitOfWork
├── AddressService.Application/         # Application layer (use cases)
│   ├── DTOs/                           # Request/Response DTOs
│   ├── Mappings/                       # AutoMapper profiles
│   ├── Validators/                     # FluentValidation rules
│   └── Services/                       # Application services
├── AddressService.Infrastructure/      # Data access layer
│   ├── Data/                           # DbContext
│   ├── Repositories/                   # Repository implementations
│   └── UnitOfWork/                     # UnitOfWork pattern
├── AddressService.API/                 # Presentation layer
│   ├── Controllers/                    # REST endpoints
│   ├── Exceptions/                     # Global exception handling
│   ├── Program.cs                      # Application bootstrap
│   └── appsettings.json                # Configuration
└── AddressService.Tests/               # Unit tests
    └── Services/                       # Service tests (MSTest + Moq)

Features

Clean Architecture - Strict separation of concerns across 5 layers
SOLID Principles - Dependency Injection, Interface Segregation, Single Responsibility
Domain-Driven Design - Address is the core entity
Repository Pattern - Data access abstraction
Unit of Work - Transaction management
Validation - FluentValidation integrated request validation
Logging - Serilog structured logging (console + file)
Error Handling - Global exception handler with RFC 7807 ProblemDetails
API Documentation - Swagger/OpenAPI integration
Unit Tests - MSTest + Moq comprehensive coverage

Prerequisites

  • .NET 10 SDK
  • SQL Server (LocalDB or full instance)
  • Visual Studio 2022 or Visual Studio Code

Getting Started

1. Create Solution & Projects

cd c:\Kautilya\Services\AddressService
dotnet new sln -n AddressService
dotnet new classlib -n AddressService.Domain
dotnet new classlib -n AddressService.Application
dotnet new classlib -n AddressService.Infrastructure
dotnet new webapi -n AddressService.API
dotnet new mstest -n AddressService.Tests

# Add projects to solution
dotnet sln add AddressService.Domain/AddressService.Domain.csproj
dotnet sln add AddressService.Application/AddressService.Application.csproj
dotnet sln add AddressService.Infrastructure/AddressService.Infrastructure.csproj
dotnet sln add AddressService.API/AddressService.API.csproj
dotnet sln add AddressService.Tests/AddressService.Tests.csproj

2. Add Project References

cd AddressService.Application
dotnet add reference ../AddressService.Domain/AddressService.Domain.csproj

cd ../AddressService.Infrastructure
dotnet add reference ../AddressService.Domain/AddressService.Domain.csproj

cd ../AddressService.API
dotnet add reference ../AddressService.Domain/AddressService.Domain.csproj
dotnet add reference ../AddressService.Application/AddressService.Application.csproj
dotnet add reference ../AddressService.Infrastructure/AddressService.Infrastructure.csproj

cd ../AddressService.Tests
dotnet add reference ../AddressService.Domain/AddressService.Domain.csproj
dotnet add reference ../AddressService.Application/AddressService.Application.csproj
dotnet add reference ../AddressService.Infrastructure/AddressService.Infrastructure.csproj
dotnet add reference ../AddressService.API/AddressService.API.csproj

3. Install NuGet Dependencies

AddressService.Application:

dotnet add package AutoMapper
dotnet add package FluentValidation

AddressService.Infrastructure:

dotnet add package Microsoft.EntityFrameworkCore
dotnet add package Microsoft.EntityFrameworkCore.SqlServer

AddressService.API:

dotnet add package Serilog.AspNetCore
dotnet add package Serilog.Sinks.Console
dotnet add package Serilog.Sinks.File
dotnet add package Swashbuckle.AspNetCore

AddressService.Tests:

dotnet add package Microsoft.NET.Test.Sdk
dotnet add package MSTest.TestAdapter
dotnet add package MSTest.TestFramework
dotnet add package Moq

4. Update Connection String

Edit AddressService.API/appsettings.json:

{
  "ConnectionStrings": {
    "DefaultConnection": "Server=YOUR_SERVER;Database=AddressServiceDb;Trusted_Connection=true;Encrypt=false;"
  }
}

5. Build & Run

# Restore dependencies
dotnet restore

# Build solution
dotnet build

# Run API
cd AddressService.API
dotnet run

# Run tests
cd ../AddressService.Tests
dotnet test

API Endpoints

Address CRUD Operations

  • GET /api/address - Get all addresses
  • GET /api/address/{id} - Get address by ID
  • POST /api/address - Create new address
  • PUT /api/address/{id} - Update address
  • DELETE /api/address/{id} - Delete address

Example Request

curl -X POST https://localhost:5001/api/address \
  -H "Content-Type: application/json" \
  -d '{
    "streetLine1": "123 Main St",
    "city": "New York",
    "stateOrProvince": "NY",
    "postalCode": "10001",
    "country": "USA",
    "addressType": 1,
    "isDefault": true
  }'

Database Migrations

cd AddressService.API

# Create migration
dotnet ef migrations add InitialCreate --project ../AddressService.Infrastructure

# Apply migration
dotnet ef database update --project ../AddressService.Infrastructure

Testing

Run unit tests:

dotnet test AddressService.Tests

# With verbose output
dotnet test AddressService.Tests --verbosity detailed

# Filter by category
dotnet test AddressService.Tests --filter "TestCategory=CreateAsync"

Architecture Highlights

Domain Layer

  • Pure, framework-agnostic entities
  • No external dependencies
  • Clear business logic boundaries

Application Layer

  • Use case orchestration
  • DTO mapping (AutoMapper)
  • Input validation (FluentValidation)
  • Transactional integrity

Infrastructure Layer

  • Entity Framework Core ORM
  • Repository pattern implementation
  • Unit of Work for transaction management
  • Database context configuration

Presentation Layer

  • RESTful controller endpoints
  • Global exception handling
  • Request/response validation
  • Swagger documentation

Technology Stack

  • Framework: .NET 10
  • Database: SQL Server
  • ORM: Entity Framework Core 10
  • Mapping: AutoMapper 13
  • Validation: FluentValidation 11
  • Logging: Serilog 4
  • Testing: MSTest + Moq
  • API Docs: Swagger/OpenAPI

License

MIT License

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages