-
Notifications
You must be signed in to change notification settings - Fork 333
Config instance
You may wish to have different settings in different scenarios. If you would not like to apply setting at a static level, Mapster also provides setting instance configurations.
var config = new TypeAdapterConfig();
config.Default.Ignore("Id");
For instance configurations, you can use the same NewConfig
and ForType
methods that are used at the global level with
the same behavior: NewConfig
drops any existing configuration and ForType
creates or enhances a configuration.
config.NewConfig<TSource, TDestination>()
.Map(dest => dest.FullName,
src => string.Format("{0} {1}", src.FirstName, src.LastName));
config.ForType<TSource, TDestination>()
.Map(dest => dest.FullName,
src => string.Format("{0} {1}", src.FirstName, src.LastName));
You can apply a specific config instance by passing it to the Adapt
method. (NOTE: please reuse your config instance to prevent recompilation)
var result = src.Adapt<TDestination>(config);
Or to an Mapper instance.
var mapper = new Mapper(config);
var result = mapper.Map<TDestination>(src);
If you would like to create configuration instance from existing configuration, you can use Clone
method. For example, if you would like to clone from global setting.
var newConfig = TypeAdapterConfig.GlobalSettings.Clone();
Or clone from existing configuration instance
var newConfig = oldConfig.Clone();
Fork
is similar to Clone
, but Fork
will allow you to keep configuration and mapping in the same location. See (https://github.com/MapsterMapper/Mapster/wiki/Config-location) for more info.
var forked = mainConfig.Fork(config =>
config.ForType<Poco, Dto>()
.Map(dest => dest.code, src => src.Id));
var dto = poco.Adapt<Dto>(forked);
- Configuration
- Config inheritance
- Config instance
- Config location
- Config validation & compilation
- Config for nested mapping
- Custom member matching logic
- Constructor mapping
- Before & after mapping
- Setting values
- Shallow & merge mapping
- Recursive & object references
- Custom conversion logic
- Inheritance