Skip to main content

A minimalistic and easy to use Dependency Injection Framework.

Project description

minidi

A minimalistic and easy to use Dependency Injection Framework. Dependency Injection should help clean up code, reduce coupling, increase cohesion and simplify setups and cleanups of unittests.

Usage

canUserAccessFile = FileSystemAccessDetector.canUserAccessFile(userId=1, file='some/path/to/file.txt')

This static code is easy to access, but since the dependencies the FileSystemAccessDetector relies on are hard coded into it, we cannot test the class itself without testing all underlying dependencies with it.

pFileSystemAccessDetector = FileSystemAccessDetector()
pFileSystemAccessDetector.pFileShareRegister = pFileShareRegister # some instance used earlier already
pFileSystemAccessDetector.pFileSystem = FileSystem()
canUserAccessFile = pFileSystemAccessDetector.canUserAccessFile(userId=1, file='some/path/to/file.txt')

The non-static approach is already better, since we now have influence over the code we give the FileSystemAccessDetector to work with, essentially enabling dependency injection, but this would be tedious to write. Just imagine a logic class needing 6 different dependencies ... if only we could save time, somehow ...?

pFileSystemAccessDetector = minidi.get(FileSystemAccessDetector)
canUserAccessFile = pFileSystemAccessDetector.canUserAccessFile(userId=1, file='some/path/to/file.txt')

And we are done. Simple, isn't it? Now how can we make this magic happen?

  1. FileSystemAccessDetector is derived from the empty Interface minidi.Injectable
  2. we annotate our dependencies, which have to be Injectable as well
  3. we code our logic functions as we would normally do
class FileSystemAccessDetector(minidi.Injectable):
	# injectables, get initialized via minidi.get if you call minidi.get(FileSystemAccessDetector)
	pFileShareRegister: FileShareRegister
	pFileSystem: FileSystem
	
	def canUserAccessFile(self, userId: int, file: str) -> bool:
		[...] # other dependencies available in self.pFileShareRegister and self.pFileSystem

This implementation opens up the code to be tested without the need of a real FileShareRegister or FileSystem, and therefor also a real file.

class TestFileSystemAccessDetector(unittest.TestCase):
	def test_CanUserAccessFile(self):
		# we don't want to initialize the dependencies over minidi.get,
		# this would keep the problems the exact same as with static code;
		# instead we mock to fake underlying functionality to only test what we want to test here
		pFileShareRegister = FileShareRegister()
		pFileShareRegister.getSharedUserIds = unittest.mock.Mock(return_value=[1,2,5,7])

		pFileSystem = FileSystem()
		pFileSystem.getOwnerUserId = unittest.mock.Mock(return_value=3)
		pFileSystem.isPublicFile = unittest.mock.Mock(return_value=False)
		pFileSystem.isProtectedFile = unittest.mock.Mock(return_value=True)
		pFileSystem.isPrivateFile = unittest.mock.Mock(return_value=False)

		pFileSystemAccessDetector = FileSystemAccessDetector()
		# the dependencies get injected from the outside right here
		pFileSystemAccessDetector.pFileShareRegister = pFileShareRegister
		pFileSystemAccessDetector.pFileSystem = pFileSystem

		[...] # run test with assertions

And boom, easy maintainable code with less hard coded dependencies.

Technical Information

  • you CANNOT give an Injectable any other member other than Injectables (except for non-annotated constants), they are designed to work stateless like global functions with the benefit of replaceability
  • minidi.get can only fill the dependencies you have annotated in your class
  • minidi will hold all created Injectables indefinitely, until your program gets terminated; still better than static code, which will be allocated on the programs start

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

minidi-1.0.1.tar.gz (16.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

minidi-1.0.1-py3-none-any.whl (16.0 kB view details)

Uploaded Python 3

File details

Details for the file minidi-1.0.1.tar.gz.

File metadata

  • Download URL: minidi-1.0.1.tar.gz
  • Upload date:
  • Size: 16.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.9.2

File hashes

Hashes for minidi-1.0.1.tar.gz
Algorithm Hash digest
SHA256 982572f18a7ce973b62a7e424c97f97d6dd9c570261210b61ddfe4cb9d39c326
MD5 75373878dc2252d8b2e38c9ba5976249
BLAKE2b-256 3998c06341dd486a7ed78c0ecc2ac8d3b1128a5e195869ee127d938ba94eb9be

See more details on using hashes here.

File details

Details for the file minidi-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: minidi-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 16.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.9.2

File hashes

Hashes for minidi-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 3be9c365fc44e6e0cb43d3f01ff4128acb0190633b69a957d235354b02185ba3
MD5 e780f57f7bf7a7fd67407dd8c5831032
BLAKE2b-256 aae67e435b6ebbd6417c97c12f3d68492c5c9bf2d2c1cf42ce7ce1be286e139a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page