-1

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);
    }
}
MrCloner
  • 11
  • 3
  • I copied my code , I just need some help to how define Mapster for entities and DTOs in a separately part – MrCloner Sep 28 '21 at 20:07

1 Answers1

0

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.

Gordon Khanh Ng.
  • 1,352
  • 5
  • 12
  • I used TypeAdapterConfig class that get two parameters as TSource, TDestination Can you please guide me to find the mistake of my work – MrCloner Sep 29 '21 at 19:02
  • @MrCloner I just updated the answer, things went quite a few change since the last time I take a look at it. Mah bad (XD) – Gordon Khanh Ng. Sep 30 '21 at 02:25