Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>While having the singleton accessor property within the class itself is a generally used shortcut, in more complicated situations when you need to deal with instances of multiple related classes it may prove the wrong way. The problem is mixing concerns within a single class.</p> <p>Your <code>MySingleton</code> class shall be rather some <code>MyClass</code> which is implemented in way generally not dependent on the number of instances that will exist on run-time. Likewise <code>MyRepository</code> shall be able to manage and provide any number of <code>MyClass</code> instances. The fact you will be dealing with only a singleton instance of <code>MyClass</code> can be encapsulated into a separate factory (or accessor or whatever you call it) class that will bind <code>MyClass</code> and <code>MyRepository</code> together.</p> <p>So try to rethink your design in the following way (schematic):</p> <pre><code>public class MyClass { ... } public class MyRepository { public MyClass GetMyClassInstance(...); } public static class MyClassFactory { public static MyClass SingletonInstance { get { if (singletonInstance == null) singletonInstance = myRepository.GetMyClassInstance(...); return singletonInstance; } } } </code></pre> <p>The remaining question is where does <code>myRepository</code> come from. For example, it can be created on program start-up and provisioned into <code>MyclassFactory</code> as a member field.</p> <p>At the end of the day, a lot can be made easier if you consider using an inversion of control container. The next step is also shift towards interface-based design. That gives you another layer of abstraction and provides more flexibility in terms of interchangeability of implementations.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload