Base template project for development with ASP.NET server using DDD, CQRS and Clean Architecture principles.
The project contains some base entities to simulate tasks, categories and users.
You may need configure appsettings.json to get it to work.
This is a sample, you may change it and adapt to your needs.
- Entity framework ORM (tested with SQL Server)
- DDD based implementation
- Unit and Integration Tests
- CQRS pattern for use cases
- Cross-cutting concerns: Auto logging with independent transaction, error handler, auto rollback
- Migrations system
- Result pattern return, avoiding throws
- Api fixed base response
- Unity of Work and repository pattern
- API Base result format
- JWT Authentication
- BCrypt for security of passwords
The project has a layered structure as follows:
Note: persistence and infraestructure are the same in my project
This layer is responsible for the presentation of the application, defining endpoints (controllers) and responses.
This layer typically receives data from the front-end and calls a mediator through the CQRS pattern, which then invokes the application layer.
It should also handle how the response data should be presented.
/// <summary>/// Create a new category/// </summary>[Authorize][HttpPost][SwaggerResponse(StatusCodes.Status201Created,Type=typeof(BaseResponse))][SwaggerResponse(StatusCodes.Status400BadRequest,Type=typeof(BaseResponse))][SwaggerResponse(StatusCodes.Status500InternalServerError,Type=typeof(BaseResponse))]publicasyncTask<IActionResult>CreateCategory([FromBody]CreateCategoryCommandcommand){returnawaitHandleApplicationResponse<Operation>(command,(resp)=>{returnnew(){Success=resp.Success,Response=null,ErrorMessage=resp.Message,Code=resp.Success?201:400};});}This layer coordinates the use cases, interacting with the domain layer, infrastructure layer, and presentation layer.
In our project, the CQRS pattern is used. In the code, you can see that the application layer is responsible for invoking the object creation, validating it, persisting it, and returning the result.
publicasyncTask<Operation>Handle(CreateCategoryCommandrequest,CancellationTokencancellationToken){if(request==null)returnOperation.MakeFailure("Invalid request");varcreateModel=_mapper.Map<CreateCategoryModel>(request);createModel.UserId=_tokenService.GetToken().Id;varnewCategoryResult=await_categoryBusiness.Create(createModel);if(!newCategoryResult.Success)returnOperation.MakeFailure(newCategoryResult.Message);await_uow.Begin();await_categoryRepo.Create(newCategoryResult.Content);await_uow.Save();await_uow.Commit();returnOperation.MakeSuccess();}This layer contains the business logic and is independent of all other layers. Here, we have the domain entities modeled with the business logic.
/// <summary>/// Category entity/// </summary>publicsealedclassCategory:BaseEntity{publicstringName{get;privateset;}publicintUserId{get;privateset;}privateCategory(){}privateCategory(stringname,intuserId){Name=name;UserId=userId;}publicstaticResult<Category>Create(stringname,intuserId){varresult=ValidateAll(name,userId);if(!result.Success)returnResult.MakeFailure<Category>(result.Message);varcategory=newCategory(name,userId);returnResult.MakeSuccess(category);}
...This layer is responsable for data persistence and other services, usually will contain the code for the ORM and return domain entities.
In this layer we also have the persistence entities that are used by the ORM.
/// <summary>/// Repository implementation for the Category entity/// </summary>publicclassCategoryRepository:Repository<DbCategory>,ICategoryRepository{publicCategoryRepository(DatabaseContextctx,IServiceProviderprovider):base(ctx.Categories,provider){}publicasyncTask<bool>ExistsByName(stringname,int?userId,int?currentId){varfilter=newFilter<DbCategory>(x =>x.Name==name);if(userId.HasValue&&userId!=default)filter.And(x =>x.UserId==userId.Value);if(currentId.HasValue&¤tId!=default)filter.And(x =>x.Id!=currentId.Value);returnawait_dbSet.AnyAsync(filter.GetExpression());}
...Responsible for the dependency injection (DI) and resolving dependencies of services.
Swagger is configured with basic documentation. It's possible to see the input data and the returning data according to the response code
The project contains two test projects: unit tests and integration tests.
In our project, unit tests were performed in the domain layer, at the entity and domain service levels, using Moq to ensure there were no external dependencies (from repositories).
You may need configure appsettings.Tests.json to get it to work.
In our project, integration tests are performed using a test SQL Server with fixed data at the infrastructure, application, and API layers.






