Skip to content
DataMiner DoJo

More results...

Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors
Search in posts
Search in pages
Search in posts
Search in pages
Log in
Menu
  • Updates & Insights
  • Questions
  • Learning
    • E-learning Courses
    • Empower Replay: Limited Edition
    • Tutorials
    • Open Classroom Training
    • Certification
      • DataMiner Fundamentals
      • DataMiner Configurator
      • DataMiner Automation
      • Scripts & Connectors Developer: HTTP Basics
      • Scripts & Connectors Developer: SNMP Basics
      • Visual Overview – Level 1
      • Verify a certificate
    • Video Library
    • Books We Like
    • >> Go to DataMiner Docs
  • Expert Center
    • Solutions & Use Cases
      • Solutions
      • Use Case Library
    • Markets & Industries
      • Media production
      • Government & defense
      • Content distribution
      • Service providers
      • Partners
      • OSS/BSS
    • Agile
      • Agile Webspace
      • Everything Agile
        • The Agile Manifesto
        • Best Practices
        • Retro Recipes
      • Methodologies
        • The Scrum Framework
        • Kanban
        • Extreme Programming
      • Roles
        • The Product Owner
        • The Agile Coach
        • The Quality & UX Coach (QX)
    • DataMiner DevOps Professional Program
      • About the DevOps Program
      • DataMiner DevOps Support
  • Downloads
  • More
    • DataMiner Releases & Updates
    • Feature Suggestions
    • Climb the leaderboard!
    • Swag Shop
    • Contact
    • Global Feedback Survey
  • PARTNERS
    • All Partners
    • Technology Partners
    • Strategic Partner Program
    • Deal Registration
  • >> Go to dataminer.services

How to create a Singleton on element level?

Solved1.36K views20th October 2023QActions Singleton static
2
Edib Šupić [SLC] [DevOps Catalyst]2.10K 20th October 2023 0 Comments

I need to create an object that will represent the state of the table, and I want to be able to use that same object from other QActions, however my object needs to be declared on an element level, meaning each element of that protocol will have it’s own single instance(singleton). I cannot use static as that would mean that all elements running that protocol will share the instance, and their table states may differ. I would like to avoid having to store the state in a dummy parameter within the actual table because that would beat the purpose of storing the state of the table in the object in the first place. Would this even be possible? I’m open to creative solutions as well, even if just as a though experiment.

Edib Šupić [SLC] [DevOps Catalyst] Selected answer as best 20th October 2023

2 Answers

  • Active
  • Voted
  • Newest
  • Oldest
6
Tom Waterbley [SLC] [DevOps Catalyst]8.89K Posted 20th October 2023 1 Comment

Hi Edib, this can be achieved by using a static dictionary, with the element ID as key.

I once created the following class to make this more generic and easy to use: https://gist.github.com/TomW-Skyline/805c689e62f0362d845ced2829a91aa1.

Usage:

var instance = ElementInstanceManager.GetInstance(protocol);

var x = instance.MyData;

However do note that the data will remain in memory, even after restarting the element. To make sure that all data gets cleaned up when the element is being stopped (or deleted), it’s important to call RemoveInstance in the IDisposable.Dispose() method of a QAction:

ElementInstanceManager.RemoveInstance(protocol);

Please let me know if more details are required.

João Severino [SLC] [DevOps Catalyst] Posted new comment 20th October 2023
João Severino [SLC] [DevOps Catalyst] commented 20th October 2023

Small note on the IDisposable.Dispose method, is only available from 10.2.9 onwards
See https://docs.dataminer.services/develop/devguide/Connector/LogicQActions.html#implementing-the-idisposable-interface

Prior to that version, an option could be to subscribe to the element state in order to detect when it gets stopped or deleted.

3
João Severino [SLC] [DevOps Catalyst]13.11K Posted 20th October 2023 5 Comments

Hi,

Could you elaborate a bit more on the reason for needing to have the info in memory?

Is it because the table is changing from different sources? Or too slow to read from within the QAction due to size?

Because having to maintain 2 copies of the same data can present some challenges especially if you start to have to deal with concurrency.

In any case, you could still use a static object as long as you have a way of indexing the data, so something like a static Dictionary where the key is the element ID in the format of <dataminerId>/<elementId> to ensure uniqueness even when dealing with migrated elements.

You would also need to make sure when the element is stopping or gets deleted that you delete the static data entry.

Another alternative would be to have an instanced QAction with a local field/property that would store the value in between executions.
Do note that with this last approach, you would need to have all your entry points in the same QAction

João Severino [SLC] [DevOps Catalyst] Posted new comment 23rd October 2023
Simon Vandamme [SLC] [DevOps Advocate] commented 20th October 2023

For the key of your dictionary, you probably want to use “dmaId/elementId” in order to avoid any possible issues if an element is migrated from one agent to another. This will be even more likely to be needed when swarming gets into play.

João Severino [SLC] [DevOps Catalyst] commented 20th October 2023

Correct, the key should always be the complete unique identifier of the element as you mentioned.
Updated the answer for clarity.

Edib Šupić [SLC] [DevOps Catalyst] commented 20th October 2023

Hi João,
reasoning behind keeping data in memory is to reduce amount of calls to SLProtocol, I’m working on Polling Manager and I would have to load table in to memory often in order to check if something is ready to be polled. There are other approaches to this problem as well but currently I’m exploring this one. Also since I will be working with the data from the table significantly I would like to reap the benefits of OOP.

Floris Cockaerts [SLC] [DevOps Advocate] commented 23rd October 2023

I understand that retaining data in memory can improve performance if the data size remains within reason. But I’m not sure why it should be static? QActions can be made dynamic by removing the static keyword from the class and run method definition.
When using a static dictionary, be careful with threading and perhaps use a ConcurrentDictionary.

João Severino [SLC] [DevOps Catalyst] commented 23rd October 2023

The only reason I see for it being static is to place it in a precompile QAction in order to be able to access it from different QActions as mentioned in the original question.
Otherwise, I would agree that an instanced QAction would be the way to go since it would give some added protection against memory leaks, which if possible would still be my advice to do.
Still, I do not believe there is an easy way of sharing data between QActions without a static object.

Please login to be able to comment or post an answer.

My DevOps rank

DevOps Members get more insights on their profile page.

My user earnings

0 Dojo credits

Spend your credits in our swag shop.

0 Reputation points

Boost your reputation, climb the leaderboard.

Promo banner DataMiner DevOps Professiona Program
DataMiner Integration Studio (DIS)
Empower Katas
Privacy Policy • Terms & Conditions • Contact

© 2025 Skyline Communications. All rights reserved.

DOJO Q&A widget

Can't find what you need?

? Explore the Q&A DataMiner Docs