question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Is that possible that the mapper can base on the source type to map?

See original GitHub issue

I used AutoMapper (and mediatr which is also developed by Jimmy Bogard) in dotnet core before. It’s pretty nice that see it again in typescript.

However, the code base currently use the destination class to found out the source class is pretty strange.

Thats mean if we have two different source class to project to same vm model will get wrong.

Here is the example that

class User {
  @Expose()
  firstName!: string;
  @Expose()
  lastName!: string;
  @ExposedType(() => Profile)
  profile!: Profile;
}

class User2 {
  @Expose()
  firstName!: string;
  @Expose()
  lastName!: string;
  @ExposedType(() => Profile)
  profile!: Profile;
}

    mapper
      .createMap(User, UserVm)
      .forMember(d => d.first, opts => opts.mapFrom(s => "Khang"))
      .forMember(d => d.last, opts => opts.mapFrom(s => s.lastName))
      .forMember(
        d => d.full,
        opts => opts.mapFrom(s => s.firstName + " " + s.lastName)
      );
    mapper
      .createMap(User2, UserVm)
      .forMember(d => d.first, opts => opts.fromValue("a"))
      .forMember(d => d.last, opts => opts.fromValue("B"))
      .forMember(
        d => d.full,
        opts => opts.mapFrom(s => s.firstName + " " + s.lastName)
      );

Now if we project a User2 obj to UserVm it will still keep using the first mapping

refs: https://codesandbox.io/s/automapper-demo-b3d7p

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
nartccommented, Jan 21, 2020

@vip30 nestjsx-automapper 2.0.7 has been released.

0reactions
vip30commented, Jan 21, 2020

It works as expected and will wait for your release on nestjsx-automapper Thanks you

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to map from base type of object? - Stack Overflow
that I want to map between where in both Source and Destination the property Data is some type for which there exists a...
Read more >
One-Stop Guide to Mapping with MapStruct - Reflectoring
Mapper Definition. We will now take a look into various types of bean mappers using MapStruct and try out whatever options are available....
Read more >
Mapper Type - Apache Ant
This mapper implementation can contain multiple nested mappers. File mapping is performed by passing the source filename to the first nested mapper, its...
Read more >
Using Multiple Source Objects with MapStruct - Baeldung
Learn how to use multiple source objects with MapStruct. ... We can now define a mapper that maps a Customer object to a...
Read more >
MapStruct 1.5.3.Final Reference Guide
MapStruct takes all public properties of the source and target types into account. This includes properties declared on super-types. 3.2.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found