series

  1. Develop blog projects based on ABP vNext and.NET Core – build projects using ABP CLI
  2. Develop blog projects based on ABP vNext and.NET Core – slim the project down and make it run
  3. Development blog project based on ABP vNext and.NET Core – Refinement and beautification, Swagger enter
  4. Develop blog project based on ABP vNext and.NET Core – data access and code priority
  5. Development blog project based on ABP vNext and.NET Core – add, delete, change and check custom warehouse
  6. Develop blog project based on ABP vNext and.NET Core – Uniform specification API, wrapper back model
  7. Develop blog projects based on ABP vNext and.NET Core – say Swagger, grouping, description, little green lock
  8. Develop blog projects based on ABP vNext and.NET Core – access GitHub and protect your API with JWT
  9. Develop blog project based on ABP vNext and.NET Core – exception handling and logging
  10. Develop blog projects based on ABP vNext and.NET Core – using Redis to cache data
  11. Develop blog project based on ABP vNext and.NET Core – integrate Hangfire for timed task processing
  12. Develop blog projects based on ABP vNext and.NET Core – Use AutoMapper to map objects
  13. Developing blog projects based on ABP vNext and.NET Core – Best Practices for Timed Tasks (Part 1)
  14. Developing blog projects based on ABP vNext and.NET Core – Best Practices for Timed Tasks (Part 2)
  15. Developing blog projects based on ABP vNext and.NET Core – Best Practices for Timed Tasks (PART 3)
  16. Blog Development project based on ABP vNext and.NET Core
  17. Abp vNext and.NET Core
  18. Blog Development project based on ABP vNext and.NET Core
  19. Blog Development project based on ABP vNext and.NET Core
  20. Blog Development project based on ABP vNext and.NET Core
  21. Abp vNext and.NET Core Development Blog Project – Blazor
  22. Abp vNext and.NET Core Development Blog Project – Blazor – Part 2
  23. Abp vNext and.NET Core Development Blog Project – Blazor
  24. Abp vNext and.NET Core Development Blog Project – Blazor
  25. Abp vNext and.NET Core Development Blog Project – Blazor
  26. Abp vNext and.NET Core Development Blog Project – Blazor – Part 6
  27. Abp vNext and.NET Core Development Blog Project – Blazor
  28. Abp vNext and.NET Core Development Blog Project – Blazor Series (8)
  29. Abp vNext and.NET Core Development Blog Project – Blazor Series (9)
  30. Abp vNext and.NET Core development blog project – Final release project

Previous article (juejin.cn/post/684490…) Hangfire is integrated with the scheduled task processing framework to complete a simple scheduled task processing solution.

IO /zh-Hans/abp/IObjectMapper /abp/IObjectMapper , add AutoMapper dependencies to the project.

Add the AbpAutoMapperModule module dependency to the.Application layer module class.

//MeowvBlogApplicationModule.cs
using Meowv.Blog.Application.Caching;
using Volo.Abp.AutoMapper;
using Volo.Abp.Identity;
using Volo.Abp.Modularity;

namespace Meowv.Blog.Application
{
    [DependsOn(
        typeof(AbpIdentityApplicationModule),
        typeof(AbpAutoMapperModule),
        typeof(MeowvBlogApplicationCachingModule)
    )]
    public class MeowvBlogApplicationModule : AbpModule
    {
        public override void ConfigureServices(ServiceConfigurationContext context)
        {... }}}Copy the code

In this project, the mapping relationship between entities and DTO is mainly dealt with. Post. Cs and PostTo. cs are mapped to each other by taking the addition, deletion, change and check in BlogService.cs as an example.

Let’s look at GetPostAsync(int ID). The previous method was to create objects manually and then assign them one by one. We can imagine that when we have too many fields, we have to write them all. Now with AutoMapper, you can do it in a single line of code.

public async Task<ServiceResult<PostDto>> GetPostAsync(int id)
{
    var result = new ServiceResult<PostDto>();

    var post = await _postRepository.GetAsync(id);
    if (post == null)
    {
        result.IsFailed("The article does not exist.");
        return result;
    }

    //var dto = new PostDto
    / / {
    // Title = post.Title,
    // Author = post.Author,
    // Url = post.Url,
    // Html = post.Html,
    // Markdown = post.Markdown,
    // CategoryId = post.CategoryId,
    // CreationTime = post.CreationTime
    / /};

    var dto = ObjectMapper.Map<Post, PostDto>(post);

    result.IsSuccess(dto);
    return result;
}
Copy the code

ObjectMapper has been injected into ApplicationService, and we inherit from ServiceBase and can use it directly.

One of the most important steps is defining mappings between classes. AutoMapper provides a variety of methods for defining mappings between classes. For more information, see the Documentation for AutoMapper: docs.automapper.org/

One way to define a mapping is to create a Profile class in. Application layer to add MeowvBlogAutoMapperProfile. Cs, direct inheritance Profile can be defined in the constructor.

//MeowvBlogAutoMapperProfile.cs
using AutoMapper;
using Meowv.Blog.Application.Contracts.Blog;
using Meowv.Blog.Domain.Blog;

namespace Meowv.Blog.Application
{
    public class MeowvBlogAutoMapperProfile : Profile
    {
        public MeowvBlogAutoMapperProfile()
        { CreateMap<Post, PostDto>(); CreateMap<PostDto, Post>().ForMember(x => x.Id, opt => opt.Ignore()); }}}Copy the code

CreateMap<> CreateMap<> CreateMap<> CreateMap<> CreateMap<> CreateMap<> CreateMap<> Second: map from PostDto Post. Because the Id attribute exists in Post, but not in PostDto, you can use ForMember(…). To ignore the Id attribute.

Once the mapping rules are defined, add them to your module class.

//MeowvBlogApplicationModule.cs.public override void ConfigureServices(ServiceConfigurationContext context)
	{
	    Configure<AbpAutoMapperOptions>(options =>
	    {
	        options.AddMaps<MeowvBlogApplicationModule>(validate: true);
	        options.AddProfile<MeowvBlogAutoMapperProfile>(validate: true); }); }...Copy the code

Modify the InsertPostAsync(PostDto DTO) method in the same way.

public async Task<ServiceResult<string>> InsertPostAsync(PostDto dto)
{
    var result = new ServiceResult<string> ();//var entity = new Post
    / / {
    // Title = dto.Title,
    // Author = dto.Author,
    // Url = dto.Url,
    // Html = dto.Html,
    // Markdown = dto.Markdown,
    // CategoryId = dto.CategoryId,
    // CreationTime = dto.CreationTime
    / /};

    var entity = ObjectMapper.Map<PostDto, Post>(dto);

    var post = await _postRepository.InsertAsync(entity);
    if (post == null)
    {
        result.IsFailed("Add failed");
        return result;
    }

    result.IsSuccess("Added successfully");
    return result;
}
Copy the code

Free hands, less code, nice to test the interface with object mapping.

As you can see, the result is also available, and we will use object mapping a lot in the future as described above.

HttpApi.Hosting layer configuration properties.

Routing rule configuration, the default routing in the Swagger is capitalized, if I want to switch to lowercase can use the following configuration code, are they not written in the book of the module class MeowvBlogHttpApiHostingModule. Cs.

public override void ConfigureServices(ServiceConfigurationContext context)
{... context.Services.AddRouting(options => {// Set the URL to lowercase
	    options.LowercaseUrls = true;
	    // Add a slash after the generated URL
	    options.AppendTrailingSlash = true; }); . }Copy the code

Middleware using HSTS, which adds strict transport security headers.

public override void OnApplicationInitialization(ApplicationInitializationContext context)
{... app.UseHsts(); . }Copy the code

Use the default cross-domain configuration directly.

public override void OnApplicationInitialization(ApplicationInitializationContext context)
{... app.UseCors(); . }Copy the code

Transfer the HTTP request to HTTPS.

public override void OnApplicationInitialization(ApplicationInitializationContext context)
{... app.UseHttpsRedirection(); . }Copy the code

Forwarding proxies the header to the current request and, in conjunction with Nginx, obtains the user’s real IP address.

public override void OnApplicationInitialization(ApplicationInitializationContext context)
{... pp.UseForwardedHeaders(newForwardedHeadersOptions { ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto }); . }Copy the code

This article introduces how to use AutoMapper to map objects to objects. The length is short and the content is relatively simple. 😁 😁 😁

Open source: github.com/Meowv/Blog/…