@@ -34,12 +34,7 @@ class InstanceLifetime(Enum):
3434 """
3535 New instance will be created every time container will be ask for object on given key.
3636 """
37- SingletonEager = 1
38- """
39- New instance will be created on registering the callable object to container and the result will be
40- stored in the container.
41- """
42- SingletonLazy = 2
37+ Singleton = 1
4338 """
4439 New instance will be created the first time container will be asked for object under given key. Both the callable
4540 and object will be stored in the container.
@@ -111,7 +106,7 @@ def register_object(self, key, obj):
111106 def register_callable (self , key , callable_object , lifetime = InstanceLifetime .NewInstancePerCall ):
112107 """
113108 Registers a callable object that will be used to create a new instance of an object that will be returned upon
114- calling the get () method.
109+ calling the get_instance () method.
115110
116111 Based on the lifetime parameter, either the callable will be stored, and called whenever object is needed, or
117112 the callable will be called on registering, and the returned object will be stored.
@@ -123,9 +118,7 @@ def register_callable(self, key, callable_object, lifetime=InstanceLifetime.NewI
123118 """
124119 if lifetime == InstanceLifetime .NewInstancePerCall :
125120 provider = providers .NewInstancesProvider (callable_object )
126- elif lifetime == InstanceLifetime .SingletonEager :
127- provider = providers .EagerSingleInstanceProvider (callable_object )
128- elif lifetime == InstanceLifetime .SingletonLazy :
121+ elif lifetime == InstanceLifetime .Singleton :
129122 provider = providers .LazySingleInstanceProvider (callable_object )
130123 else :
131124 raise TypeError ('Unsupported instance lifetime.' )
@@ -135,33 +128,31 @@ def register_callable(self, key, callable_object, lifetime=InstanceLifetime.NewI
135128 def register_callable_with_deps (self , key , callable_object , lifetime = InstanceLifetime .NewInstancePerCall ):
136129 if lifetime == InstanceLifetime .NewInstancePerCall :
137130 provider = providers .NewInstancesWithDepsProvider (callable_object , self )
138- elif lifetime == InstanceLifetime .SingletonEager :
139- provider = providers .EagerSingleInstanceWithDepsProvider (callable_object , self )
140- elif lifetime == InstanceLifetime .SingletonLazy :
131+ elif lifetime == InstanceLifetime .Singleton :
141132 provider = providers .LazySingleInstanceWithDepsProvider (callable_object , self )
142133 else :
143134 raise TypeError ('Unsupported instance lifetime.' )
144135
145136 self ._register_provider_for_key (key , provider )
146137
147- def resolve (self , key ):
138+ def resolve (self , key , context = None ):
148139 """
149140 Return instance based on what was registered for a given key.
150141
151142 :param key: Key under which the object or callable was registered.
152- :return: Instance related to that key.
143+ :return: Instance related to that key.
153144 """
154- return self ._resolve (key )
145+ return self ._resolve (key , context )
155146
156- def build (self , cls ):
147+ def build (self , cls , context = None ):
157148 """
158149 Build a new instance of class cls injecting dependencies of an object from objects registered in the container.
159150
160151 :param cls: Class of which object to build.
161152 :return:
162153 """
163154 provider = providers .NewInstancesWithDepsProvider (cls , self )
164- return provider .get ( )
155+ return provider .get_instance ( context )
165156
166157 @property
167158 def name (self ):
@@ -180,9 +171,17 @@ def get_keys(self):
180171 """
181172 return self ._locator .get_keys ()
182173
183- def _resolve (self , key ):
184- instance_provider = self ._locator .get (key )
185- return instance_provider .get ()
174+ def _resolve (self , key , context = None ):
175+ if context :
176+ try :
177+ item = context [key ]
178+ except KeyError :
179+ pass
180+ else :
181+ return item
182+
183+ instance_provider = self ._locator .locate (key )
184+ return instance_provider .get_instance (context )
186185
187186 def _register_provider_for_key (self , id , provider ):
188187 self ._locator .register (id , provider )
@@ -226,15 +225,25 @@ def get_all_keys(self):
226225
227226 return result
228227
229- def _resolve (self , id ):
228+ def _resolve (self , id , context = None ):
230229 if isinstance (id , str ):
231230 instance_id = self ._name_resolver .parse (id )
231+
232232 if instance_id .namespace :
233233 container = self ._sub_containers [instance_id .namespace ]
234- return container .resolve (instance_id .id )
234+ return container .resolve (instance_id .id , context )
235235 else :
236- provider = self ._locator .get (instance_id .id )
237- return provider .get ()
236+ if context :
237+ try :
238+ item = context [instance_id .id ]
239+ except KeyError :
240+ pass
241+ else :
242+ return item
243+
244+ provider = self ._locator .locate (instance_id .id )
245+ return provider .get_instance (context )
246+
238247 else :
239- provider = self ._locator .get (id )
240- return provider .get ( )
248+ provider = self ._locator .locate (id )
249+ return provider .get_instance ( context )
0 commit comments