1 /** 2 3 License: 4 Boost Software License - Version 1.0 - August 17th, 2003 5 6 Permission is hereby granted, free of charge, to any person or organization 7 obtaining a copy of the software and accompanying documentation covered by 8 this license (the "Software") to use, reproduce, display, distribute, 9 execute, and transmit the Software, and to prepare derivative works of the 10 Software, and to permit third-parties to whom the Software is furnished to 11 do so, all subject to the following: 12 13 The copyright notices in the Software and this entire statement, including 14 the above license grant, this restriction and the following disclaimer, 15 must be included in all copies of the Software, in whole or in part, and 16 all derivative works of the Software, unless such copies or derivative 17 works are solely in the form of machine-executable object code generated by 18 a source language processor. 19 20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 DEALINGS IN THE SOFTWARE. 27 28 Authors: 29 Alexandru Ermicioi 30 **/ 31 module aermicioi.aedi.container.singleton_container; 32 33 import aermicioi.aedi.container.container; 34 import aermicioi.aedi.storage.object_storage; 35 import aermicioi.aedi.storage.locator_aware; 36 import aermicioi.aedi.storage.locator; 37 import aermicioi.aedi.factory.factory; 38 import aermicioi.aedi.exception; 39 import aermicioi.aedi.container.factory; 40 41 import std.range.interfaces; 42 import std.typecons; 43 44 /** 45 Singleton container. 46 47 It creates objects from ObjectFactory implementations and sets them as long as it lives in application. 48 **/ 49 class SingletonContainer : ConfigurableContainer { 50 51 private { 52 53 ObjectStorage!() singletons; 54 ObjectStorage!(ObjectFactory, string) factories; 55 } 56 57 public { 58 59 this() { 60 this.singletons = new ObjectStorage!(); 61 this.factories = new ObjectStorage!(ObjectFactory, string); 62 } 63 64 SingletonContainer set(ObjectFactory object, string key) { 65 this.factories.set(new ExceptionChainingObjectFactory(new InProcessObjectFactoryDecorator(object), key), key); 66 67 return this; 68 } 69 70 SingletonContainer remove(string key) { 71 this.factories.remove(key); 72 this.singletons.remove(key); 73 74 return this; 75 } 76 77 Object get(string key) { 78 79 if (!this.singletons.has(key)) { 80 if (!this.factories.has(key)) { 81 throw new NotFoundException("Object with id " ~ key ~ " not found."); 82 } 83 84 this.singletons.set( 85 this.factories.get(key).factory(), 86 this.resolve(key), 87 ); 88 } 89 90 return this.singletons.get(key); 91 } 92 93 bool has(in string key) inout { 94 return this.factories.has(key); 95 } 96 97 SingletonContainer instantiate() { 98 import std.algorithm : filter; 99 foreach (pair; this.factories.contents.byKeyValue.filter!((pair) => pair.key !in this.singletons.contents)) { 100 this.singletons.set( 101 pair.value.factory, 102 pair.key, 103 ); 104 } 105 106 return this; 107 } 108 109 SingletonContainer link(string key, string alias_) { 110 this.singletons.link(key, alias_); 111 this.factories.link(key, alias_); 112 113 return this; 114 } 115 116 SingletonContainer unlink(string alias_) { 117 this.singletons.unlink(alias_); 118 this.factories.unlink(alias_); 119 120 return this; 121 } 122 123 const(string) resolve(in string key) const { 124 return this.factories.resolve(key); 125 } 126 127 128 ObjectFactory getFactory(string identity) { 129 return this.factories.get(identity); 130 } 131 132 InputRange!(Tuple!(ObjectFactory, string)) getFactories() { 133 import std.algorithm; 134 135 return this.factories.contents.byKeyValue.map!( 136 a => tuple(a.value, a.key) 137 ).inputRangeObject; 138 } 139 } 140 }