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.prototype_container; 32 33 import aermicioi.aedi.container.container; 34 import aermicioi.aedi.storage.object_storage; 35 import aermicioi.aedi.factory.factory; 36 import aermicioi.aedi.exception; 37 import aermicioi.aedi.container.factory; 38 39 import std.range.interfaces; 40 import std.typecons; 41 42 /** 43 Prototype container. 44 45 Instantiates a new object using passed ObjectFactory implementation, on each request of it by some part of an application. 46 **/ 47 class PrototypeContainer : ConfigurableContainer { 48 49 private { 50 51 ObjectStorage!(ObjectFactory, string) factories; 52 } 53 54 public { 55 56 this() { 57 this.factories = new ObjectStorage!(ObjectFactory, string); 58 } 59 60 PrototypeContainer set(ObjectFactory object, string key) { 61 this.factories.set(new ExceptionChainingObjectFactory(new InProcessObjectFactoryDecorator(object), key), key); 62 63 return this; 64 } 65 66 PrototypeContainer remove(string key) { 67 this.factories.remove(key); 68 69 return this; 70 } 71 72 Object get(string key) { 73 if (this.factories.has(key)) { 74 75 return this.factories.get(key).factory(); 76 } 77 78 throw new NotFoundException("Object by id " ~ key ~ " not found"); 79 } 80 81 bool has(in string key) inout { 82 return this.factories.has(key); 83 } 84 85 PrototypeContainer instantiate() { 86 87 return this; 88 } 89 90 PrototypeContainer link(string key, string alias_) { 91 this.factories.link(key, alias_); 92 93 return this; 94 } 95 96 PrototypeContainer unlink(string alias_) { 97 this.factories.unlink(alias_); 98 99 return this; 100 } 101 102 const(string) resolve(in string key) const { 103 return this.factories.resolve(key); 104 } 105 106 ObjectFactory getFactory(string identity) { 107 return this.factories.get(identity); 108 } 109 110 InputRange!(Tuple!(ObjectFactory, string)) getFactories() { 111 import std.algorithm; 112 113 return this.factories.contents.byKeyValue.map!( 114 a => tuple(a.value, a.key) 115 ).inputRangeObject; 116 } 117 } 118 }