Skip to main content

ASP.NET Core-style dependency injection service collection for Python

Project description

Here's the updated README with an example including the creation and usage of the inject function:


service_collection

service_collection is a lightweight Python package that mimics ASP.NET's dependency injection system. It also incorporates an Angular-style Inject method to resolve dependencies, making it easier to manage and inject dependencies in your applications. It currently supports transient and singleton services. Scoped services will be added in a future release if there is a demand for them.

Installation

Install the package using pip:

pip install service-collection

Usage

Setting Up the Service Collection

  1. Define your services and interfaces:
from abc import ABC, abstractmethod

# Define an abstract base class for the service interface
class IFooService(ABC):
    @abstractmethod
    def do_something(self):
        pass

# Implement the interface
class FooService(IFooService):
    def do_something(self):
        return "FooService did something!"

# Define another service that depends on the interface
class IBarService(ABC):
    @abstractmethod
    def do_something(self):
        pass

class BarService(IBarService):
    def __init__(self, foo_service: IFooService):
        self.foo_service = foo_service

    def do_something(self):
        return f"BarService did something with {self.foo_service.do_something()}"
  1. Register your services:
from service_collection import ServiceCollection

services = ServiceCollection()
services.add_transient(IFooService, FooService)
services.add_singleton(IBarService, BarService)
  1. Build the service provider:
service_provider = services.build_service_provider()

Creating and Using the Inject Function

Define an inject function to retrieve service instances easily in your code:

def inject(service_type):
    return service_provider.get_service(service_type)

Use the inject function to resolve services:

foo_service = inject(IFooService)
print(foo_service.do_something())  # Output: FooService did something!

bar_service = inject(IBarService)
print(bar_service.do_something())  # Output: BarService did something with FooService did something!

Full Example

Here is a complete example demonstrating how to set up and use the service_collection package with the inject function:

from abc import ABC, abstractmethod
from service_collection import ServiceCollection

# Define an abstract base class for the service interface
class IFooService(ABC):
    @abstractmethod
    def do_something(self):
        pass

# Implement the interface
class FooService(IFooService):
    def do_something(self):
        return "FooService did something!"

# Define another service that depends on the interface
class IBarService(ABC):
    @abstractmethod
    def do_something(self):
        pass

class BarService(IBarService):
    def __init__(self, foo_service: IFooService):
        self.foo_service = foo_service

    def do_something(self):
        return f"BarService did something with {self.foo_service.do_something()}"

# Register services
services = ServiceCollection()
services.add_transient(IFooService, FooService)
services.add_singleton(IBarService, BarService)

# Build the service provider
service_provider = services.build_service_provider()

# Define the inject function
def inject(service_type):
    return service_provider.get_service(service_type)

# Resolve and use services
foo_service = inject(IFooService)
print(foo_service.do_something())  # Output: FooService did something!

bar_service = inject(IBarService)
print(bar_service.do_something())  # Output: BarService did something with FooService did something!

License

This project is licensed under the MIT License. See the LICENSE file for details.


This README provides an overview of how to install, set up, and use the service_collection package with example code snippets, demonstrating the use of abstract base classes for service interfaces and including the creation and usage of the inject function.

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

service_collection-0.4.0.tar.gz (4.5 kB view details)

Uploaded Source

Built Distribution

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

service_collection-0.4.0-py3-none-any.whl (4.8 kB view details)

Uploaded Python 3

File details

Details for the file service_collection-0.4.0.tar.gz.

File metadata

  • Download URL: service_collection-0.4.0.tar.gz
  • Upload date:
  • Size: 4.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.9.6

File hashes

Hashes for service_collection-0.4.0.tar.gz
Algorithm Hash digest
SHA256 a908db092071e64b31c6db48c848481f92fea8c663c66210b4f6a008bc66991c
MD5 a0d81632f923256a98b3ab4a6294ef7b
BLAKE2b-256 a338dc07a786178334f7ece389b4ad243b1ffe66a30cf8de681bd7c8feda4e0f

See more details on using hashes here.

File details

Details for the file service_collection-0.4.0-py3-none-any.whl.

File metadata

File hashes

Hashes for service_collection-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1d51f431de9757ad523041feedb3cb7ed252c587fe7c2886e1283c2ccaf8dd5f
MD5 71af137b84feca8e1e9362f7047f3679
BLAKE2b-256 2d88eed34ab3d6d724638e97fe900d52ed80ee75ea2528cabd06b3a2a1c1e600

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