Hi there AppWorks fans,
Welcome to a new installment of AppWorks tips.
And a good Monday morning on this sunny day (at my place!). A great start to play with a design pattern called ‘Singleton’. Read more about the pattern on this wiki page
Feature description: Make sure only 1 instance of the entity can be created in the solution.
Let get right into it…
Several steps need to be done to make this all happen. Don’t worry…This post will give you the steps required to accomplish it. First spin up your VM, open your workspace with your beloved project where you have the control as the ‘Developer’ role.
Create a ‘config’ entity and update the entity properties
Easy peasy…Enter the ‘entities’ folder of the project and just create a new entity with the name ‘config’. After this creation update the entity properties like this.
And update the ‘Identity’ building block like this so it’s in line with our other entities.
Time for some properties. Make it as fancy as you like, but for me a ‘Name’ is sufficient.
Create forms (Create/Default)
Also here nothing fancy, but it’s required otherwise we can’t create a new instance and view it!
The ‘Create’ form (don’t forget the capital ‘C’)
And the ‘Default’ form (that includes the ‘Create’ form! and don’t forget the capital ‘D’)
Add a list (All Configurations)
I guess only the screenshot tells you enough…correct!?
Add webservices with an ‘AllConfigs’ operation
Now add also the ‘Web service’ building block to the entity and add an extra operation ‘AllConfigs’ from the ‘Advanced configuration’
Add a Security building block
This is required to expose that ‘AllConfigs’ find operation for our end user.
Now add the ‘Identity User’ role to the list with the green ‘plus’ button
And mark all the options available for now (especially the ‘Use’ for the ‘AllConfigs’ webservice!)
Save, publish, and do the first tests
Now do your cycle of save, publish, and see if we can create a new instance of the ‘config’ entity.
It’s a green light for me…
And for our service endpoint?
Let’s see on this URL: http://192.168.56.107:8080/home/appworks_tips/services/
You see my service has more operations, but that is because I played with it before! You probably need to clean some cache to get rid of them, but that’s not for this post now.
Add a BPM ‘check_singleton’
Now for the BPM! Go to you ‘bpms’ folder in the project and create a new ‘Business Process Model’ with the name ‘check_singleton’.
Set the ‘execution’ mode to ‘Short Lived’ what makes the BPM interactive with the front-end (there is more information about this setting in the previous post!).
Next step is to drag the green ‘Start’ construct on the main panel and insert our ‘AllConfigs’ webservice
Search for ‘AllConfigs’ to find the correct one and add it to the BPM. Also connect it to the ‘Start’ construct.
Next step is to add an ‘Exclusive’ decision with a descriptive name ‘Config instance found?’
And we end with 2 endpoints where our ‘YES’ will give an error like this
With also some more error details…
Back to the properties of our ‘Decision’ split to tell what should happen for both ‘End’ constructs in the ‘Conditions’ tab.
This is the value to copy from if you want
Make sure it’s on the ‘YES’ condition where the ‘NO’ condition is the ‘Default’ fallback scenario when the ‘YES’ condition is not met.
Now save, publish, validate & build, and do a quick debug on your craftsmanship!
You can Search for ‘Debug’ on this site to help your further. Or let me know in the comments if want to have it within this post!?
Start BPM from rule ‘OnInitCheckSingleton’
When all is fine we need to create a new ‘Rule’ building block (of type ‘Event’) with the name of ‘OnInitCheckSingleton’ where we configure the rule to start our BPM.
Deploy, save and retest
Now we can save and deploy our entity…and…check the result in runtime!
Make sure to delete the old created instances of the ‘Config’ entity so you get an error the second time you create a new instance!
And that’s it! 💪
Extend the feature (singleton 2.0?)
I nice extra feature I thought of during the creation of this blog is adding an extra property called ‘conf_is_singleton’. This makes the entity a bit more reusable for more configuration instances in the solution (but it also breaks the ‘real’ singleton principle). And you also need to add more complexity in the related BPM as you need to check if the instance ‘name’ already exists and if the ‘Is singleton’ mark is set to TRUE. As always, this all depends on what you want to do…
Again, a greatly earned ‘DONE’ for our singleton implementation on our entity creation. Is there more to say? Well, what about all the other design patterns that our available for you to explore! Curious? Have a read session at the Gang of Four. I see U in the next post on a brand-new installment of AppWorks Tips…Cheers!
Don’t forget to subscribe to get updates on the activities happening on this site. Have you noticed the quiz where you find out if you are also “The AppWorks guy”?