optimajet / WorkflowEngine.NET

WorkflowEngine.NET - component that adds workflow in your application. It can be fully integrated into your application, or be in the form of a specific service (such as a web service).
https://workflowengine.io
892 stars 249 forks source link

How to create a default Parameter #123

Open rjps12 opened 2 years ago

rjps12 commented 2 years ago

Hi,

I was wondering how to create a default Parameter that will be displayed in the Parameter settings in the workflow designer?

I am looking at the workflow runtime and I wanna know if there is some ways like how the actors are being populated.

Jerac102 commented 9 months ago

I would also like to know this. Can we get a example of implementation of IDynamicParameterCompatible? Thank You

rylee-soll commented 9 months ago

I would also like to know this. Can we get a example of implementation of IDynamicParameterCompatible? Thank You

First, I'd like to clarify the context and specifics of using IDynamicParameterCompatible implementations. This interface is used within IWorkflowExternalParametersProvider to transfer the values of the parameters themselves. This is just one of the ways you can pass objects into the Workflow Engine. You can find more details in the documentation.

Most often, DynamicParameter is used to transfer objects, but it can only work with whole objects and your data will first be converted to JSON and then used. This is a downside, which is absent when using a Dictionary. Although you could simply implement Get and Set methods for the Dictionary parameter in External Parameters Provider, you'd probably want to encapsulate this logic in a separate object. This is exactly what IDynamicParameterCompatible is designed for.

By implementing this interface, you can build any logic for reading fields from the business object you're interested in. For example, you can connect your service, repository, Entity Framework, or directly read values from your database using a reader. There can be many implementations, but I've written a simple example for you with a repository.

public class MyParameterCompatibleWithDynamicObject : IDynamicParameterCompatible
{
    public MyParameterCompatibleWithDynamicObject(MyRepository repository)
    {
        _repository = repository;
    }

    public IDictionary<string, object> GetPropertiesAsDictionary()
    {
        IDictionary<string, object> result;

        var dto = _repository.GetDto();

        result = new Dictionary<string, object>
        {
            {"FieldA", dto.FieldA},
            {"FieldB", dto.FieldB},
            {"FieldC", dto.FieldC}
        };

        // or

        result = new Dictionary<string, object>
        {
            {"FieldA", _repository.GetField("FieldA")},
            {"FieldB", _repository.GetField("FieldB")},
            {"FieldC", _repository.GetField("FieldC")}
        };

        return result;
    }

    public void SetPropertiesFromDictionary(IDictionary<string, object> properties)
    {
        var dto = _repository.GetDto();

        dto.FieldA = (string)properties["FieldA"];
        dto.FieldB = (int)properties["FieldB"];
        dto.FieldC = (DateTime)properties["FieldC"];

        _repository.SetDto(dto);

        // or

        _repository.SetField("FieldA", properties["FieldA"]);
        _repository.SetField("FieldB", properties["FieldB"]);
        _repository.SetField("FieldC", properties["FieldC"]);
    }

    private readonly MyRepository _repository;
}

I hope this information helps you better understand how to structure the process of working with your data through the Workflow Engine.