A Python library for implementing layered architecture with services and dependency injection.
Project description
LayeredPy
LayeredPy is a Python library built to implement a clean, maintainable layered architecture. It offers support for service-oriented programming and includes built-in dependency injection (DI) to improve code modularity and testability.
Features
- Layered Architecture: Enables a structured and modular separation of concerns.
- Dependency Injection: Dependencies get injected automatically, improving testability and reducing tight coupling.
- Service Management: Defines base services with extensible behavior for your application logic.
- CLI Tool: Generate service boilerplate with the
servicetool.
Installation
Install LayeredPy using pip:
pip install layered-py
Getting Started
Here’s how you can use LayeredPy in your projects.
1. Define and Register a Service
Create a service by subclassing the Service class and use the @register decorator to register it.
from layered_py.service import Service
from layered_py.decorators import register
@register(singleton=True)
class GreetingService(Service):
def say_hello(self):
print("Hello from LayeredPy!")
2. Inject the Service Where Needed
With the @inject decorator, services can be directly injected as attributes of the class. You don’t need to pass them explicitly.
LayeredPy uses lazy loading with the load_all_modules function to collect all classes with the register annotation. Using this function in a
central point in your software is mandatory so classes that use Inject function properly.
from layered_py.decorators import inject
from layered_py.bootstrap import load_all_modules
class MyApp:
@inject
def run(self, GreetingService):
GreetingService.say_hello() # Directly access the injected service
# Example usage
if __name__ == "__main__":
load_all_modules()
app = MyApp()
app.run()
Output:
Hello from LayeredPy!
3. Generate Service Templates with the CLI
You can use the built-in layeredpy CLI tool to create new service boilerplates automatically.
The CLI tool of LayeredPy can create services, repositories, domains and presentation classes
Example Usage:
layeredpy create MyNewService
This command generates the following services/MyNewService.py file:
from layered_py.service import Service
from layered_py.decorators import register
@register(singleton=True)
class MyNewService(Service):
def setup(self):
pass
def handle(self):
raise NotImplementedError
Create complete class sets with layeredpy generate
LayeredPy is capable of creating complete sets of classes for example this command:
layeredpy generate User
will create: UserService, UserRepository, UserModel and UserRoutes with the register annotation so they are DI-ready.
Configuration
To change the paths where the classes will be generated create a layeredpy_config.yml in your project root
The .yml File should contain the following:
service_destination: "your_services"
domain_destination: "your_domains"
repository_destination: "your_repositories"
presentation_destination: "your_presentations"
License
This project is licensed under the MIT License.
Support
For questions or support, feel free to open an issue on the GitHub Issues page. You can find more information on the project repository.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file layered_py-3.0.0.tar.gz.
File metadata
- Download URL: layered_py-3.0.0.tar.gz
- Upload date:
- Size: 8.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0d204812c6fc530e356a34a4ad05d3811c234d24311ad7f38a19cbb3058c3c64
|
|
| MD5 |
07c53171657d70a2fb892c3f966c5771
|
|
| BLAKE2b-256 |
a22d3f335515e78fc6be488faf60bd2f737ae406d1e2423c18804eeea6c66cdc
|
File details
Details for the file layered_py-3.0.0-py3-none-any.whl.
File metadata
- Download URL: layered_py-3.0.0-py3-none-any.whl
- Upload date:
- Size: 9.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
471ae995719b4dcabaa28fda6984b14635e9765ec60fd5c77c0febe5fac937a5
|
|
| MD5 |
ad63afa0e868ba0cb257eacf79c9bff8
|
|
| BLAKE2b-256 |
03496f6f9d67af925c036090c92f286959e8f59499d1f99fd5bd451ee8aef565
|