Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

service-collection

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

service-collection

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

  • 0.6.3
  • PyPI
  • Socket score

Maintainers
1

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
from service_collection import inject

# 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):
        self.foo_service = inject(IFooService)

    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

Import the inject function to retrieve service instances easily in your code: from service_collection import inject

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, inject

# 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):
        self.foo_service = inject(IFooService)

    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()

# 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.

FAQs


Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc