How nguyenhbtrung/electro-shop-backend works
Main Components
• Entry Point & Configuration: Start with Program.cs and appsettings.json which bootstrap the application and house critical configurations like database connections, JWT authentication settings, email configuration, and VNPay parameters.
• API Layer (Controllers): This layer contains multiple controllers (e.g., BannerController, BrandController, CartController, etc.) that receive HTTP requests. Each controller corresponds to a distinct domain (product, order, cart, user, etc.).
• Service Layer: Represented by services (e.g., BannerService, CartService, OrderService) and their corresponding interfaces (located in Services/Interfaces). This layer contains the business logic and orchestrates interactions between the controllers, the data layer, and external integrations.
• Data Access Layer: Illustrated by the ApplicationDBContext in the Data folder, along with the Migrations directory. This layer is responsible for database operations using Entity Framework Core, mapping to the Models/Entities.
• Domain Models: These are split into several areas:
• DTOs (Data Transfer Objects) for communication between layers (in Models/DTOs).
• Entities representing the persistent data models (in Models/Entities).
• Mappers to convert between DTOs and Entities.
• Exceptions & Helpers: Custom exception classes help handle error scenarios while Helpers and Extensions provide utility functionalities (like query handling or VnPay integration).
• Real-Time Communication: The Hubs directory, with ChatHub as an example, indicates integration with SignalR for real-time communication.
• External Integrations: Configured within appsettings.json and supported by services such as VnPayService, EmailService, etc. They let the system process payments and send out emails.
Relationships & Data Flow
• Requests arrive at the Controllers which then invoke the corresponding Service methods.
• Services handle business logic, utilize Helpers and Extensions where needed, and interact with the Data Access Layer (ApplicationDBContext) for CRUD operations.
• The Data Access Layer communicates with a relational database (configured via DefaultSQLConnection) and is evolved via Migrations.
• External service integrations (e.g., VNPay, Email) occur in the Service layer where services (like VnPayService and EmailService) integrate with their respective providers.
• Real-time interactions (ChatHub) may be invoked from controllers or services to handle notifications/messages.
Architectural Patterns
• Layered Architecture: Clearly separated presentation (API), business logic (Services), and data persistence layers.
• Dependency Injection: Evident from the use of interfaces in Services/Interfaces.
• MVC/Mediated Pattern: Controllers act as mediators that route requests to Services.
• Real-Time Communication Pattern: Using SignalR Hubs.
Technology Stack
• ASP.NET Core & C# for backend development.
• Entity Framework Core for data handling.
• SignalR for real-time updates.
• Integration with VNPay and SMTP-based email services.
• JWT for authentication/authorization.
• Visual Studio for development and debugging.