asp.net-coreentity-framework-coremappingautomappermapster

Is this the right way for mapping entities and dtos with using Mapper?


I have a Layer as Services and added a class as Mapper and i want to put all of my mapping here. and i'm using asp.net core 5 and Mapster for mapping am i do right? this is my Code :

public class Mapping
{
    public static void InitializeAutomapper()
    {
        TypeAdapterConfig<BookMark, BookMarkDTO>.ForType().Map(dest => dest.Id, src => src.Id);
        TypeAdapterConfig<Category, CategoryDTO>.ForType().Map(dest => dest.Id, src => src.Id);
        TypeAdapterConfig<Comment, CommentDTO>.ForType().Map(dest => dest.Id, src => src.Id);
        TypeAdapterConfig<Like, LikeDTO>.ForType().Map(dest => dest.Id, src => src.Id);
        TypeAdapterConfig<Post, PostDTO>.ForType().Map(dest => dest.Id, src => src.Id);
        TypeAdapterConfig<Role, RoleDTO>.ForType().Map(dest => dest.Id, src => src.Id);
        TypeAdapterConfig<Status, StatusDTO>.ForType().Map(dest => dest.Id, src => src.Id);
        TypeAdapterConfig<Tag, TagDTO>.ForType().Map(dest => dest.Id, src => src.Id);
        TypeAdapterConfig<User, UserDTO>.ForType().Map(dest => dest.Id, src => src.Id);
    }
}

Solution

  • It's not right... If the compiler doesn't yell at you... then something was wrong with the IDE itself.

    As far as I can see, seems like we want to create a mapping config out of Mapster. Do it like this:

    // Somewhere we could access IServiceCollection
    var config = new TypeAdapterConfig();
    config.NewConfig<BookMark, BookMarkDTO>.ForType().Map(dest => dest.Id, src => src.Id);
    config.NewConfig<Category, CategoryDTO>.ForType().Map(dest => dest.Id, src => src.Id);
    config.NewConfig<Comment, CommentDTO>.Map(dest => dest.Id, src => src.Id);
    config.NewConfig<Like, LikeDTO>.Map(dest => dest.Id, src => src.Id);
    config.NewConfig<Post, PostDTO>.Map(dest => dest.Id, src => src.Id);
    config.NewConfig<Role, RoleDTO>.Map(dest => dest.Id, src => src.Id);
    config.NewConfig<Status, StatusDTO>.Map(dest => dest.Id, src => src.Id);
    config.NewConfig<Tag, TagDTO>.Map(dest => dest.Id, src => src.Id);
    config.NewConfig<User, UserDTO>.Map(dest => dest.Id, src => src.Id);
    
    // Register config as singleton, ServiceMapper as scope.
    services.AddSingleton(config);
    // Make sure we have Mapster.DependencyInjection package installed
    services.AddScoped<IMapper, ServiceMapper>();
    
    // Using it be like
    public IActionResult MapsterStaticMappingTest([FromServices] MapsterMapper.IMapper mapper)
    {
        var book = new BookMark();
        var bookMap = mapper.From(book).AdaptToType<BookMarkDTO>();
        return Ok(bookMap);
    }
    

    The code was wrote on .Net 5, Mapster 7.2.0

    UPDATE

    As I just look back the Mapster source, a new public static class TypeAdapterConfig<TSource, TDestination> was there... I don't remember that the last time I look it was there... So this is my bad... your code was totally valid.

    Just for a note, static class TypeAdapterConfig<TSource, TDestination> make all change under GlobalSettings. Which would stand there, still doing nothing if we're not make use of some ServiceMapper, which have a direct dependency on TypeAdapterConfig concrete type.