Domain Services¶
Domain Services encapsulate business logic that doesn't naturally fit within entities or value objects. They orchestrate complex operations across multiple domain objects.
MergeService¶
MergeService ¶
MergeService(biorempp_repo: DatabaseRepository, kegg_repo: DatabaseRepository, hadeg_repo: DatabaseRepository, toxcsm_repo: DatabaseRepository)
Domain service to orchestrate merges with databases.
Coordinates the process of merging the input dataset with the 4 system databases: BioRemPP, KEGG, HADEG, and ToxCSM.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
biorempp_repo | DatabaseRepository | Repository for the BioRemPP database | required |
kegg_repo | DatabaseRepository | Repository for the KEGG database | required |
hadeg_repo | DatabaseRepository | Repository for the HADEG database | required |
toxcsm_repo | DatabaseRepository | Repository for the ToxCSM database | required |
Notes
This service depends on repositories that will be injected, following the Dependency Inversion Principle (SOLID).
Initialize the service with the necessary repositories.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
biorempp_repo | DatabaseRepository | BioRemPP repository | required |
kegg_repo | DatabaseRepository | KEGG repository | required |
hadeg_repo | DatabaseRepository | HADEG repository | required |
toxcsm_repo | DatabaseRepository | ToxCSM repository | required |
Source code in src/domain/services/merge_service.py
Functions¶
merge_all ¶
Execute all merges sequentially.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dataset | Dataset | Input dataset with samples and KOs | required |
Returns:
| Type | Description |
|---|---|
MergedData | Entity with all merge results |
Raises:
| Type | Description |
|---|---|
ValueError | If any mandatory merge fails |
Notes
The process follows this order: 1. Merge with BioRemPP (mandatory) 2. Merge with KEGG (mandatory) 3. Merge with HADEG (mandatory) 4. Merge with ToxCSM (optional, depends on compounds)
Source code in src/domain/services/merge_service.py
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 | |
merge_biorempp ¶
Execute only the merge with BioRemPP.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dataset | Dataset | Input dataset | required |
Returns:
| Type | Description |
|---|---|
Dict[str, Any] | Data merged with BioRemPP |
Notes
Useful for partial or incremental processing.
Source code in src/domain/services/merge_service.py
get_merge_statistics ¶
Calculate statistics about the merges performed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
merged_data | MergedData | Merged data | required |
Returns:
| Type | Description |
|---|---|
Dict[str, Any] | Merge statistics |
Source code in src/domain/services/merge_service.py
ValidationService¶
ValidationService ¶
Domain service for complex validations.
Implements validation rules that involve multiple entities or business logic that does not belong to a specific entity.
Notes
This is a Domain Service that encapsulates complex validation logic, keeping the entities simple and focused. All methods are static and stateless.
Functions¶
validate_raw_input staticmethod ¶
Validate raw content from sample upload.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
content | str | Content of the samples file in BioRemPP format | required |
Returns:
| Type | Description |
|---|---|
Tuple[bool, str] | Tuple (is_valid, error_message) where is_valid is True if the content is valid, and error_message contains the error description if any |
Notes
Expected format: - Lines starting with '>' indicate the start of a new sample - Lines starting with 'K' are KO entries
Source code in src/domain/services/validation_service.py
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 | |
validate_dataset staticmethod ¶
Validate a complete dataset.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dataset | Dataset | Dataset to be validated | required |
Returns:
| Type | Description |
|---|---|
Tuple[bool, str] | Tuple (is_valid, error_message) |
Notes
Validates both the dataset structure and each sample individually, ensuring complete consistency.
Source code in src/domain/services/validation_service.py
validate_ko_list staticmethod ¶
Validate and convert a list of strings to a list of KOs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ko_list | List[str] | List of strings representing KOs | required |
Returns:
| Type | Description |
|---|---|
Tuple[bool, str, List[KO]] | Tuple (is_valid, error_message, ko_objects) where ko_objects contains the validated KOs if successful |
Notes
This method is useful for validating user inputs before creating domain entities.
Source code in src/domain/services/validation_service.py
check_duplicate_samples staticmethod ¶
Check for duplicate samples in the dataset.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dataset | Dataset | Dataset to be checked | required |
Returns:
| Type | Description |
|---|---|
Tuple[bool, List[str]] | Tuple (has_duplicates, duplicate_ids) where has_duplicates is True if there are duplicates, and duplicate_ids contains the duplicate IDs |
Notes
Duplicate samples may indicate an error in the input file or incorrect processing.
Source code in src/domain/services/validation_service.py
validate_file_size staticmethod ¶
Validate file size against maximum limit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
size_bytes | int | File size in bytes | required |
max_bytes | int | Maximum allowed size in bytes | required |
Returns:
| Type | Description |
|---|---|
Tuple[bool, str] | Tuple (is_valid, error_message) |
Notes
Provides user-friendly error messages with sizes in MB. Logs validation failures with detailed context.
Source code in src/domain/services/validation_service.py
validate_sample_count staticmethod ¶
Validate number of samples against maximum limit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sample_count | int | Number of samples in dataset | required |
max_samples | int | Maximum allowed samples | required |
Returns:
| Type | Description |
|---|---|
Tuple[bool, str] | Tuple (is_valid, error_message) |
Notes
Provides clear error messages when limit is exceeded. Logs validation context for debugging.
Source code in src/domain/services/validation_service.py
validate_ko_count staticmethod ¶
Validate number of KO entries against maximum limit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ko_count | int | Number of KO entries in dataset | required |
max_kos | int | Maximum allowed KO entries | required |
Returns:
| Type | Description |
|---|---|
Tuple[bool, str] | Tuple (is_valid, error_message) |
Notes
Formats large numbers with commas for readability. Logs detailed context for validation failures.
Source code in src/domain/services/validation_service.py
validate_encoding staticmethod ¶
Validate and decode file content encoding.
Attempts UTF-8 decoding first, falls back to latin-1 if needed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
content_bytes | bytes | Raw file content | required |
Returns:
| Type | Description |
|---|---|
Tuple[bool, str, str] | Tuple (is_valid, decoded_content, error_message) - is_valid: True if decoding succeeded - decoded_content: Decoded string (empty if failed) - error_message: Error description (empty if successful) |
Notes
Encoding priority: 1. UTF-8 (preferred) 2. Latin-1 (fallback)
Logs warnings when fallback encoding is used. Returns user-friendly error messages.
Source code in src/domain/services/validation_service.py
Related Documentation¶
- Domain Entities - Business objects
Domain Service Patterns¶
When to Use Domain Services¶
Use domain services when:
- Multi-Entity Operations: Logic spans multiple aggregates
- Complex Business Rules: Too complex for a single entity
- External Dependencies: Requires external data or calculations
- Stateless Operations: No state to maintain