Core
Namespace: RA.Utilities.Core
The the projects within the Core solution folder (RA.Utilities.Core, RA.Utilities.Core.Constants, and RA.Utilities.Core.Exceptions) serve as the foundational, innermost layer of your Clean Architecture.
They are designed to have zero dependencies on any other layer in your solution, which is a critical principle for creating a decoupled and maintainable system.
Here’s how they support the rest of the solution:
1. RA.Utilities.Core - The Heart of Business Logic Communication
This package contains the fundamental building blocks for your application's logic.
- Purpose: It provides a way to handle expected operational outcomes (both success and failure) without resorting to throwing exceptions. This is a powerful pattern for distinguishing between predictable failures (e.g., "user not found," "invalid input") and true, unexpected errors.
- How it Supports Other Layers:
- Application Layer (
RA.Utilities.Feature): Your business logic (CQRS handlers) will create and returnResult<T>objects. This allows a handler to clearly communicate "The operation succeeded and here is the data" or "The operation failed for this specific business reason." - API Layer (
RA.Utilities.Api): The API endpoints receive theResult<T>object from the application layer. As shown The endpoint can then use the.Match()method to transform the result into the appropriate HTTP response (200OK on success, or a400 Bad Request/404 Not Foundon a predictable failure). This cleanly separates business logic from API concerns.
- Application Layer (
2. RA.Utilities.Core.Exceptions - Standardizing Unexpected Errors
This package defines a set of custom, semantic exceptions that represent exceptional, unrecoverable application states.
- Purpose: To create a standardized vocabulary for exceptional events. When your application logic encounters a situation it cannot recover from (e.g., trying to retrieve an entity that must exist but doesn't), it can throw a specific exception like
NotFoundExceptionorConflictException. - How it Supports Other Layers:
- Application & Data Layers: These layers can throw these exceptions without needing any knowledge of HTTP status codes. For example, a repository can throw
NotFoundException("Product", 123)if a product with that ID is not found. - API Layer (
RA.Utilities.Api): TheGlobalExceptionHandlermiddleware in this layer is specifically designed to catch these custom exceptions. It acts as a translator, converting aNotFoundExceptioninto an HTTP404 Not Foundresponse, and a ConflictException into an HTTP409 Conflict. This is a perfect example of the API layer adapting core business exceptions into web-specific responses.
- Application & Data Layers: These layers can throw these exceptions without needing any knowledge of HTTP status codes. For example, a repository can throw
3. RA.Utilities.Core.Constants - Eliminating Magic Strings
This is the simplest but arguably one of the most important packages for maintaining consistency.
- Purpose: It centralizes shared, static values like configuration keys, header names (
X-Request-Id), or policy names. - How it Supports Other Layers: Any project that needs to reference a common value can depend on this package. For example:
- The
DefaultHeadersMiddlewareinRA.Utilities.Api.Middlewaresneeds to know the exact string for theX-Request-Idheader. Instead of hardcoding it, it can reference a constant from this package. - If you have specific claim types or authorization policy names, they would live here and be used by both the RA.Utilities.Authorization package (to define the policy) and the application layer (to require the policy).
- The
Summary
In essence, the Core packages form the stable, central foundation of your entire ecosystem. They enforce the Dependency Rule of Clean Architecture: all dependencies flow inwards.
Api,Data, andInfrastructureall depend onCore.Coredepends on nothing.
This structure makes your solution incredibly robust, testable, and easy to reason about. Your business logic remains pure and independent of external details like databases, web frameworks, or logging providers.
🗃️ RA.Utilities.Core
2 items
🗃️ RA.Utilities.Core.Constants
4 items
🗃️ RA.Utilities.Core.Exceptions
5 items