Code Explainers

Code explainers tagged #dependency-injection

php
<?php
 
declare(strict_types=1);
 

A single-action JSON user controller in PHP

invokable-class json-api validation
Intermediate 7 steps
rust
use std::sync::Arc;
use axum::{
    body::Body,
    extract::{Extension, State},

Passing per-request context through Axum middleware

middleware request-context dependency-injection
Intermediate 8 steps
typescript
import { DynamicModule, Module, Provider } from '@nestjs/common';
import Redis, { RedisOptions } from 'ioredis';
 
export const REDIS_CLIENT = Symbol('REDIS_CLIENT');

Building a dynamic Redis module in NestJS

dependency-injection dynamic-modules provider-tokens
Intermediate 8 steps
java
package com.example.payments.config;
 
import com.stripe.StripeClient;
import org.springframework.beans.factory.annotation.Value;

Swapping payment gateways by profile in Spring

dependency-injection profiles configuration
Intermediate 6 steps
typescript
import { Injectable, Scope, Inject } from '@nestjs/common';
import { REQUEST } from '@nestjs/core';
import { Request } from 'express';
 

Request-scoped context service in NestJS

dependency-injection request-scope immutability
Intermediate 8 steps
java
package com.example.maintenance;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

How a scheduled cleanup job runs in Spring

scheduling cron transactions
Intermediate 6 steps
typescript
import {
  ArgumentsHost,
  Catch,
  ExceptionFilter,

A catch-all exception filter in NestJS

error-handling exception-filter http
Intermediate 8 steps
typescript
import { Module, Injectable } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import * as Joi from 'joi';
 

Validated, typed config in NestJS

configuration validation dependency-injection
Intermediate 6 steps
php
<?php
 
final class UserRepository
{

A safe PDO user repository in PHP

prepared-statements repository-pattern sql-injection
Intermediate 7 steps
java
@RestController
@RequestMapping("/api/users")
public class UserController {
 

How a Spring REST controller maps users

rest-api dependency-injection dto-mapping
Intermediate 7 steps
typescript
import { inject } from '@angular/core';
import {
  CanActivateFn,
  Router,

Functional route guards in Angular

route-guards dependency-injection observables
Intermediate 5 steps
java
@Service
public class ReportGenerationService {
 
    private final ReportRepository reportRepository;

Async report generation with Spring @Async

async dependency-injection completablefuture
Intermediate 7 steps
python
from fastapi import Depends, FastAPI, HTTPException
from sqlalchemy import create_engine
from sqlalchemy.orm import Session, sessionmaker
 

Wiring SQLAlchemy sessions into FastAPI

dependency-injection orm sessions
Intermediate 7 steps
java
@Service
public class ProductCatalogService {
 
    private final ProductRepository productRepository;

How Spring cache annotations keep data fresh

caching cache-invalidation dependency-injection
Intermediate 7 steps
java
@RestController
@RequestMapping("/api/users")
public class UserController {
 

Bean Validation in a Spring REST controller

validation rest-api exception-handling
Intermediate 9 steps
java
@Service
public class OrderService {
 
    private final OrderRepository orderRepository;

Decoupling side effects with Spring events

event-driven transactions decoupling
Intermediate 8 steps
typescript
import {
  Directive,
  Input,
  TemplateRef,

Building a structural *appUnless directive in Angular

structural-directive template-rendering dependency-injection
Intermediate 8 steps
typescript
import { inject } from '@angular/core';
import { ResolveFn, Router, ActivatedRouteSnapshot } from '@angular/router';
import { catchError, of, EMPTY } from 'rxjs';
import { Article } from './models/article';

Prefetching route data with an Angular resolver

route-resolver dependency-injection rxjs
Intermediate 6 steps