问题
It is my second question on the topic. My first question is here.
Roman Marusyk provided an easy answer to the question. However, I have more difficult case in reality and it will get even more complex. Therefore, I need to use AutoMapper to map the config (though I would be very happy and surprised if default binding coped with this as well).
Here is my real json with models followed:
{
"startupConfig": {
"noSubscription": {
"calls": [
{
"percentage": 30,
"techPriority": 1,
"timePriority": 2
},
{
"percentage": 30,
"techPriority": 1,
"timePriority": 2
}
],
"profiles": [
{
"type": "startup",
"percentage": 20,
"techPriority": 2,
"timePriority": 1
}
]
}
}
}
namespace FeedService.FeedConfigurations
{
public class FeedConfiguration
{
public ICollection<CallConfiguration> CallsConfig { get; set; }
public ICollection<ProfileConfiguration> ProfilesConfig { get; set; }
}
public class ProfileConfiguration
{
public CompanyTypeEnum CompanyTypeEnum { get; set; }
public int Percentage { get; set; }
public int TechPriority { get; set; }
public int TimePriority { get; set; }
}
public class CallConfiguration
{
public int Percentage { get; set; }
public int TechPriority { get; set; }
public int TimePriority { get; set; }
}
}
Here, as you see I need config value of profiles:type
to be mapped to property with enum type by name. Apparently, default configuration binder does not do magic for me. I also would not like to change type of the property to string or type of the value to integer. Therefore, I still need an answer for the original question for mapping with AutoMapper (where shortened example is enough to extend it for the second part).
===Original question copied for convenience===
I have the following json config file:
{
"startupConfig": {
"noSubscription": {
"calls": [
{
"percentage": 30,
"techPriority": 1,
"timePriority": 2
},
{
"percentage": 30,
"techPriority": 1,
"timePriority": 2
}
]
}
}
}
And here is my code reading from the file:
var config = _mapper.Map<FeedConfiguration>(_configuration
.GetSection("startupConfig").GetChildren()
.FirstOrDefault(cc => cc.Key == "noSubscription")?.GetChildren());
However, the mapping does not work. Here is the code of mapping configuration:
CreateMap<IEnumerable<IConfigurationSection>, CallConfiguration>()
.ForMember(cc => cc.Percentage,
mo => mo.MapFrom(css => css.FirstOrDefault(cs => cs.Key == "percentage").Value))
.ForMember(cc => cc.TechPriority,
mo => mo.MapFrom(css => css.FirstOrDefault(cs => cs.Key == "techPriority").Value))
.ForMember(cc => cc.TimePriority,
mo => mo.MapFrom(css => css.FirstOrDefault(cs => cs.Key == "timePriority").Value));
CreateMap<IEnumerable<IConfigurationSection>, FeedConfiguration>()
.ForMember(fc => fc.CallsConfig,
mo => mo.MapFrom(css => css.FirstOrDefault(cs => cs.Key == "calls").GetChildren()));
Classes that I am mapping to:
namespace FeedService.FeedConfigurations
{
public class FeedConfiguration
{
public ICollection<CallConfiguration> CallsConfig { get; set; }
}
public class CallConfiguration
{
public int Percentage { get; set; }
public int TechPriority { get; set; }
public int TimePriority { get; set; }
}
}
And here is an exception I get:
AutoMapper.AutoMapperConfigurationException:
Unmapped members were found. Review the types and members below.
Add a custom mapping expression, ignore, add a custom resolver, or modify the source/destination type
For no matching constructor, add a no-arg ctor, add optional arguments, or map all of the constructor parameters
=============================================================================================================
AutoMapper created this type map for you, but your types cannot be mapped using the current configuration.
IConfigurationSection -> CallConfiguration (Destination member list)
Microsoft.Extensions.Configuration.IConfigurationSection -> FeedService.FeedConfigurations.CallConfiguration (Destination member list)
Unmapped properties:
Percentage
TechPriority
TimePriority
Would be very thankful for your help!
回答1:
Your profile must look like this
namespace FeedService.FeedConfigurations
{
public class FeedConfigurationMappingProfile : Profile
{
public FeedConfigurationMappingProfile()
{
CreateMap<IConfigurationSection, FeedConfiguration>()
.ForMember(fc => fc.Calls,
mo => mo.MapFrom(fc => fc.Get<FeedConfiguration>().Calls));
CreateMap<IConfigurationSection, CallConfiguration>()
.ForMember(cc => cc.Percentage,
mo => mo.MapFrom(css => css.GetChildren().FirstOrDefault(cs => cs.Key == "percentage").Value))
.ForMember(cc => cc.TechPriority,
mo => mo.MapFrom(css => css.GetChildren().FirstOrDefault(cs => cs.Key == "techPriority").Value))
.ForMember(cc => cc.TimePriority,
mo => mo.MapFrom(css => css.GetChildren().FirstOrDefault(cs => cs.Key == "timePriority").Value));
}
}
}
and then use mapper
_mapper.Map<FeedConfiguration>(_configuration.GetSection("startupConfig:noSubscription"));
EDIT(additional option)
CreateMap<IConfigurationSection, FeedConfiguration>()
.ForMember(fc => fc.Calls,
mo => mo.MapFrom(fc => fc.GetChildren().FirstOrDefault(fd=>fd.Key == "calls").GetChildren()));
来源:https://stackoverflow.com/questions/56476564/automapper-map-from-collection-of-iconfigurationsection-with-complex-mapping