You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Description:
When having a class map with different mappings depending on some property when the mapping changes registering the class again with the new mapping does not take affect even though in the object the mapping is reinitialized correctly.
Code example:
publicclassUserMapping:ClassMap<UserDto>{publicUserMapping(boolisAdmin){if(isAdmin){
Map(u => u.Permission);}else{
Map(u => u.Permissopn).Constant(Permissions.Normal);}}}//This part here is how it is used
csv.Context.UnregisterClassMap();varisAdmin= csv.GetField<string>(nameof(UserDto.IsAdmin));varclassMap=new UserMapping(isAdmin);//here if isAdmin is true for first user then mapping is done correctly, but if the second row is not admin the mapping is initialized correctly but internally the first Map(u => u.Permission); is still used.
csv.Context.RegisterClassMap(classMap);varrecord= csv.GetRecord<UserDto>();
Expected:
Mapping is unregistered correctly.
What I've found to work as workaround:
I thought initially that splitting the UserMapping in two and then deciding which map to use would fix the problem keep in mind that both class maps where for the same UserDto class but it was not the case, somehow it seems that once map is registered for a class it is reused all over again.
What worked was to have basically a new UserDTO2 which inherits UserDTO (basically an empty class) and use that for the second class map and it worked.
Note:
Other then this problem which I think could improve the usage a lot, the library is great and worked for a pretty complex validation scenario.
The text was updated successfully, but these errors were encountered:
This is because the delegate to create the objects is cached with the settings for the original map. I may need to clear those when a mapping for a type is removed, or even added a second time (updated).
For now you can have base class that does everything, and the 2 classes that don't do anything accept pass in their isAdmintrue or false value.
I've just tested this and it s not working, I think because behind the mapping is cached for the UserDto class which is still the same even though the registered class map is different, I've tried it with inheritance and without, basically just having two different classes like:
public class AdminMapping : ClassMap<UserDto>
{
public AdminMapping() { //some mapping here }
}
public class UserMapping : ClassMap<UserDto>
{
public UserMapping() {// different mapping here}
}
I guess for now the only solution that works is this:
public class AdminMapping : ClassMap<UserDto>
{
public AdminMapping() { //some mapping here }
}
public class UserMapping : ClassMap<UserDtoFake>
{
public UserMapping() {// different mapping here}
}
public class UserDtoFake : UserDto { }
But regarding of what workaround we can use, I think the cleanest way will be to clear the cache from behind.
This is not something impactful on our side even though we use this in production we can live with the way it works now so when you have time if it's not something big to do (from the way you've described it looks like it's not) a fix for this will be a great value.
Description:
When having a class map with different mappings depending on some property when the mapping changes registering the class again with the new mapping does not take affect even though in the object the mapping is reinitialized correctly.
Code example:
Expected:
Mapping is unregistered correctly.
What I've found to work as workaround:
I thought initially that splitting the UserMapping in two and then deciding which map to use would fix the problem keep in mind that both class maps where for the same UserDto class but it was not the case, somehow it seems that once map is registered for a class it is reused all over again.
What worked was to have basically a new UserDTO2 which inherits UserDTO (basically an empty class) and use that for the second class map and it worked.
Note:
Other then this problem which I think could improve the usage a lot, the library is great and worked for a pretty complex validation scenario.
The text was updated successfully, but these errors were encountered: