Giter VIP home page Giter VIP logo

Comments (7)

BuzzDee3000 avatar BuzzDee3000 commented on May 18, 2024

Hi jigneshkvp,
have you tried to define a custom constructor configuration?

//Example using a non-default constructor TypeAdapterConfig<TSource, TDestination>.NewConfig() .ConstructUsing(src => new TDestination(src.Id, src.Name));

from mapster.

eswann avatar eswann commented on May 18, 2024

THanks for the heads-up, the abstract type should be ok. I'll add a test case to test this in a bit.

from mapster.

eswann avatar eswann commented on May 18, 2024

Ahh...I see the issue now. Because your ProductDto has a list of VehicleDto, it's trying to map to the VehicleDto type (meaning it will try to new up this type). It can't assume you want to map to CarDto just because that is one mapping that is defined. You have to know the destination type you are mapping to. Problem is that if you have several vehicle types, you'll need some kind of logic in the mapping to differentiate the type to map to.

from mapster.

chaowlert avatar chaowlert commented on May 18, 2024

This is interesting issue. I will check how EF can return information from abstract class. For now, could you remove from abstract modifier from VehicleDto class?

from mapster.

jigneshkvp avatar jigneshkvp commented on May 18, 2024

Hi,

I managed to do something like this as suggested by BuzzDee3000 (Thanks!):

TypeAdapterConfig<Vehicle, VehicleDto>.NewConfig()
.ConstructUsing(src => MapVehicle(src));

public VehicleDto MapVehicle(Vehicle vehicle)
{
  var vehicleType = vehicle.GetType().BaseType;
  if(vehicleType == null) return null;

  if(vehicleType.FullName == typeOf(Car).Name)
      return new CarDto();

 if(vehicleType.FullName == typeOf(Truck).Name)
      return new TruckDto();  

 return null;
}

In AutoMapper 4.2.1, inheritance is handled as follows and it automatically takes care of instantiating concrete classes from abstract classes:

var config = new MapperConfiguration(cfg =>  
                           {
                                  cfg.CreateMap<Vehicle, VehicleDto>()
                                       .Include<Car, CarDto>()
                                       .Include<Truck, TruckDto>();
                           });

It would have been really great if we could have something similar in Mapster.

Also, what I do use in AutoMapper is the "Profile" class, which provides named configuration for maps.

using AutoMapper;
public class VehicleProfile : Profile
{
   protected override void Configure()
    {
         var config = new MapperConfiguration(cfg =>  
                           {
                                  cfg.CreateMap<Vehicle, VehicleDto>()
                                       .Include<Car, CarDto>()
                                       .Include<Truck, TruckDto>();
                           });
    }
}

So I can register/add all profiles during Application_Start and then my Mapping is done once for the session. I then resolve the mapping using the "MappingEngine" class which I pass as a dependency wherever required.

Could you suggest me something similar in Mapster?

Thanks for your quick response! 👍

from mapster.

eswann avatar eswann commented on May 18, 2024

Hi @jigneshkvp , it make sense to see if we have a mapping that qualifies as a "default/defined" mapping for that destination type already registered. I'll leave this issue open and take a look.

from mapster.

chaowlert avatar chaowlert commented on May 18, 2024

Hi, I added Include command in Mapster 3.0 (https://github.com/chaowlert/Mapster/wiki/Config-inheritance), please try. Thx u.

from mapster.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.