I’m developing a system with multiple presets classes and I built a DynamicEnum to recall em easily per category. Everything is quite nice except I got a weird issue that feels like a bug. Basically my classes register in the DynamicEnum via the RegisterServices operation but this operation is not called until i navigate to the page where the operation resides. I would expect the RegisterServices to execute on patch opening, not just when i open the specific page.
Before is how DynamicEnums looj by just opening the patch, before navigating to the Definitions page.
After is how these look after just navigating to the Definitions page.
Definitions is how the page looks for now.
(I combined all into one image since the forum doesn’t let me upload multiple media)
Ideally I’d put the registration in the RegisterServices operation inside each preset class so each one automatically register itself but that would mean to manually open each class definition and that’s why I opted for the Definition page itself for the moment.
Is this a bug or intended behaviour? Do you suggest me to handle this differently so that I can register enums automatically at patch opening?
You see the problem here, is that you need something to get evaluated to actually register, e.g. you need instance created, while it’s in definitions it can kind of initialize and register everything in advance.
Let’s see, for instance, lazy initializable, e.g. register service upon request:
private ISomething? _something;
public ISomething Something
{
get
{
if (_something == null)
{
var something = new Something();
AppHost.Current.Services.Register<ISomething>(something);
_something = something;
}
return _something;
}
}
But, your actual problem is that you have a dynamic enum that has to form it list of entries before they are actually exist, so lazy initialization won’t work meaning that your current approach is correct: “You have to register everything in advance if you want it as list”
Prolly there is hybrid aproach avalible also, you register list of types, then you on each type you have lazy initializer, your enumb built from list of types… and so on…
Honestly I would stick with what you have, unless there are other explicit requirements. And if you want to change likely you don’t want to use dynamic enum.
@aptrn why are you using an Enum exactly ?
It looks like you are trying to make a factory but I’m wondering if the enum is for selecting the type from the Gamma Editor only.
@lecloneur
Yes, it’s just to select presets from the Gamma Editor atm
@antokhio
yep that’s what i imagined but expected RegisterServices to run anyway without visiting the definition page somehow
My issue atm is just that I have to explicitly navigate there to make the list correctly appear
I opted for the enum because these class are now a small set but they’ll grow quite a lot and thought that was the best way to have em easily recallable without typing exact name for each of them, allowing exploring various presets just by switching in the dropdown, basically userfriendlyness
thank you @antokhio, i’ll try this in a test environment asap
BTW, for NO REASON, it actually started working as intended without opening the Definition page.
Don’t ask me why but while it works i won’t touch it lol