// code
Code Samples
Custom Middleware Logging
This code demonstrates how to create custom middleware in ASP.NET Core to intercept incoming HTTP requests and log their paths. By placing this reusable logic in the middleware pipeline, you can monitor and analyze request traffic for debugging or analytics purposes. It leverages the built-in HttpContext to capture request details before passing control to the next component.
public async Task Invoke(HttpContext context) {
Console.WriteLine(context.Request.Path);
await _next(context);
}
Fluent Validation Rules
Easily validate user input using FluentValidation.
RuleFor(x => x.Email)
.NotEmpty()
.EmailAddress();
Repository Pattern (Generic)
Generic abstraction for data access operations.
public interface IRepository<T> {
Task<T> GetById(int id);
Task<IEnumerable<T>> GetAll();
}
Unit of Work Pattern
Coordinate transactions across multiple repositories.
public interface IUnitOfWork {
IUserRepository Users { get; }
Task SaveChangesAsync();
}