Skip to content
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

Fix issue #524. Adding support for working with Types packed into an object to the standard adapter #645

Open
wants to merge 1 commit into
base: development
Choose a base branch
from

Conversation

DocSvartz
Copy link

@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
Author

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

@DocSvartz
Copy link
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
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

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.

1 participant