Self registering Classes

Viewed 1773

I am currently starting developing an Alexa Skill in C#. As I did not want to have a messy if block checking the intent type of an incoming request I got the following idea: Writing a static class RequestHandler which stores a list of all possible intents (List of IIntent interface which will be implemented by classes which represent an intent).

I know have the conceptional problem, that I do not know what the best way to register those intents might be. My first idea was to just write

internal static void RegisterIntent(IIntent intent) => Intents.Add(intent);

and call this in static constructors of each intent. At this point I didn´t know static constructors only will be executed the first time the class will be accessed. So this idea is not working as I thought. The other ideas I for know can think of, are:

  • adding a static Initialize / Register method to each intent class in which it creates an instance which will be added into the static intent list --> this will result in a lot of initiliaze methods I would have to add manually to the executed lambda function.
  • adding an Initialize method to the static RequestHandler which will add an instance of each intent class to the static list --> I still would have to manually write the RegisterIntent(new ABCIntent()) command for each intent class.

Is there a way to get all intent classes be added into the static list without adding code for this register action manually? Or are there better ways to achieve this than those two ones I had so far? Maybe with reflection (tbh: I am quite new to AWS lamda development and I do not know if this will work).

1 Answers
Related