Skip to main content

Port from .NET to Python of the Neuroglia Framework

Project description

Neuroglia Python Framework

Neuroglia is a lightweight, opinionated framework built on top of FastAPI that provides a comprehensive set of tools and patterns for building clean, maintainable, and scalable microservices. It enforces architectural best practices and provides out-of-the-box implementations of common patterns.

๐Ÿš€ Key Features

  • ๐Ÿ—๏ธ Clean Architecture: Enforces separation of concerns with clearly defined layers (API, Application, Domain, Integration)
  • ๐Ÿ’‰ Dependency Injection: Lightweight container with automatic service discovery and registration
  • ๐ŸŽฏ CQRS & Mediation: Command Query Responsibility Segregation with built-in mediator pattern
  • ๐Ÿ“ก Event-Driven Architecture: Native support for CloudEvents, event sourcing, and reactive programming
  • ๐ŸŽฏ Resource Oriented Architecture: Declarative resource management with watchers, controllers, and reconciliation loops
  • ๐Ÿ”Œ MVC Controllers: Class-based API controllers with automatic discovery and OpenAPI generation
  • ๐Ÿ—„๏ธ Repository Pattern: Flexible data access layer with support for MongoDB, Event Store, and in-memory repositories
  • ๐Ÿ“Š Object Mapping: Bidirectional mapping between domain models and DTOs
  • โšก Reactive Programming: Built-in support for RxPy and asynchronous event handling
  • ๐Ÿ”ง 12-Factor Compliance: Implements all 12-Factor App principles
  • ๐Ÿ“ Rich Serialization: JSON serialization with advanced features

๐ŸŽฏ Architecture Overview

Neuroglia promotes a clean, layered architecture that separates concerns and makes your code more maintainable:

src/
โ”œโ”€โ”€ api/           # ๐ŸŒ API Layer (Controllers, DTOs, Routes)
โ”œโ”€โ”€ application/   # ๐Ÿ’ผ Application Layer (Commands, Queries, Handlers, Services)
โ”œโ”€โ”€ domain/        # ๐Ÿ›๏ธ Domain Layer (Entities, Value Objects, Business Rules)
โ””โ”€โ”€ integration/   # ๐Ÿ”Œ Integration Layer (External APIs, Repositories, Infrastructure)

๐Ÿ“š Documentation

๐Ÿ“– Complete Documentation

Quick Links

Sample Applications

Learn by example with complete sample applications:

  • ๐Ÿฆ OpenBank - Event-sourced banking domain with CQRS
  • ๐Ÿงช Lab Resource Manager - Resource Oriented Architecture with watchers and reconciliation
  • ๐Ÿ–ฅ๏ธ Desktop Controller - Remote desktop management API
  • ๐Ÿšช API Gateway - Microservice gateway with authentication

๐Ÿ”ง Quick Start

# Install from PyPI
pip install neuroglia

# Or install from source
git clone <repository-url>
cd pyneuro
pip install -e .

Create your first application:

from neuroglia.hosting.web import WebApplicationBuilder

# Create and configure the application
builder = WebApplicationBuilder()
builder.add_controllers(["api.controllers"])

# Build and run
app = builder.build()
app.use_controllers()
app.run()

๐Ÿ—๏ธ Framework Components

Component Purpose Documentation
Core Base types, utilities, module loading ๐Ÿ“– Core
Dependency Injection Service container and registration ๐Ÿ“– DI
Hosting Web application hosting and lifecycle ๐Ÿ“– Hosting
MVC Controllers and routing ๐Ÿ“– MVC
Mediation CQRS, commands, queries, events ๐Ÿ“– Mediation
Resource Oriented Architecture Watchers, controllers, reconciliation ๐Ÿ“– ROA
Data Repository pattern, event sourcing ๐Ÿ“– Data
Eventing CloudEvents, pub/sub, reactive ๐Ÿ“– Events
Mapping Object-to-object mapping ๐Ÿ“– Mapping
Serialization JSON and other serialization ๐Ÿ“– Serialization

๐Ÿ“‹ Requirements

  • Python 3.11+
  • FastAPI
  • Pydantic
  • RxPy (for reactive features)
  • Motor (for MongoDB support)
  • Additional dependencies based on features used

๐Ÿงช Testing

Neuroglia includes a comprehensive test suite covering all framework features with both unit and integration tests.

Running Tests

Run All Tests

# Run the complete test suite
pytest

# Run with coverage report
pytest --cov=neuroglia --cov-report=html --cov-report=term

# Run in parallel for faster execution
pytest -n auto

Run Specific Test Categories

# Run only unit tests
pytest tests/unit/

# Run only integration tests  
pytest tests/integration/

# Run tests by marker
pytest -m "unit"
pytest -m "integration"
pytest -m "slow"

Run Feature-Specific Tests

# Test dependency injection
pytest tests/unit/test_dependency_injection.py

# Test CQRS and mediation
pytest tests/unit/test_cqrs_mediation.py

# Test data access layer
pytest tests/unit/test_data_access.py

# Test object mapping
pytest tests/unit/test_mapping.py

# Run integration tests
pytest tests/integration/test_full_framework.py

Test Coverage

Our test suite provides comprehensive coverage of the framework:

  • Unit Tests: >95% coverage for core framework components
  • Integration Tests: End-to-end workflow validation
  • Performance Tests: Load testing for critical paths
  • Sample Application Tests: Real-world usage scenarios

Test Organization

tests/
โ”œโ”€โ”€ unit/              # ๐Ÿ”ฌ Unit tests for individual components
โ”œโ”€โ”€ integration/       # ๐Ÿ”— Integration tests for workflows
โ”œโ”€โ”€ fixtures/          # ๐Ÿ› ๏ธ Shared test fixtures and utilities
โ””โ”€โ”€ conftest.py       # โš™๏ธ pytest configuration

What's Tested

  • Basic dependency injection service registration and resolution
  • CQRS command and query handling through the mediator
  • Object mapping between different types
  • Repository pattern with various backend implementations
  • Full framework integration workflows

Test Fixtures

We provide comprehensive test fixtures for:

  • Dependency injection container setup
  • Sample services and repositories
  • Mock data and test entities
  • Configuration and settings

Known Test Limitations

  • Some dependency injection features (like strict service lifetimes) may have implementation-specific behavior
  • MongoDB integration tests require a running MongoDB instance
  • Event Store tests require EventStoreDB connection

Adding Tests

When contributing, please include tests for new features:

import pytest
from neuroglia.dependency_injection import ServiceCollection

class TestNewFeature:
    
    @pytest.mark.unit
    def test_my_unit_feature(self):
        """Test individual component"""
        result = self.service.do_something()
        assert result == expected_value

๐Ÿค Contributing

We welcome contributions! Please see our Contributing Guide for details.

๐Ÿ“„ License

This project is licensed under the Apache License 2.0 - see the LICENSE file for details.

๐Ÿ“– Documentation

Complete documentation is available at https://bvandewe.github.io/pyneuro/

Disclaimer

This project was the opportunity for me (cdavernas) to learn Python while porting some of the concepts and services of the .NET version of the Neuroglia Framework

Packaging

# Set `package-mode = true` in pyproject.toml
# Set the version tag in pyproject.toml
# Commit changes
# Create API Token in pypi.org...
# Configure credentials for pypi registry:
poetry config pypi-token.pypi  {pypi-....mytoken}
# Build package locally
poetry build
# Publish package to pypi.org:
poetry publish

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

neuroglia_python-0.1.7.tar.gz (58.7 kB view details)

Uploaded Source

Built Distribution

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

neuroglia_python-0.1.7-py3-none-any.whl (83.7 kB view details)

Uploaded Python 3

File details

Details for the file neuroglia_python-0.1.7.tar.gz.

File metadata

  • Download URL: neuroglia_python-0.1.7.tar.gz
  • Upload date:
  • Size: 58.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.4 CPython/3.12.1 Darwin/24.6.0

File hashes

Hashes for neuroglia_python-0.1.7.tar.gz
Algorithm Hash digest
SHA256 ba76eb17b057a9a474ec3bd9bcacbb0cba09ba27314139622e98bb69a3c09e34
MD5 c321e196ac860b685e9c71e08e01db1c
BLAKE2b-256 51369945cea04f1255a4598d063613dbff73494e721bd4a627b7e101daf3352f

See more details on using hashes here.

File details

Details for the file neuroglia_python-0.1.7-py3-none-any.whl.

File metadata

  • Download URL: neuroglia_python-0.1.7-py3-none-any.whl
  • Upload date:
  • Size: 83.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.4 CPython/3.12.1 Darwin/24.6.0

File hashes

Hashes for neuroglia_python-0.1.7-py3-none-any.whl
Algorithm Hash digest
SHA256 c301d5274f646b14535afe1fb0219a78a113d48f4e068a0302aa57975126a255
MD5 0c5453557270745387166b04dcc700bd
BLAKE2b-256 dd2cf0447324ae461462e64764618f14b01e0501d2e2cd4fa67b1a093cc2a3c7

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