Skip to content

Conversation

@DocSvartz
Copy link
Contributor

@DocSvartz DocSvartz commented Oct 18, 2023

Fix issue #524

Before:

if Type was packaged into an object:

object _source = new TSource()

Instead of updating with data from TSource, it was converted to the TDestination type

_source.Adapt(_destination) == _source.Adapt<TDistination>

@DocSvartz DocSvartz changed the title Fix #524 - Added support Update function TDistination in Object Adapter Fix issue #524 - Added support Update function TDistination in Object Adapter Oct 19, 2023
@DocSvartz DocSvartz changed the title Fix issue #524 - Added support Update function TDistination in Object Adapter Fix issue #524 - Added support Update function TDestination in Object Adapter Oct 20, 2023
@DocSvartz DocSvartz changed the title Fix issue #524 - Added support Update function TDestination in Object Adapter Added support Update function TDestination in Object Adapter Oct 20, 2023
@DocSvartz DocSvartz changed the title Added support Update function TDestination in Object Adapter Fix issue #524. Adding support for working with Types packed into an object to the standard adapter Oct 20, 2023
@DocSvartz
Copy link
Contributor Author

Seems like, It was simply necessary to apply an already developed solution. By adding it to a standard adapter. :)

@DocSvartz
Copy link
Contributor Author

DocSvartz commented Oct 21, 2023

Hello @andrerav
It looks like the simplest and most correct solution would actually be to use the mechanism from the special overload of the Adapt Method.

  1. In general, the Expression for an adapt is created based on the types of generic arguments.
    With this call, generation will always occur for the object type as a TSource or TDestination. Since the generic method was called for them: Adapt<object,TDestination>(), Adapt<object,object>(), Adapt<TSource ,object> ( but not Adapt<TSource,TDestination>().
  2. The received conversion Adapt function is called with Runtime arguments (variables) passed for updating.
    But since the Adapt function has already been generated for the Object type, processing for it will occur as an Object.

Therefore, this is not exactly an ObjectAdapter problem, as I initially decided.
You just need to initially generate the Adapt function for the Type Packed into an object ( TSource or TDestination )

  1. Even to call a function dynamically, need to know the real type of the TSource before calling them.
    Without this cannot create this:
var method = (from m in typeof(TypeAdapterConfig).GetMethods(BindingFlags.Instance | BindingFlags.Public)
    where m.Name == nameof(GetMapToTargetFunction)
    select m).First().MakeGenericMethod(sourceType, destinationType);

The main problem is the following:
For this to work, need to capture the Runtime type of variables _source.GetType() _destination.GetType() somewhere and save them for subsequent construction of the Adapt function (Theory, maybe this won't work either).

@DocSvartz
Copy link
Contributor Author

DocSvartz commented Oct 21, 2023

@andrerav It looks like now the work on the fix is really complete.

Update:
I left the endless loop breaker in both places, for greater reliability - in ObjectAdapter this not required

@andrerav
Copy link
Contributor

andrerav commented Jan 2, 2025

@DocSvartz Could you please resolve this merge conflict when you have time? Looks like it should be fairly trivial :)

@andrerav andrerav changed the title Fix issue #524. Adding support for working with Types packed into an object to the standard adapter Fix issue #524 - Add support for working with types packed into an object to the standard adapter Jan 3, 2025
@andrerav andrerav merged commit 3bf9e6b into MapsterMapper:development Jan 3, 2025
1 check passed
@andrerav
Copy link
Contributor

andrerav commented Jan 3, 2025

Thank you @DocSvartz!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants