How lion3993vn/njs1703_himari_be works
Project Type & Purpose
• This is a backend API built on .NET 8.0 that uses Entity Framework Core for database interactions. It serves as the backend for a project called “Himari” and includes real-time AI chatbot streaming and integration with the PayOS VietQR payment system.
• The project is organized as a multi-project solution with clearly separated layers:
– The API layer (HimariServer.API) that handles HTTP requests.
– The Repository layer (HimariServer.Repository) which manages Entity Framework Core interactions, database migrations, and data access patterns (using repositories and unit of work).
– The Service layer (HimariServer.Service) which handles business logic and coordinates between the API layer and the repository, including integrations with external systems (e.g., Redis, Firebase, Mail, deep learning models).
File Structure
• The HimariServer.API directory contains Controllers, Middlewares, Program.cs, settings (appsettings.json in Development) and dependency injection – this is your entry point where client requests are handled.
• The HimariServer.Repository directory holds:
– DBContext, Entities (domain models), Enums, Migrations, and Repository implementations for handling CRUD operations.
– A UnitOfWork pattern that coordinates repository operations.
• The HimariServer.Service directory is organized into:
– Business Models that serve as DTOs.
– Service Interfaces and Implementations that encapsulate business logic.
– Integration points for real-time processing (e.g., ChatHub for streaming chat), and external integrations with payment (PayOS), onnx embedding for AI, Redis caching, Firebase storage, and email services.
• The existence of configuration files (Dockerfile, GitHub workflows, launchSettings) further indicates support for CI/CD, containerization, and local server runtime configurations.
Key Components
• API Layer (HimariServer.API):
– Controllers: Each controller (e.g., Auth, Blog, Payment, Chat) forms an entry point for different functionalities.
– Middleware: Exception handling and authentication from the pipeline.
– Dependency Injection Configuration: Sets up service instances for controllers.
• Service Layer (HimariServer.Service):
– Business Logic: Handles operations for domains like blogs, orders, products, notifications, authentication, and real-time chat.
– External Service Integrations: ChatHub for real-time communications, MailService for emails, Payment services (PayOS), and services for AI/embedding (using ONNX with Vietnamese embedding, Deepseek, Gemini, ChromaDB).
• Repository Layer (HimariServer.Repository):
– Data Access: Repositories for different domain entities (Blog, Category, Order, etc.) and the Entity Framework DBContext.
– Unit of Work: Coordinates transactions across repositories.
– Database Migrations & Entities: Show the automatic schema evolution.
• External Systems:
– SQL Server: Primary relational database as defined in the appsettings.
– Redis: For caching and session management.
– Firebase Storage: For file storage.
– Payment Gateway: PayOS integration for VietQR payments.
– Email Server: SMTP details (configured with Gmail settings) for sending notifications.
– AI & ML Utilities: The inclusion of an ONNX model and integration settings for deep learning tools (Deepseek, Gemini, HuggingFaceTokenizer).
Relationships & Data Flow
• Client / External Request Flow: Client requests hit the API Controllers.
• API Layer: Controllers delegate requests to the Service Layer after handling authentication and middleware processing.
• Service Layer: Processes business logic, performs validations, and then delegates data persistence or retrieval to the Repository Layer.
• Repository Layer: Interacts with the SQL Server using Entity Framework Core, managing data through repositories and the Unit of Work.
• Real-time interactions: Include a component for SignalR (ChatHub) that illustrates bi-directional communication for chat functionalities.
Architectural Patterns & Design Principles
• Layered Architecture: Clearly separate Controllers (presentation/API) → Services (business logic) → Repositories (data access).
• Domain-Driven Design: The project separates domain entities, business models, and service contracts.
• Integration Concerns: Document how external systems are leveraged and the configuration-driven approach (e.g., JWT, Redis, Firebase, Payment API keys).