PetaPoco icon indicating copy to clipboard operation
PetaPoco copied to clipboard

Support Xamarin under iOS by providing a factory not using IL / Reflection.Emit

Open 6pac opened this issue 5 years ago • 10 comments

There are some limitations on use of Reflection under iOS, which affects Xamarin Projects:

https://docs.microsoft.com/en-us/xamarin/ios/internals/limitations

Essentially, Reflection.Emit and the use of IL is not supported. This is used only in specific sections of code anyway, basically GetFactory() and the MultiPocoFactory.

I have been using PetaPoco with SQLite for my Xamarin projects (I've only delivered UWP so far, which has no issue with the full PetaPoco).
Has anyone taken this journey before? It shouldn't be too hard to rewrite the IL into regular C# code using reflection ... I think!
I'm assuming that the IL is just for speed rather than flexibility.

Note I have gone back to PetaPoco v4, v3 and v2 but they all still use the IL loop for the factory.

BTW, love the recent work, v6 has some really nice features. I giggled a bit at the SQLKata plugin though, the interface is almost identical to the SubSonic v2 interface from all those years ago, which was dropped by Brad Robinson, due to its inflexibility in complex situations, in favour of the more flexible PetaPoco SQL generator. The wheel has come full circle!

6pac avatar Jan 06 '20 00:01 6pac

The IL gen stuff is a PITA. It's not needed anymore, and was originally added as a pref optimisation.

If you had the time, you could port this PR (https://github.com/CollaboratingPlatypus/PetaPoco/pull/149/files) the the latest version of PP 😀😁 and raise a new PR

pleb avatar Jan 06 '20 00:01 pleb

Thanks! I was about to try to do that myself from scratch, so this is a great help. I get to test it on iOS too which should be a pretty unforgiving test environment!

6pac avatar Jan 06 '20 00:01 6pac

Erm, OK, that PR is actually not much help. It mainly consists of changing object types to Delegate. It does eliminate a small amount of Reflection.Emit code, but doesn't replace it with anything.

Not being familiar with IL (though I was a 6502 assembler gun back in the 80's, which certainly helps), I am struggling to translate what is there, even after reading up on IL. Mostly I'm just confused as to why IL was ever used, and I'm not really familiar with this part of the code - I've never used the converters/mappers other than in default mode.

Does there happen to be some code in the archive that pre-dates the Emit 'optimisations'? I've gone back to PetaPoco 1 and even had a look at the Massive and Subsonic 2 and 3 code, but there doesn't seem to be an equivalent.

6pac avatar Jan 07 '20 01:01 6pac

Ah, sorry. I was under the assumption that the PR replaced all IL code gen

The IL stuff is simply creating factories per poco type, which create pocos from a db reader row (high level overview). The use case is performance. The alternate method would be to use reflection to create pocos, which may be slower

pleb avatar Jan 07 '20 02:01 pleb

OK, so it's meant to be a bit like pre-compiling a RegExp search. Hoping I can just translate it back into code, but it might take me a while. Meanwhile, I think SubSonic 2 is probably able to be run in the environment I want, and I still use that for some of my really old projects.

6pac avatar Jan 07 '20 02:01 6pac

There's tools like Dotpeek that would be able to decompile the IL code. The issue is as this code is dynamically generated at runtime there's no easy way to get access to it, that I know of

pleb avatar Jan 07 '20 03:01 pleb

I have got this basically working. Since it's such a core part of PP, I'll keep testing this for another month, I think. It is apparent that the use of IL would speed things up, possibly significantly, so it's worth making the non-IL factory an option rather than replacing the existing. iOS doesn't seem to have any problem compiling and deploying the Emit code, it just chokes at runtime. So that's a bonus, we can probably leave what's there and provide an 'iOS compatibility' option.

6pac avatar Jan 10 '20 00:01 6pac

I may take a stab at this when I get some free time. Would the feature branch be created from development or v7 @pleb?

Ste1io avatar Aug 09 '23 09:08 Ste1io

Most of the work was done here but I haven't had the time to find the bug and integrate the code properly. I'm still around though, and this code has been running in my Xamarin codebase for several years, albeit on a much older version of PetaPoco.

6pac avatar Aug 09 '23 11:08 6pac

Good to see you around @6pac, thx for the reply. I'll take a look at that PR this coming weekend (🤞🏻).

Ste1io avatar Aug 09 '23 11:08 Ste1io