1 /**                                                                                                                                                  
2   Copyright: 2017 © LLC CERERIS                                                                                                                      
3   License: MIT                                                                                                                               
4   Authors: LLC CERERIS                                                                                                                               
5 */
6 
7 module dich.reuse;
8 
9 import dich.provider;
10 
11 /// Interface for reuse object
12 interface ReuseInterface
13 {
14   public:
15     /** Get reuse object
16 
17       Params:
18         key = Reuse object name
19         provider = Object provider
20 
21       Returns:
22         Reuse $(D Object)
23     */
24     Object get(string key, ProviderInterface provider);
25 }
26 
27 /** Transient reuse object
28   Returns the object directly
29 */
30 class Transient: ReuseInterface
31 {
32   public:
33     /** Get transient reuse component 
34 
35       Params:
36         key = Not used parameter
37         provider = Object provider
38 
39       Returns:
40         Reuse $(D Object)
41     */
42     override Object get(string key, ProviderInterface provider)
43     {
44       return provider.get;
45     }
46 }
47 
48 /** Singleton reuse object
49   Guarantees that there is only one object for the given object name
50 */
51 class Singleton: ReuseInterface
52 {
53   public:
54     /** Get singleton reuse component 
55 
56       Params:
57         key = Unique name for Singleton
58         provider = Object provider
59 
60       Returns:
61         Reuse object
62     */
63     override Object get(string key, ProviderInterface provider)
64     {
65       if(key !in _instances)
66       {
67          _instances[key] = provider.get;
68       }
69 
70       return _instances[key];
71     }
72 
73   private:
74     /// Object instance array for all singleton objects
75     Object[string] _instances;
76 }