mirror of
				https://github.com/ets-labs/python-dependency-injector.git
				synced 2025-10-30 23:47:40 +03:00 
			
		
		
		
	Update Singleton provider docs
This commit is contained in:
		
							parent
							
								
									1e1266a4d3
								
							
						
					
					
						commit
						98c4ec71c1
					
				
										
											Binary file not shown.
										
									
								
							| Before Width: | Height: | Size: 9.4 KiB After Width: | Height: | Size: 10 KiB | 
										
											Binary file not shown.
										
									
								
							| Before Width: | Height: | Size: 11 KiB After Width: | Height: | Size: 10 KiB | 
|  | @ -1,7 +1,7 @@ | ||||||
| Singleton providers | Singleton providers | ||||||
| ------------------- | ------------------- | ||||||
| 
 | 
 | ||||||
| ``Singleton`` provider creates new instance of specified class on first call | ``di.Singleton`` provider creates new instance of specified class on first call | ||||||
| and returns same instance on every next call. | and returns same instance on every next call. | ||||||
| 
 | 
 | ||||||
| Example: | Example: | ||||||
|  | @ -16,9 +16,9 @@ Example: | ||||||
| Singleton providers and injections | Singleton providers and injections | ||||||
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||||||
| 
 | 
 | ||||||
| ``Singleton`` providers use ``Factory`` providers for first creation of | ``di.Singleton`` providers use ``di.Factory`` providers for first creation of | ||||||
| specified class instance, so, all of the rules about injections are the same, | specified class instance, so, all of the rules about injections are the same, | ||||||
| as for ``Factory`` providers. | as for ``di.Factory`` providers. | ||||||
| 
 | 
 | ||||||
| .. image:: /images/providers/singleton_internals.png | .. image:: /images/providers/singleton_internals.png | ||||||
|     :width: 80% |     :width: 80% | ||||||
|  | @ -26,27 +26,29 @@ as for ``Factory`` providers. | ||||||
| 
 | 
 | ||||||
| .. note:: | .. note:: | ||||||
| 
 | 
 | ||||||
|     Due that ``Singleton`` provider creates specified class instance only on |     Due that ``di.Singleton`` provider creates specified class instance only on | ||||||
|     the first call, all injections are done once, during the first call, also. |     the first call, all injections are done once, during the first call, also. | ||||||
|     Every next call, while instance has been already created and memorized, no |     Every next call, while instance has been already created and memorized, no | ||||||
|     injections are done, ``Singleton`` provider just returns memorized earlier |     injections are done, ``di.Singleton`` provider just returns memorized  | ||||||
|     instance. |     earlier instance. | ||||||
| 
 | 
 | ||||||
|     This may cause some problems, for example, in case of trying to bind |     This may cause some problems, for example, in case of trying to bind | ||||||
|     ``Factory`` provider with ``Singleton`` provider (provided by dependent |     ``di.Factory`` provider with ``di.Singleton`` provider (provided by  | ||||||
|     ``Factory`` instance will be injected only once, during the first call). |     dependent ``di.Factory`` instance will be injected only once, during the  | ||||||
|     Be aware that such behaviour was made with opened eyes and is not a bug. |     first call). Be aware that such behaviour was made with opened eyes and is  | ||||||
|  |     not a bug. | ||||||
| 
 | 
 | ||||||
|     By the way, in such case, ``Delegate`` provider can be useful. It makes |     By the way, in such case, ``di.Delegate`` provider can be useful. It makes | ||||||
|     possible to inject providers *as is*. Please check out full example in |     possible to inject providers *as is*. Please check out full example in | ||||||
|     *Providers delegation* section. |     *Providers delegation* section. | ||||||
| 
 | 
 | ||||||
| Singleton providers resetting | Singleton providers resetting | ||||||
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||||||
| 
 | 
 | ||||||
| Created and memorized by ``Singleton`` instance can be reset. Reset of | Created and memorized by ``di.Singleton`` instance can be reset. Reset of | ||||||
| ``Singleton``'s memorized instance is done by clearing reference to it. Further | ``di.Singleton``'s memorized instance is done by clearing reference to it.  | ||||||
| lifecycle of memorized instance is out of ``Singleton`` provider's control. | Further lifecycle of memorized instance is out of ``di.Singleton`` provider's  | ||||||
|  | control. | ||||||
| 
 | 
 | ||||||
| Example: | Example: | ||||||
| 
 | 
 | ||||||
|  | @ -56,13 +58,14 @@ Example: | ||||||
| Singleton providers delegation | Singleton providers delegation | ||||||
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||||||
| 
 | 
 | ||||||
| ``Singleton`` provider could be delegated to any other provider via any kind of | ``di.Singleton`` provider could be delegated to any other provider via any  | ||||||
| injection. Delegation of ``Singleton`` providers is the same as ``Factory`` | kind of injection. Delegation of ``di.Singleton`` providers is the same as  | ||||||
| providers delegation, please follow *Factory providers delegation* section for | ``di.Factory`` providers delegation, please follow  | ||||||
| example. | *Factory providers delegation* section for example. | ||||||
| 
 | 
 | ||||||
| ``Singleton`` delegate could be created obviously using  | ``di.Singleton`` delegate could be created obviously using  | ||||||
| ``Delegate(Singleton())`` or by calling ``Singleton.delegate()`` method. | ``di.Delegate(di.Singleton())`` or by calling ``di.Singleton.delegate()``  | ||||||
|  | method. | ||||||
| 
 | 
 | ||||||
| Example: | Example: | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -1,6 +1,6 @@ | ||||||
| """`Singleton` providers example.""" | """`di.Singleton` providers example.""" | ||||||
| 
 | 
 | ||||||
| from dependency_injector.providers import Singleton | import dependency_injector as di | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| class UserService(object): | class UserService(object): | ||||||
|  | @ -9,7 +9,7 @@ class UserService(object): | ||||||
| 
 | 
 | ||||||
| # Singleton provider creates new instance of specified class on first call and | # Singleton provider creates new instance of specified class on first call and | ||||||
| # returns same instance on every next call. | # returns same instance on every next call. | ||||||
| users_service_provider = Singleton(UserService) | users_service_provider = di.Singleton(UserService) | ||||||
| 
 | 
 | ||||||
| # Retrieving several UserService objects: | # Retrieving several UserService objects: | ||||||
| user_service1 = users_service_provider() | user_service1 = users_service_provider() | ||||||
|  |  | ||||||
|  | @ -1,13 +1,12 @@ | ||||||
| """`Singleton` providers delegation example.""" | """`di.Singleton` providers delegation example.""" | ||||||
| 
 | 
 | ||||||
| from dependency_injector.providers import Singleton | import dependency_injector as di | ||||||
| from dependency_injector.providers import Delegate |  | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| # Some singleton provider and few delegates of it: | # Some singleton provider and few delegates of it: | ||||||
| singleton_provider = Singleton(object) | singleton_provider = di.Singleton(object) | ||||||
| singleton_provider_delegate1 = singleton_provider.delegate() | singleton_provider_delegate1 = singleton_provider.delegate() | ||||||
| singleton_provider_delegate2 = Delegate(singleton_provider) | singleton_provider_delegate2 = di.Delegate(singleton_provider) | ||||||
| 
 | 
 | ||||||
| # Making some asserts: | # Making some asserts: | ||||||
| assert singleton_provider_delegate1() is singleton_provider | assert singleton_provider_delegate1() is singleton_provider | ||||||
|  |  | ||||||
|  | @ -1,6 +1,6 @@ | ||||||
| """`Singleton` providers resetting example.""" | """`di.Singleton` providers resetting example.""" | ||||||
| 
 | 
 | ||||||
| from dependency_injector.providers import Singleton | import dependency_injector as di | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| class UserService(object): | class UserService(object): | ||||||
|  | @ -8,7 +8,7 @@ class UserService(object): | ||||||
|     """Example class UserService.""" |     """Example class UserService.""" | ||||||
| 
 | 
 | ||||||
| # Users service singleton provider: | # Users service singleton provider: | ||||||
| users_service_provider = Singleton(UserService) | users_service_provider = di.Singleton(UserService) | ||||||
| 
 | 
 | ||||||
| # Retrieving several UserService objects: | # Retrieving several UserService objects: | ||||||
| user_service1 = users_service_provider() | user_service1 = users_service_provider() | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue
	
	Block a user