elsa-workflows / elsa-core

A .NET workflows library
https://v3.elsaworkflows.io/
MIT License
6.25k stars 1.14k forks source link

Create activity properties in runtime. #874

Closed tomy2105 closed 3 years ago

tomy2105 commented 3 years ago

My use case is that at initialization I need to discover number of properties and property types for an activity.

Since properties, at least at first glance, seem to be tied to activity class members this type of dynamic adding seems impossible or would require dynamic creation of the whole class or something equally complex.

Is something like this possible? Did I miss some relatively easy way of doing this?

One idea that came to my mind, would it be possible to have several activity properties (as displayed in designer) map to one activity member variable (map, array, complex object with submemeber)? One property going to member.first or member["first"] and other going to member.second or member["second"]?

sfmskywalker commented 3 years ago

You should be able to do it today with relative ease. The key ingredient for this is the IActivityTypeProvider abstraction. When you implement this interface (and register it with DI), Elsa will use it to discover activities.

As part of this implementation, you construct a thing called an ActivityType, which contains a description of the activity. This description includes a list of activity properties that you can set.

Elsa contains an implementation of IActivityTypeProvider that uses the activity classes to dynamically construct these activity types. As you can see, activity classes are merely a "source" of material to generate activity types from. You're free to add more activity type providers. Two other such examples are the NotificationActivityTypeProvider (from the Telnyx activity library that yields activity types based on Telnyx webhook payload classes) and WebhookActivityTypeProvider, which dynamiclly yield activity types based on webhook definitions stored in the database.

We'll have more activity providers in the future, such as one that use Open API and GraphQL schemas to dynamically yield activity types that represent various API actions.