deploy: 78fa831aec
This commit is contained in:
parent
52fa2ea99a
commit
35e175dacf
208 changed files with 416 additions and 416 deletions
|
|
@ -1,4 +1,4 @@
|
|||
<!doctype html><html lang=en class=no-js><head><meta charset=utf-8><meta name=viewport content="width=device-width,initial-scale=1,shrink-to-fit=no"><meta name=generator content="Hugo 0.107.0"><meta name=robots content="index, follow"><link rel="shortcut icon" href=/favicons/favicon.ico><link rel=apple-touch-icon href=/favicons/apple-touch-icon-180x180.png sizes=180x180><link rel=icon type=image/png href=/favicons/favicon-16x16.png sizes=16x16><link rel=icon type=image/png href=/favicons/favicon-32x32.png sizes=32x32><link rel=icon type=image/png href=/favicons/android-36x36.png sizes=36x36><link rel=icon type=image/png href=/favicons/android-48x48.png sizes=48x48><link rel=icon type=image/png href=/favicons/android-72x72.png sizes=72x72><link rel=icon type=image/png href=/favicons/android-96x96.png sizes=96x96><link rel=icon type=image/png href=/favicons/android-144x144.png sizes=144x144><link rel=icon type=image/png href=/favicons/android-192x192.png sizes=192x192><title>Singleton Codeunit | alguidelines.dev - Business Central Design Patterns</title><meta name=description content="By Bogdana Botez at Microsoft Development Center Copenhagen
|
||||
<!doctype html><html lang=en class=no-js><head><meta charset=utf-8><meta name=viewport content="width=device-width,initial-scale=1,shrink-to-fit=no"><meta name=generator content="Hugo 0.110.0"><meta name=robots content="index, follow"><link rel="shortcut icon" href=/favicons/favicon.ico><link rel=apple-touch-icon href=/favicons/apple-touch-icon-180x180.png sizes=180x180><link rel=icon type=image/png href=/favicons/favicon-16x16.png sizes=16x16><link rel=icon type=image/png href=/favicons/favicon-32x32.png sizes=32x32><link rel=icon type=image/png href=/favicons/android-36x36.png sizes=36x36><link rel=icon type=image/png href=/favicons/android-48x48.png sizes=48x48><link rel=icon type=image/png href=/favicons/android-72x72.png sizes=72x72><link rel=icon type=image/png href=/favicons/android-96x96.png sizes=96x96><link rel=icon type=image/png href=/favicons/android-144x144.png sizes=144x144><link rel=icon type=image/png href=/favicons/android-192x192.png sizes=192x192><title>Singleton Codeunit | alguidelines.dev - Business Central Design Patterns</title><meta name=description content="By Bogdana Botez at Microsoft Development Center Copenhagen
|
||||
|
||||
Problem: In some situations, global state needs to be preserved at runtime throughout a …"><meta property="og:title" content="Singleton Codeunit"><meta property="og:description" content="By Bogdana Botez at Microsoft Development Center Copenhagen
|
||||
Problem: In some situations, global state needs to be preserved at runtime throughout a session.
|
||||
|
|
@ -41,7 +41,7 @@ The debugger needs to remember the session which is being debugged The permissio
|
|||
</span></span></span><span style=display:flex><span><span style=color:#f8f8f8;text-decoration:underline> </span>PermissionManager<span style=color:#ce5c00;font-weight:700>.</span>SetTestabilitySoftwareAsAService<span style=color:#ce5c00;font-weight:700>(</span>FALSE<span style=color:#ce5c00;font-weight:700>)</span><span style=color:#000;font-weight:700>;</span><span style=color:#f8f8f8;text-decoration:underline>
|
||||
</span></span></span><span style=display:flex><span><span style=color:#f8f8f8;text-decoration:underline></span><span style=color:#204a87;font-weight:700>END</span><span style=color:#000;font-weight:700>;</span><span style=color:#f8f8f8;text-decoration:underline>
|
||||
</span></span></span></code></pre></div><p>The figure below explains what happens when the Permission Manager is not a singleton. When it is invoked from different places (first from the test, second from the production code), then different instances of the Permission Manager will fire up and answer. In detail:</p><ol><li>The test calls Initialize which sets SaaS=TRUE in codeunit Permission Manager</li><li>The test calls into production code to validate it works as expected. It calls AzureMachineLearningUsage codeunit to find out if the monthly quota has been reached. The function IsAzureMLLimitReached in AzureMachineLearningUsage codeunit is designed only for SaaS. If the code doesn’t run in SaaS, then it always returns FALSE.</li><li>Therefore, a call to PermissionManager is made, to find out if the environment is SaaS.</li><li>However, a different instance of Permission Manager is reached – and instance where SaaS was never set to TRUE. This is a mistake – the test intended to simulate SaaS, but the state it set in the beginning is not reachable from production code.</li><li>The production code will assess (wrongly) that it’s not running SaaS, and say that the Azure ML limit has not been reached (incorrect – and the test fails).</li></ol><p><a href=Singleton-Codeunit-_2D00_-example-_2D00_-bad.PNG><img src=Singleton-Codeunit-_2D00_-example-_2D00_-bad.PNG alt=" "></a></p><p><strong>Solution:</strong> restrict the number of instantiations of a codeunit to only one, by setting the codeunit property <strong>SingleInstance</strong> to <strong>Yes</strong>.</p><p>Returning to the previous example, let’s analyze the case when the codeunit Permission Manager is a singleton codeunit:</p><p><a href=2313.Singleton-Codeunit-_2D00_-CSIDE-SingleInstance-property.PNG><img src=2313.Singleton-Codeunit-_2D00_-CSIDE-SingleInstance-property.PNG alt=" "></a></p><p>When the codeunit Permission Manager is a singleton, then no matter from where it is invoked, the same instance will be reached. Therefore, the status set by the test (SaaS = TRUE) will be reachable from the production code, and the test will pass, as seen in the figure below.</p><p><a href=Singleton-Codeunit-_2D00_-example-_2D00_-good.PNG><img src=Singleton-Codeunit-_2D00_-example-_2D00_-good.PNG alt=" "></a></p><p><strong>Consequences</strong></p><p>1. Use Singleton Codeunit with care and only when there is no other solution. Preserving a global state could often enough be more harmful than useful. One risk is that tests might fail apparently non-deterministically.</p><p>For example, a problem we have met in the development team for Madeira release, was that the singleton codeunit function PermissionManager.SetSoftwareAsAService(TRUE) is often used to emulate and test SaaS conditions. However, if a test ‘forgets’ to reset the state to default (FALSE), then another codeunit which is not supposed to emulate SaaS, will suddenly run as SaaS and will fail. Even if the test has code that resets the state to FALSE, this code might never be reached because of an earlier failure or other error in the test which would stop execution.</p><p>2. The singleton codeunit is only “alive” for the current session. If the user logs out, the old session is closed and the singleton cleared out so any values stored in the old session’s singleton will be lost when the session was closed. When the user logs in again, a new session (with a new fresh instance of the singleton) will be created.</p><p><strong>NAV Usages</strong></p><p>Most of the usages in NAV refer to the so-called “management codeunits”. The management codeunits are needed to run, in a centralized way, various modular parts of the application (features), like the CRM integration, Permissions, Workflows etc. Some of the <strong>Singleton Codeunits</strong> in NAV are listed below:</p><ul><li>Codeunit 423 Change Log Management</li><li>Codeunit 1503 Workflow Record Management</li><li>Codeunit 1511 Notification Lifecycle Mgt.</li><li>Codeunit 1629 Office Attachment Manager</li><li>Codeunit 1632 Office Error Engine</li><li>Codeunit 5150 Integration Management</li><li>Codeunit 9002 Permission Manager</li><li>Etc.</li></ul><p><strong>Note:</strong> while the object-oriented <strong>Singleton</strong> pattern can restrict the number of instantiations of the singleton to an integer n > 0, in Dynamics NAV the <strong>Singleton Codeunit</strong> can only have n=1.</p><div class="text-muted mt-5 pt-3 border-top">Last modified February 24, 2022: <a href=https://github.com/microsoft/alguidelines/commit/5ee0436639455a35835c5ce88241d3fad662ee06>Added tags & categories + cleanup (5ee04366)</a>
|
||||
by waldo1001</div></div></main></div></div><footer class="bg-dark py-5 row d-print-none"><div class="container-fluid mx-sm-5"><div class=row><div class="col-6 col-sm-4 text-xs-center order-sm-2"><ul class="list-inline mb-0"><li class="list-inline-item mx-2 h3" data-toggle=tooltip data-placement=top title=GitHub aria-label=GitHub><a class=text-white target=_blank rel=noopener href=https://github.com/microsoft/alguidelines aria-label=GitHub><i class="fab fa-github"></i></a></li><li class="list-inline-item mx-2 h3" data-toggle=tooltip data-placement=top title=Twitter aria-label=Twitter><a class=text-white target=_blank rel=noopener href="https://twitter.com/search?q=%23bcalhelp" aria-label=Twitter><i class="fab fa-twitter"></i></a></li><li class="list-inline-item mx-2 h3" data-toggle=tooltip data-placement=top title=Discord aria-label=Discord><a class=text-white target=_blank rel=noopener href=https://discord.gg/4wbfNv3 aria-label=Discord><i class="fab fa-discord"></i></a></li></ul></div><div class="col-6 col-sm-4 text-right text-xs-center order-sm-3"></div><div class="col-12 col-sm-4 text-center py-2 order-sm-2"><small class=text-white>© 2022 alguidelines.dev Project All Rights Reserved</small></div></div></div></footer></div><script src=https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js integrity=sha384-9/reFTGAW83EW2RDu2S0VKaIzap3H66lZH81PoYlFhbGU+6BZp6G7niu735Sk7lN crossorigin=anonymous></script>
|
||||
by waldo1001</div></div></main></div></div><footer class="bg-dark py-5 row d-print-none"><div class="container-fluid mx-sm-5"><div class=row><div class="col-6 col-sm-4 text-xs-center order-sm-2"><ul class="list-inline mb-0"><li class="list-inline-item mx-2 h3" data-toggle=tooltip data-placement=top title=GitHub aria-label=GitHub><a class=text-white target=_blank rel=noopener href=https://github.com/microsoft/alguidelines aria-label=GitHub><i class="fab fa-github"></i></a></li><li class="list-inline-item mx-2 h3" data-toggle=tooltip data-placement=top title=Twitter aria-label=Twitter><a class=text-white target=_blank rel=noopener href="https://twitter.com/search?q=%23bcalhelp" aria-label=Twitter><i class="fab fa-twitter"></i></a></li><li class="list-inline-item mx-2 h3" data-toggle=tooltip data-placement=top title=Discord aria-label=Discord><a class=text-white target=_blank rel=noopener href=https://discord.gg/4wbfNv3 aria-label=Discord><i class="fab fa-discord"></i></a></li></ul></div><div class="col-6 col-sm-4 text-right text-xs-center order-sm-3"></div><div class="col-12 col-sm-4 text-center py-2 order-sm-2"><small class=text-white>© 2023 alguidelines.dev Project All Rights Reserved</small></div></div></div></footer></div><script src=https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js integrity=sha384-9/reFTGAW83EW2RDu2S0VKaIzap3H66lZH81PoYlFhbGU+6BZp6G7niu735Sk7lN crossorigin=anonymous></script>
|
||||
<script src=https://cdn.jsdelivr.net/npm/bootstrap@4.6.1/dist/js/bootstrap.min.js integrity="sha512-UR25UO94eTnCVwjbXozyeVd6ZqpaAE9naiEUBK/A+QDbfSTQFhPGj5lOR6d8tsgbBk84Ggb5A3EkjsOgPRPcKA==" crossorigin=anonymous></script>
|
||||
<script src=https://cdn.jsdelivr.net/npm/mermaid@8.13.4/dist/mermaid.min.js integrity="sha512-JERecFUBbsm75UpkVheAuDOE8NdHjQBrPACfEQYPwvPG+fjgCpHAz1Jw2ci9EXmd3DdfiWth3O3CQvcfEg8gsA==" crossorigin=anonymous></script>
|
||||
<script src=/js/tabpane-persist.js></script>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue