@@ -5,21 +5,21 @@ public class Container {
55 private( set) static var root = Container ( )
66
77 /// Stored object instance factories.
8- var modules : [ String : Module ] = [ : ]
8+ var _storage : [ String : Module ] = [ : ]
99
1010 public init ( ) { }
11- deinit { modules . removeAll ( ) }
11+ deinit { _storage . removeAll ( ) }
1212}
1313
1414public extension Container {
1515 /// Registers a specific type and its instantiating factory.
1616 @discardableResult
1717 func register( module: Module ) -> Self {
1818 let key = module. name
19- if let _ = modules [ key] {
19+ if let _ = _storage [ key] {
2020 assertionFailure ( " \( key) Key is existed. Please check module \( module) " )
2121 }
22- modules [ key] = module
22+ _storage [ key] = module
2323 return self
2424 }
2525
@@ -49,15 +49,15 @@ extension Container {
4949 static func weakResolve< T, U: InjectionKeyType > ( for type: U . Type ) -> T ? {
5050 root. module ( type) ? . resolve ( ) as? T
5151 }
52-
52+
5353 /// Check if the dependency is registered in the container.
5454 static func isRegistered< T: InjectionKeyType > ( _ type: T . Type ) -> Bool {
5555 root. module ( type) != nil
5656 }
5757
5858 func module< T: InjectionKeyType > ( _ type: T . Type ) -> Module ? {
5959 let keyName = KeyName ( type) . name
60- return modules [ keyName]
60+ return _storage [ keyName]
6161 }
6262}
6363
@@ -95,6 +95,18 @@ public extension Container {
9595 // Used later in property wrapper
9696 Self . root = self
9797 }
98+
99+ static subscript< T: InjectionKeyType > ( _ type: T . Type ) -> T . Value {
100+ resolve ( for: type)
101+ }
102+
103+ var count : Int {
104+ _storage. count
105+ }
106+
107+ var isEmpty : Bool {
108+ _storage. isEmpty
109+ }
98110}
99111
100112#if DEBUG
@@ -103,6 +115,14 @@ public extension Container {
103115 static func clear( ) {
104116 root = . init( )
105117 }
118+
119+ static func register( _ module: Module ) {
120+ root. register ( module: module)
121+ }
122+
123+ static func register( contentsOf newElements: [ Module ] ) {
124+ root. register ( contentsOf: newElements)
125+ }
106126}
107127
108128#endif
0 commit comments