Support generic user implemented mappings
See original GitHub issueAdd support for user implemented generic mapping methods:
class Optional<T> where T : notnull { public T? Value; }
class User { string Id; }
class UserDto { string Id; }
class Document { Optional<User> ModifiedBy; }
class DocumentDto { Optional<UserDto> ModifiedBy; }
[Mapper]
public partial class Mapper
{
public partial DocumentDto MapDocument(Document source);
private Optional<T> MapOptional<TTarget, TSource>(Optional<TSource> source)
{
return new Optional<TTarget> { Value = Map<TTarget>(source.Value) };
}
private partial T Map(object source);
private partial UserDto MapUser(User source);
}
Mapperly should use the user implemented MapOptional
method to map Document.ModifiedBy
to DocumentDto.ModifiedBy
.
Depends on https://github.com/riok/mapperly/issues/357.
Discussed in #444.
Issue Analytics
- State:
- Created 4 months ago
- Reactions:1
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Customize Azure Active Directory attribute mappings in ...
Learn about attribute mappings for Software as a Service (SaaS) apps in Azure Active Directory Application Provisioning.
Read more >Use Service Mappings
Use service mappings to map data from a source to a destination and modify the data. Used with analgorithm service mappings are a...
Read more >Generic and Explicit Object Mappings
Generic mapping, which allows you to store arbitrary objects without special configuration or administration. Explicit mapping, which maps specific objects and ...
Read more >KB5014754—Certificate-based authentication changes on ...
Certificate mappings. Domain administrators can manually map certificates to a user in Active Directory using the altSecurityIdentities attribute of the users ...
Read more >Role Mapping Providers
Role Mapping Providers. Role mapping is the process whereby principals (users or groups) are dynamically mapped to security roles at runtime.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
https://github.com/riok/mapperly/issues/421 occurred when Mapperly didn’t have any support for generic methods, but still tried to call them. If a generic method was implemented by a user, Mapperly handled the generic type as any other type. Therefore it did type checks with
typeof(TGeneric)
which won’t work outside the method itself, also methods with a generic target type were called like any other method, without explicit type arguments. But if the return type is a type argument, the c# compiler cannot infer the type. The fix to #421 was to just ignore them for now and open this issue to implement support. Example of how Mapperly would currently invoke generic user implemented methods if it has a generic return type:To support this, as you figured out, Mapperly needs to implement generic mapping support during the mapping resolution in
MappingCollection
andUserMethodMappingExtractor.BuildUserImplementedMapping
. Additionally the syntax generation needs to be aware of generics and whether they need to be specified explicitly. Probably a newGenericUserImplementedMethodMapping
is needed or the existingUserImplementedMethodMapping
could be adjusted. In theBuild
method of the generic mapping, theInvocation
needs to be aGenericInvocation
if type arguments need to be specified explicitly.@Fube I think this works for methods where the source is the only generic type argument (would definitely need some cleanup 😄). If the target is (also) generic, not only the lookup side, but also the syntax generation side needs to be adjusted as explicit specification of the generic type arguments is required.