I am trying to map source string to List<T> for member but the value of the mapped member list property is always null.
Here is my code.
//Source model
public class Claims
{
public int Id { get; set; }
public string ClaimType {get; set; }
public string ClaimValue { get; set; } // produce json string [{"Action":"read","Status":"active"}]
}
// Dest model
public class ClaimsDto
{
public int Id { get; set; }
public string ClaimType { get; set; }
public List<ResourceActions> ClaimValues { get; set; }
}
public class ResourceActions
{
public string Action { get; set; }
public string Status { get; set; }
}
CreateMap<ResourceActions, ResourceActions>();
CreateMap<Claims, ClaimsDto>()
.ForMember(dest => dest.ClaimValues,
opt => opt.MapFrom( src=> JsonConvert.DeserializeObject<List<ResourceActions>>(src.ClaimValue)))
.ReverseMap();
Also tried with resolver like this.
.ForMember(dto => dto.ClaimValues, opt => opt.ResolveUsing<CustomResolver,string>(src=>src.ClaimValue ))
The resolver:
public class CustomResolver : IMemberValueResolver<Claims, ClaimsDto, string, List<ResourceActions>>
{
public List<ResourceActions> Resolve(Claims source, ClaimsDto destination, string sourceMember, List<ResourceActions> destinationMember, ResolutionContext context)
{
var data = JsonConvert.DeserializeObject<List<ResourceActions>>(sourceMember);
return context.Mapper.Map<List<ResourceActions>>(data); // not working
return JsonConvert.DeserializeObject<List<ResourceActions>>(sourceMember); // not working
}
}
And the controller
var mapped = _mapper.Map<List<ClaimsDto>>(source); // success without exception but the list property is null.
Other properties are mapped except this string to List<T> property.
I think this profile should solve the issue:
public class MyMapperProfile : Profile
{
public MyMapperProfile()
{
CreateMap<Claims, ClaimsDto>()
.ForMember(dto => dto.ClaimValues, cfg => cfg.MapFrom((claim, _) =>
JsonSerializer.Deserialize<IReadOnlyCollection<ResourceActions>>(claim.ClaimValue)));
}
}
Here is an example:
var config = new MapperConfiguration(cfg => cfg.AddProfile<MyMapperProfile>());
var mapper = config.CreateMapper();
var someClaims = new List<Claims>
{
new Claims { Id = 5, ClaimType = "Foo", ClaimValue = #"[{""Action"":""read"",""Status"":""active""}]" },
new Claims { Id = 7, ClaimType = "Bar", ClaimValue = #"[{""Action"":""create"",""Status"":""disabled""}]" },
};
var result = mapper.Map<List<ClaimsDto>>(someClaims);
foreach (var item in result)
{
Console.WriteLine(JsonSerializer.Serialize(item));
}
Related
I have a property in my destination entity with IgnoreMap attribute.
I want to disable only once. I use Automapper list to list mapping.
public class TestDto {
public string Name { get; set; }
public DateTime UpdateDate { get; set; }
}
public class Test {
public string Name { get; set; }
//Normally, I want to ignore this entities all mapping except one method.
[IgnoreMap]
public DateTime UpdateDate { get; set; }
}
class Program {
public void MapMethod(List<TestDto> sourceList)
{
var content = new MapperConfigurationExpression();
content.CreateMap<TestDto,Test>();
var config = new MapperConfiguration(content);
var mapper = config.CreateMapper();
//I do not want to ignore UpdateDate entity in here.
var destinationList = mapper.Map<List<Test>>(sourceList);
}
}
you can try this:
_mapper.Map<DestType>(result, options => options.AfterMap((s, d) => ((DestType) d).Code = null));
Full Example
void Main()
{
IConfigurationProvider conf = new MapperConfiguration(exp => exp.CreateMap<Src, Dest>());
IMapper mapper = new Mapper(conf);
var src = new Src(){
Id =1,
Name= "John Doe"
};
var result = mapper.Map<Dest>(src, options => options.AfterMap((s, d) => ((Dest) d).Name = null));
result.Dump();
var result2 = mapper.Map<List<Dest>>(srcList, options => options.AfterMap((s, d) => ((List<Dest>) d).ForEach(i => i.Name = null)));
result2.Dump();
}
public class Src
{
public int Id {get; set;}
public string Name {get; set;}
}
public class Dest
{
public int Id {get; set;}
public string Name {get; set;}
}
Alternatively
void ConfigureMap(IMappingOperationOptions<Src, Dest> opt)
{
opt.ConfigureMap()
.ForMember(dest => dest.Name, m => m.Ignore());
};
var result3 = mapper.Map<List<Dest>>(srcList, ConfigureMap());
result3.Dump();
public class SourceExamModel
{
public int ExamId { get; set; }
public List<SectionModel> Sections { get; set; }
}
public class DesiationExamModel
{
public in ExamId {get;set;}
public System.Collections.Generic.IEnumerable<SectionModel> DestSections
{
get
{
}
set
{
}
}
What I tried:
var config = new MapperConfiguration(cfg => {
cfg.CreateMap<CrmMapper.SourceExamModel, CrmMapper.DestiationExamModel>()
.ForMember(v => v.Id, opts => opts.MapFrom(src => src.Id))
.ForMember(v => v.DestSections, opts => opts.MapFrom(src => src.SourceSections));
});
IMapper mapper = config.CreateMapper();
var source = new ExamModel();
var dest = mapper.Map<SourceExamModel, CrmMapper.DestiationExamModel>(source);
Can ayone help me how to map list of comple objects o los of complex objects
Assuming half of your example code is simple spelling mistakes, you pretty much have it working.
If the properties on the source and destination are named the same, you don't have to explicitly map them.
Your source in your example doesn't make sense, it needs the correct object and data.
Here's my attempt at a working example you can copy past into a console application.
class Program
{
static void Main(string[] args)
{
var config = new MapperConfiguration(cfg =>
cfg.CreateMap<SourceExamModel, DestinationExamModel>()
.ForMember(dest => dest.DestSections, c => c.MapFrom(src => src.Sections))
);
config.AssertConfigurationIsValid();
var mapper = config.CreateMapper();
var source = new SourceExamModel
{
ExamId = 1,
Sections = new List<SectionModel> { new SectionModel { SectionId = 1 }, new SectionModel { SectionId = 2 } }
};
var destination = mapper.Map<SourceExamModel, DestinationExamModel>(source);
}
}
public class SourceExamModel
{
public int ExamId { get; set; }
public List<SectionModel> Sections { get; set; }
}
public class DestinationExamModel
{
public int ExamId { get; set; }
public List<SectionModel> DestSections { get; set; }
}
public class SectionModel
{
public int SectionId { get; set; }
}
Not sure that I'm wording this the right way so hopefully the example is clear enough.
What I'm trying to do seems pretty basic to me so I'm assuming I'm missing something obvious.
For this example, the two ForMember mappings are trivial and get the job done. The question is for a more complex class, given any intermediate mappings are configured, how do you simply map the property of one object to the entire destination?
I searched for a while now and the closest I came to finding an answer is here but the ConvertUsing syntax doesn't work for me (I'm using Automapper 4.2.1)
Here are the example classes:
public class UserRoleDto
{
public string Name { get; set; }
public string Description { get; set; }
}
public class DbRole
{
public Guid RoleId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
}
public class DbUserRole
{
public Guid UserId { get; set; }
public DbRole Role { get; set; }
}
And here's my test case with the Automapper config setup (testing in LINQPad, that's what the Dump() is for at the end of the last line)
var dbRole = new DbRole { RoleId = Guid.NewGuid(), Name = "Role Name", Description = "Role Description" };
var dbUserRole = new DbUserRole { UserId = Guid.NewGuid(), Role = dbRole };
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<DbRole, UserRoleDto>();
/* Works but verbose for a class with more than a few props */
cfg.CreateMap<DbUserRole, UserRoleDto>()
.ForMember(dest => dest.Name, opt => opt.MapFrom(src => src.Role.Name))
.ForMember(dest => dest.Description, opt => opt.MapFrom(src => src.Role.Description))
;
});
config.AssertConfigurationIsValid();
var mapper = config.CreateMapper();
var userRoleDto = mapper.Map<UserRoleDto>(dbUserRole).Dump();
How about passing in the sub-object to be mapped? E.g.
cfg.CreateMap<DbRole, UserRoleDto>();
Then, instead of mapping dbUserRole, you would map dbUserRole.Role.
var userRoleDto = mapper.Map<UserRoleDto>(dbUserRole.Role);
Here is another similar example using the following classes:
public class Person
{
public int person_id;
public int age;
public string name;
}
public class Address
{
public int address_id;
public string line1;
public string line2;
public string city;
public string state;
public string country;
public string zip;
}
public class PersonWithAddress
{
public int person_id;
public int age;
public string name;
public InnerAddress address;
}
public class InnerAddress
{
public string city;
public string state;
public string country;
}
With the following test case:
var person = new Person { person_id = 100, age = 30, name = "Fred Flintstone" };
var address = new Address { address_id = 500, line1 = "123 Main St", line2 = "Suite 3", city = "Bedrock", state = "XY", country = "GBR", zip="90210" };
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<Person, PersonWithAddress>();
cfg.CreateMap<Address, InnerAddress>();
});
var mapper = config.CreateMapper();
var person_with_address = mapper.Map<Person, PersonWithAddress>(person);
person_with_address.address = new InnerAddress();
mapper.Map<Address, InnerAddress>(address, person_with_address.address);
Regards,
Ross
I have the code, which use the userClass converters. I want to do the same thing using automapper. How to rewrite the code?
public static ClaimIdentityView ConvertToClaimIdentityView(this ClaimsIdentity Identity)
{
ClaimIdentityView result = new ClaimIdentityView()
{
Name = Identity.Name,
NameClaimType = Identity.NameClaimType,
AuthenticationType = (AuthenticationTypeEnum)EnumStringValue.Parse(typeof(AuthenticationTypeEnum), Identity.AuthenticationType),
RoleClaimType = Identity.RoleClaimType
};
foreach (Claim item in Identity.Claims)
result.ClaimViewList.Add(item.ConvertToClaimView());
return result;
}
public static ClaimView ConvertToClaimView(this Claim Claim)
{
return new ClaimView()
{
Type = Claim.Type,
Value = Claim.Value,
ValueType = Claim.ValueType
};
}
And the second class (the first one is from System.Security.Claims; namespace):
public class ClaimIdentityView
{
public ClaimIdentityView()
{
ClaimViewList = new List<ClaimView>();
}
public Guid UserId { get; set; }
public AuthenticationTypeEnum AuthenticationType { get; set; }
public IList<ClaimView> ClaimViewList { get; set; }
public string Name { get; set; }
public string NameClaimType { get; set; }
public string RoleClaimType { get; set; }
}
Your mappings would look like this:
AutoMapper.Mapper.CreateMap<ClaimsIdentity, ClaimIdentityView>()
.ForMember(dest => dest.ClaimViewList, opt => opt.MapFrom(src => src.Claims))
.ForMember(dest => dest.AuthenticationType,
opt => opt.MapFrom(src => (AuthenticationTypeEnum)
EnumStringValue.Parse(typeof (AuthenticationTypeEnum), src.AuthenticationType)));
AutoMapper.Mapper.CreateMap<Claim, ClaimView>();
Example mapping code:
var claimIdentity = new ClaimsIdentity(WindowsIdentity.GetCurrent());
var view = AutoMapper.Mapper.Map<ClaimsIdentity, ClaimIdentityView>(claimIdentity);
This test would then pass:
var claimIdentity = new ClaimsIdentity(WindowsIdentity.GetCurrent());
// realistically the current account will have claims, but..
claimIdentity.AddClaim(new Claim("Type", "Value"));
var view = AutoMapper.Mapper.Map<ClaimsIdentity, ClaimIdentityView>(claimIdentity);
Assert.IsTrue(view.ClaimViewList.Count > 0);
I have an Order object that has a list of OrderLine objects and an OrderVm object that has a list of OrderLineVm objects. The OrderLine object has a ValueB field that does not exist in the OrderLineVm object.
The problem I have is that ValueB gets set to null by AutoMapper even though I tell it to ignore this property:
[TestFixture]
public class AutomapperDestinationIssueTest2
{
[Test]
public void OrderLineValueBShouldNotBeNull()
{
Mapper.CreateMap<OrderVm, Order>().ForMember(dest => dest.Lines, opt => opt.UseDestinationValue());
Mapper.CreateMap<OrderLineVm, OrderLine>()
.ForMember(dest => dest.ValueB, opts => opts.Ignore());
var orderVm = new OrderVm() { Id = 1 };
orderVm.Lines.Add(new OrderLineVm() { ValueA = "New ValueA"} );
var order = new Order() { Id = 1 };
order.Lines.Add(new OrderLine() { ValueA = "Old ValueA", ValueB = "Old ValueB " });
Mapper.Map(orderVm, order);
Assert.IsNotNull(order.Lines[0].ValueB); // Fails. ValueB is null here.
}
public class OrderLine
{
public string ValueA { get; set; }
public string ValueB { get; set; }
}
public class OrderLineVm
{
public string ValueA { get; set; }
}
public class Order
{
public int Id { get; set; }
public List<OrderLine> Lines { get; set; }
public Order()
{
Lines = new List<OrderLine>();
}
}
public class OrderVm
{
public int Id { get; set; }
public List<OrderLineVm> Lines { get; set; }
public OrderVm()
{
Lines = new List<OrderLineVm>();
}
}
}
What I am missing?
Add an additional mapping to your map creation, the map from list to list and it starts working.
Mapper.CreateMap<OrderVm, Order> ().ForMember(dest => dest.Lines, opt => opt.UseDestinationValue());
Mapper.CreateMap<List<OrderLineVm>,List<OrderLine>> ();
Mapper.CreateMap<OrderLineVm, OrderLine> ().ForMember (d => d.ValueB, opt => opt.Ignore());