Custom Flags
It is possible to define custom flags with custom priorities by injecting them during the creation of the ITsm object. The following code example can also be found in the example files shipped with the PowerTSM .NET API installation package, in project 3_Advanced_Features → Program.cs → UsingCustomFlags().
Custom Flags
Section titled “Custom Flags”For injecting the custom flags during the creation of the ITsm object use the UseCustomFlag method. The following code shows how to:
- inject custom flags
- overwrite the priority of an existing flag using the flag value
- overwrite the priority of an existing flag using the flag
- instantiate a custom flag
- compare priorities
- read priorities
var tsm = ITsm.Create((container) =>{ container // configure the ITsm instance to create a new custom flag with a value of 11 and a // priority of 35. .UseCustomFlag(11, 35, "CustomFlag") // configure the ITsm instance to treat flags with a value of 12 as having a // priority of 77. .UseCustomFlag(12, 77, "CustomFlag2") // configure the ITsm instance to treat flags with a value of 19 ( = Missing) as having a // priority of 5. .UseCustomFlag(Flag.Missing(), 5, "Missing");}, true /*<- this makes sure all other defaults are still configured.*/);
var validFlag = Flag.Valid(); // "valid" - by default - has a priority of 30
// Instantiating a flag with a custom value:var customFlag = new Flag(11);
// Returns a flag with a value of 11 ( = customFlag), since 35 is higher than 30tsm.FlagPriorities.HighestPriorityFlag(validFlag, customFlag);
// Returns a flag with a value of 9 ( = customFlag), since 30 is lower than 77tsm.FlagPriorities.LowestPriorityFlag(validFlag, new Flag(12));
// Returns 77tsm.FlagPriorities.GetPriority(Flag.Scheduled());
// Returns 5tsm.FlagPriorities.GetPriority(Flag.Missing());
// Returns 35tsm.FlagPriorities.GetPriority(customFlag);
// Returns "Valid"tsm.FlagNames.GetName(validFlag);
// Returns "CustomFlag"tsm.FlagNames.GetName(customFlag);
// Returns "CustomFlag2"tsm.FlagNames.GetName(new Flag(12));
// Returns "Missing"tsm.FlagNames.GetName(Flag.Missing());Injecting a Custom Flag
Section titled “Injecting a Custom Flag”To inject a new custom flag inject a custom flag with a non existing value during the creation of the ITsm object. The following code injects a custom flag with the value 11, priority 35 and name “MyCustomFlag”.
var tsm = ITsm.Create((container) =>{ container .UseCustomFlag(11, 35, "MyCustomFlag");}, true);Overwriting the Priority of an Existing Flag
Section titled “Overwriting the Priority of an Existing Flag”To map a custom priority to an existing default flag, inject a custom flag with either the value or the flag object of the flag to be overwritten during the creation of the ITsm object. The following code maps a new priority 77 to the flag value 12 (“Scheduled”) and a new priority 5 to the flag value 19 (“Missing”). The mapping of the new name “CustomFlag2” to the flag value 12 has no effect. It will continue to be resolved with the name (“Scheduled”).
var tsm = ITsm.Create((container) =>{ container .UseCustomFlag(12, 77, "CustomFlag2") .UseCustomFlag(Flag.Missing(), 5, "Missing");}, true);Instantiating a Custom Flag
Section titled “Instantiating a Custom Flag”To instantiate a custom flag just call the Flag(int value) constructor. A custom flag can be created even without injecting it in the ITsm object but the mapping to the priority and name will then be missing.
var customFlag = new Flag(11);Priorities
Section titled “Priorities”With the implementation of the IFlagPriorityResolver the mapped priority to a flag can be read or comparisons between flags can be performed.
Read and compare flag priorities
Section titled “ Read and compare flag priorities”// Returns a flag with a value of 11 ( = customFlag), since 35 is higher than 30tsm.FlagPriorities.HighestPriorityFlag(validFlag, customFlag);
// Returns a flag with a value of 9 ( = customFlag), since 30 is lower than 77tsm.FlagPriorities.LowestPriorityFlag(validFlag, new Flag(12));
// Returns 77tsm.FlagPriorities.GetPriority(Flag.Scheduled());With the implementation of the IFlagPriorityResolver the name of a flag can be read.
Read the name of a flag
Section titled “ Read the name of a flag”// Returns "CustomFlag"tsm.FlagNames.GetName(customFlag);
// Returns "CustomFlag2"tsm.FlagNames.GetName(new Flag(12));
// Returns "Missing"tsm.FlagNames.GetName(Flag.Missing());