Comments (7)
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.
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.
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.
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.
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.
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.
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)
- Regression bug - works on 7.3.0 not on 7.4.0 HOT 2
- Can't use conditional mapping with different types of source field HOT 2
- The tool doesn't seem to work on net8.0 HOT 3
- mapster is missing NuGet package README file HOT 1
- NullReferenceException with FastExpressionCompiler HOT 1
- Value overwritten by re-mapping
- Support NET. 8.0 - Mapster.Tool HOT 2
- Update Mapster.Core to target netstandard2.0
- PreserveReference is using the src PropertyName to set the Destination property.
- Mapster tool How to declare custom mapping.
- Mapster re-writing value when mapping resulting in wrong destination type HOT 2
- Adapt to IList<T> does not update the destination IList<T>
- How Map From A Method Like Update ?
- Struggling with mapping readonly ICollection property
- EFcore uses ProjectToType mapping errors
- Destination and source type mismatch does not seem to be validated
- How can I obtain the changed field values after mapping two entities?
- New object is created on destination instead of null when using Adapt<> HOT 2
- Mapper NOT mapping capitalized abbreviations to record property names
- Adapt/AdaptToType DOES NOT working with anonymous object that returned from method
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mapster.