PDM, Task Add-in, Is anyone else using a [Serializable()] class/struct for task variables?
Posted: Wed May 12, 2021 11:38 am
When I started framing up our PDM Task Add-in (new to PDM) I needed to use the SetValEx and GetValEx for some data pertaining to the task itself. Having a tiny bit of Object Oriented programming background, I started getting the heebie-geebies from calling Set/GetValEx(“variableName”, variableValue) all over the place I had the variable names as constants but there were different data types and a lot of casting I wasn’t liking it. Then when I wanted to store an enumerated type but couldn’t (either needed to just store as string, number or serialize) I restructured a bit.
I created a class “TaskVarEx” to encapsulate all the variables with the Serialize method, the default constructor for initial values, an overriding constructor that takes a string parameter to deserialize and Properties to access all the variables. The variables themselves are each private in the serializable struct “Vars” which is public but only instantiated within the scope of the TaskVarEx class. So all of the variables are stuffed into one task variable, the name of that variable is stored in the static variable:
Getting the variables object looks like this in C#:
Accessing them is as simple as using the Property:
And storing them:
The add in has been running this way for over a year and I have not yet found issues with this method of encapsulating the task variables. There’s been other issues I’ve addressed, but so far this is working. I my mind it makes the code much simpler to read and a bit more inline with object oriented programming. I’ve been curious is anyone else is doing something similar with their custom task add-in.
Thanks.
I created a class “TaskVarEx” to encapsulate all the variables with the Serialize method, the default constructor for initial values, an overriding constructor that takes a string parameter to deserialize and Properties to access all the variables. The variables themselves are each private in the serializable struct “Vars” which is public but only instantiated within the scope of the TaskVarEx class. So all of the variables are stuffed into one task variable, the name of that variable is stored in the static variable:
Code: Select all
public const string VARS_EX_NAME = "VARS";
Code: Select all
IEdmTaskProperties props = (IEdmTaskProperties)poCmd.mpoExtra;
…
TaskVarEx taskVars = new TaskVarEx((string)props.GetValEx(TaskVarEx.VARS_EX_NAME));
Code: Select all
taskVars.TaskType
or
taskVars.ShowMenu
Code: Select all
task = (IEdmTaskInstance)poCmd.mpoExtra;
…
task.SetValEx(TaskVarEx.VARS_EX_NAME, taskVars.Serialize());
The add in has been running this way for over a year and I have not yet found issues with this method of encapsulating the task variables. There’s been other issues I’ve addressed, but so far this is working. I my mind it makes the code much simpler to read and a bit more inline with object oriented programming. I’ve been curious is anyone else is doing something similar with their custom task add-in.
Thanks.