Skip to main content
Version: Next

Backend Template Code Explanation

CMS.Plugin.MyPluginName is a CMS platform plugin built based on Domain-Driven Design (DDD) principles, designed to provide complete lifecycle management functions for the MyEntityName entity. The project achieves high cohesion and low coupling design goals through a modular architecture, supporting flexible deployment in different database environments (MySQL, PostgreSQL, SQL Server).

The plugin adopts a layered architecture design, including Presentation Layer, Application Layer, Domain Layer, and Infrastructure Layer, ensuring a clear separation between business logic and technical implementation. This project not only provides basic CRUD operations but also integrates advanced functions such as process processing, background tasks, and event handling, suitable for enterprise-level application scenarios requiring extension of CMS platform core capabilities.


Quick Start​

Complete Process for Creating a New Plugin​

1. Clone Template​

Copy the entire template directory and rename the root folder to your plugin name.

2. Replace Placeholders​

Globally search and replace the following placeholders:

  • MyPluginName → Your plugin name (e.g., ProductCatalog)
  • MyEntityName → Your entity name (e.g., Product)

3. Rename Project Files​

Renaming all .csproj files and namespaces containing placeholders accordingly.

Project Structure Overview​

The template adopts a Domain-Driven Design (DDD) architecture, containing multiple layered modules:

  • Application Layer: Handles business logic and API exposure
  • Domain Layer: Defines core entities and business rules
  • Infrastructure Layer: Responsible for data persistence and external integration
  • Abstraction Layer: Provides shared interfaces and constant definitions

Core Component Analysis​

Plugin Entry Point (CMSPluginEntry.cs)​

This is the main entry point of the plugin, responsible for registering services and configuring dependency injection.

Module Initialization (CMSPluginModule.cs)​

Defines the module dependencies and background job configuration of the plugin.

Database Configuration and Initialization​

Database Type Selection​

Decide which database to use via configuration in CMSPluginEntry.cs:

Configuration File Details​

Main Configuration File (appsettings.json)​

Located in the main plugin project, used to configure external APIs.

{
"IMyPluginNameExternalApi": {
"HttpHost": "http://127.0.0.1:18000/"
}
}

Database Connection Configuration​

Located in the EntityFrameworkCore project, defining the database connection string.

{
"ConnectionStrings": {
"Default": "Server=127.0.0.1;Database=CMS.Plugin.MyPluginName;User ID=root;Password=123456"
}
}

Start Service and Verify​

Service Startup Flow​

API Availability Test​

Use curl command to test basic functions:

# Get List
curl -X GET "http://localhost:5000/api/v1/MyPluginName/MyEntityName"

# Create Entity
curl -X POST "http://localhost:5000/api/v1/MyPluginName/MyEntityName" \
-H "Content-Type: application/json" \
-d '{"code":"P001","name":"Test Product"}'

Directory Structure Explanation​

This project adopts a layered architecture based on Domain-Driven Design (DDD), combined with a plugin-based module organization method, with a clear structure and distinct responsibilities. The whole is divided into two main directories: src (source code) and test (test code), managing production code and test code respectively.

Main Module: CMS.Plugin.MyPluginName​

CMS.Plugin.MyPluginName is the main module of the entire plugin, responsible for integrating all layered components and serving as the plugin entry. It references the application layer, data access layer, and database adapter modules, and is the starting point at runtime.

This module contains the following key subdirectories:

  • Controller: Provides Web API controllers, handling HTTP requests
  • Apis: Defines external service call interfaces
  • Jobs: Background job definitions, such as scheduled tasks
  • Workers: Background worker services
  • FlowProcessors: Process processors, used for business process orchestration
  • Flows: Stores process definition files (such as .pfd and FlowInfo.xml)
  • Properties/launchSettings.json: Development environment startup configuration

Application Layer: Application​

The CMS.Plugin.MyPluginName.Application module implements application services, responsible for coordinating domain objects to complete business use cases. It depends on the domain layer and application contract layer, serving as a bridge between the outside (such as controllers) and the domain model.

Key structures:

  • Implements: Concrete implementation classes of application services
  • MapperProfiles: AutoMapper configuration files, used for conversion between DTOs and domain entities

Domain Layer: Domain​

CMS.Plugin.MyPluginName.Domain is where the core business logic resides, encapsulating domain models, repository interfaces, domain events, etc. Following DDD principles, this layer does not depend on any external frameworks or infrastructure.

Main components:

  • MyEntityNames: Contains entity MyEntityName, repository interface IMyEntityNameRepository, and specification pattern classes
  • Data: Defines data access related interfaces, such as ICMSPluginDapperRepository
  • EventHandlers: Domain event handlers
  • Settings: Setting definition providers

Domain Shared Layer: Domain.Shared​

The CMS.Plugin.MyPluginName.Domain.Shared module stores constants, enums, resource files, and basic DTOs (Eto) shared across layers, avoiding circular dependencies.

Core contents include:

  • Enums: Such as MyPluginNameStatus enum, defining plugin state machine
  • Localization: Multi-language resource files (zh-Hans.json, en.json), supporting internationalization
  • MyEntityNames: Entity-related constant definitions (MyEntityNameConsts) and event transfer objects (MyEntityNameEto)

Data Persistence Implementation: EntityFrameworkCore​

The CMS.Plugin.MyPluginName.EntityFrameworkCore module provides data access implementation based on Entity Framework Core, including DbContext and concrete repositories.

Main responsibilities:

  • Implement repository interfaces defined in the Domain layer (such as EfCoreMyEntityNameRepository)
  • Manage database context ICMSPluginDbContext
  • Handle database schema migrations (CMSPluginDbSchemaMigrator)

Database Adapter Modules​

The project provides support for multiple databases, implemented through independent adapter modules:

  • CMS.Plugin.MyPluginName.MySQL
  • CMS.Plugin.MyPluginName.PostgreSql
  • CMS.Plugin.MyPluginName.SqlServer

Each adapter module contains:

  • Specific database implementation of CMSPluginDbContext
  • DbContextFactory for design-time migrations
  • Corresponding module definition (such as CMSPluginMySQLModule)
  • Batch scripts (UpdateDB.bat, UpdateMigrations.bat) for automated database updates

Application Contracts: Application.Contracts​

The CMS.Plugin.MyPluginName.Application.Contracts module defines public contracts for the application layer, including:

  • Dtos: Data Transfer Objects, used for API input and output
  • Services: Application service interfaces, implemented by the Application layer

Abstract Definitions: Abstractions​

The CMS.Plugin.MyPluginName.Abstractions module provides plugin-level abstract definitions, such as:

  • IMyPluginNameFlowService: Flow service interface
  • MyPluginNameActivity: Activity definition base class

Test Structure​

The test directory organizes layered test projects:

  • CMS.Plugin.MyPluginName.Application.Tests: Application layer unit tests
  • CMS.Plugin.MyPluginName.Domain.Tests: Domain layer unit tests
  • CMS.Plugin.MyPluginName.EntityFrameworkCore.Tests: EF Core integration tests
  • CMS.Plugin.MyPluginName.TestBase: Test base classes and shared test data

Module Dependencies​


Detailed Component Analysis​

Application Service Analysis​

The application service layer defines CRUD operations for the MyEntityName entity through the IMyEntityNameAppService interface. This interface is located in the CMS.Plugin.MyPluginName.Application.Contracts project, ensuring clear definition of service contracts.

Domain Entity Analysis​

The MyEntityName entity in the domain layer defines the business data model, including properties and behaviors. This entity is located in the CMS.Plugin.MyPluginName.Domain project and is the core of Domain-Driven Design.

Data Transfer Object Analysis​

Data Transfer Objects (DTOs) are used to pass data between the application layer and clients. MyEntityNameDto and MyEntityNameCreateDto are used for query and create operations respectively.

API Interface Implementation​

Controllers provide standard CRUD RESTful API interfaces.


Troubleshooting Common Issues​

Database Migration Issues​

Ensure the database type is correctly configured and run the corresponding UpdateDB.bat script.

Dependency Injection Errors​

Check if service registration in CMSPluginEntry.cs is complete, especially the addition of plugin modules.

API Inaccessible​

Verify if route configuration is correct, especially version numbers and controller names.

Module Load Failure​

Check module dependencies, service registration, and database connection string configuration.


Performance Considerations​

The project optimizes performance through the ABP framework's caching mechanism and asynchronous programming model. The domain layer uses AbpCachingModule to provide caching support, and the application layer uses AddAutoMapperObjectMapper to optimize object mapping performance.


Summary​

This project provides a CMS plugin template based on Domain-Driven Design, supporting multiple functions and database adaptations. Through the modular design of the ABP framework, the project has good extensibility and maintainability.

Roles of each module are clear:

  • Main Module: Integration and startup
  • Application: Application service coordination
  • Domain: Core business logic
  • EntityFrameworkCore: Data persistence
  • Application.Contracts: API contracts
  • Domain.Shared: Cross-layer shared types
  • Database Adapters: Multi-database support
  • Abstractions: Core abstract definitions

Developers can quickly locate code positions based on function types, understand layering intent, and facilitate maintenance and extension.