-
-
Notifications
You must be signed in to change notification settings - Fork 125
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for mapping base type when using MapDerivedType #692
Comments
I don't fully get what you are requested... Could you provide an example how the configuration and how the generated could would look like to address your needs? |
Sure :) Example: [Mapper]
public partial class Mapper
{
[MapDerivedType<DerivedDto, Derived>]
public partial Base Map(BaseDto source);
}
public class Base
{
public int Number { get; set; }
}
public class BaseDto
{
public int Number { get; set; }
}
public class Derived : Base
{
public string? Name { get; set; }
}
public class DerivedDto : BaseDto
{
public string? Name { get; set; }
} This is the generated code: public partial global::Base Map(global::BaseDto source)
{
return source switch
{
global::DerivedDto x => MapToDerived(x),
_ => throw new System.ArgumentException($"Cannot map {source.GetType()} to Base as there is no known derived type mapping", nameof(source)),
};
} I want a option to generate this code: public partial global::Base Map(global::BaseDto source)
{
return source switch
{
global::DerivedDto x => MapToDerived(x),
_ => MapToBase(source)
};
} So it just maps it to the base class if instead of throwing an exception. And there could also be an option that, instead of always defaulting to the base class, it will throw an exception if |
Ah got it 😊 Proposed APIpublic sealed class MapperAttribute : Attribute
{
+ public DerivedTypeMappingFallbackStrategy DerivedTypeMappingsFallback { get; set; } = DerivedTypeFallbackStrategy.Throw;
}
+[AttributeUsage(AttributeTargets.Method)]
+public sealed class MapDerivedTypes : Attriubte
+{
+ public DerivedTypeMappingFallbackStrategy FallbackStrategy { get; set; } = DerivedTypeFallbackStrategy.Throw;
+}
+public enum DerivedTypeMappingFallbackStrategy
+{
+ Throw,
+ MapBaseType,
+} Feel free to comment on the proposed API. |
Maybe there could also be an option to generate something like this: public partial global::Base Map(global::BaseDto source)
{
return source switch
{
global::DerivedDto x => MapToDerived(x),
global::BaseDto x when !typeof(BaseDto).IsSubclassOf(source.GetType()) => MapToBase(x),
_ => throw new System.ArgumentException($"Cannot map {source.GetType()} to Base as there is no known derived type mapping", nameof(source))
};
} And then maybe a enum like this: public enum DerivedTypeMappingStrategy
{
ExcludeBaseType,
IncludeBaseType,
FallbackToBaseType
} However, it could also just be an attribute: [AttributeUsage(AttributeTargets.Method)]
public class MapBaseTypeAttribute : Attribute
{
public bool FallbackToBaseType { get; set; } = false;
} |
So |
No the exact opposite. Of course the use case for this is still pretty small but just defaulting to the base type might lead to some unexpected behaviours. Edit: An example of unexpected behaviour: you might not want a derived to be mapped or you might expect it to map all the derived types properties if you are not fully familiar with all of the code base (e.g. when collaborating on larger projects). With the |
@simo026q wouldn't then a check in the form |
Yeah true, that would do the same thing |
I can't think of a use-case for which this is useful. Can you elaborate? |
Is your feature request related to a problem? Please describe.
I want to create a mapper that can map to a derived type and the base type. I could not find a way to do this in the documentation.
I tried doing this but it creates a infinite loop:
Describe the solution you'd like
I would like the option to default to the base type, map to the base type if it is the exact type of the base type and not derived from it, and then the option to throw an exception as it is right now.
Something like this enum:
It could be set by a new attribute that should be added.
The text was updated successfully, but these errors were encountered: