|
| 1 | +This document provides a detailed guide for AI agents to understand and contribute to the Firebase Admin DotNet SDK. Adhering to these guidelines is crucial for maintaining code quality, consistency, and idiomatic C# practices. |
| 2 | + |
| 3 | +## High-Level Overview |
| 4 | + |
| 5 | +The Firebase Admin DotNet SDK provides C# developers with access to Firebase services on privileged environments. Its design emphasizes idiomatic C#, thread-safety, and a consistent, predictable API surface. |
| 6 | + |
| 7 | +## Directory Structure |
| 8 | + |
| 9 | +- **`FirebaseAdmin/FirebaseAdmin.sln`**: The main solution file for the entire project. |
| 10 | +- **`FirebaseAdmin/FirebaseAdmin/`**: The primary source code for the SDK. |
| 11 | + - **`FirebaseAdmin/FirebaseAdmin/FirebaseApp.cs`**: The main entry point and initialization class for the SDK. |
| 12 | + - **`FirebaseAdmin/FirebaseAdmin/Auth/`**: Source code for the Authentication service. |
| 13 | + - **`FirebaseAdmin/FirebaseAdmin/Messaging/`**: Source code for the Firebase Cloud Messaging (FCM) service. |
| 14 | + - **`FirebaseAdmin/FirebaseAdmin/Internal/`**: Internal utilities and classes not meant for public consumption. Direct use of this code in public APIs is strictly forbidden. |
| 15 | +- **`FirebaseAdmin/FirebaseAdmin.Tests/`**: Unit tests for the SDK. |
| 16 | +- **`FirebaseAdmin/FirebaseAdmin.IntegrationTests/`**: Integration tests. |
| 17 | +- **`FirebaseAdmin/FirebaseAdmin.Snippets/`**: Code snippets used in documentation. |
| 18 | + |
| 19 | +## Core Design Patterns |
| 20 | + |
| 21 | +- **Initialization**: The SDK is initialized via the `FirebaseApp.Create()` method, which configures a singleton instance. |
| 22 | +- **Service Clients**: Services like `FirebaseAuth` and `FirebaseMessaging` are accessed through the `FirebaseApp.DefaultInstance`. |
| 23 | +- **Error Handling**: Exceptions are the primary mechanism for error handling. Custom exceptions inherit from `FirebaseException` (defined in `FirebaseAdmin/FirebaseAdmin/FirebaseException.cs`) and are defined within each service module. |
| 24 | +- **HTTP Communication**: All outgoing HTTP requests are managed by a centralized `HttpClient` provided via the `AppOptions.HttpClientFactory`. |
| 25 | +- **Asynchronous Operations**: Asynchronous operations are handled using `Task`-based asynchronous programming (`async`/`await`). |
| 26 | + |
| 27 | +## Coding Style and Naming Conventions |
| 28 | + |
| 29 | +- **Formatting**: Code style is enforced using `stylecop`. Run `dotnet format` to apply the rules. |
| 30 | +- **Naming**: |
| 31 | + - Classes and public methods use `PascalCase`. |
| 32 | + - Private members are not explicitly prefixed, but it is preferred to prefix them with an underscore (`_`). |
| 33 | + - Constants are `PascalCase`. |
| 34 | + |
| 35 | +## Testing Philosophy |
| 36 | + |
| 37 | +- **Unit Tests**: Located in `FirebaseAdmin.Tests/`, with a file naming pattern of `*Test.cs`. `xunit` is the testing framework. Moq is the preferred library for mocking dependencies. |
| 38 | +- **Integration Tests**: Located in `FirebaseAdmin.IntegrationTests/`. These tests interact with live Firebase services and require a configured service account. |
| 39 | + |
| 40 | +## Dependency Management |
| 41 | + |
| 42 | +- **Manager**: Dependencies are managed using NuGet. |
| 43 | +- **Manifest**: The `FirebaseAdmin/FirebaseAdmin/FirebaseAdmin.csproj` file lists all dependencies. |
| 44 | +- **Command**: To add a dependency, use `dotnet add package <PACKAGE_NAME>`. |
| 45 | + |
| 46 | +## Critical Developer Journeys |
| 47 | + |
| 48 | +### Journey 1: How to Add a New API Method |
| 49 | + |
| 50 | +1. **Public Method**: Define the new public method in the relevant service class (e.g., `FirebaseAuth.cs`). |
| 51 | +2. **Internal Logic**: Implement the core logic as a private method. |
| 52 | +3. **HTTP Client**: Use the internal `HttpClient` to make the API call. |
| 53 | +4. **Error Handling**: Wrap API calls in `try-catch` blocks and throw appropriate `FirebaseException` subtypes. |
| 54 | +5. **Testing**: Add a unit test in the corresponding `*Test.cs` file and an integration test in `FirebaseAdmin.IntegrationTests/`. |
| 55 | +6. **Snippet**: Add a code snippet in `FirebaseAdmin.Snippets/` to demonstrate the new feature. |
| 56 | + |
| 57 | +### Journey 2: How to Add a New Field to an Existing API Response |
| 58 | + |
| 59 | +1. **Data Model**: Locate and modify the relevant data model class in the service's directory. |
| 60 | +2. **Testing**: Update unit and integration tests to verify the presence and correctness of the new field. |
| 61 | + |
| 62 | +## Critical Do's and Don'ts |
| 63 | + |
| 64 | +- **DO**: Use the centralized `HttpClient` for all API calls. |
| 65 | +- **DO**: Follow the established `async`/`await` patterns for asynchronous code. |
| 66 | +- **DON'T**: Expose types from the `FirebaseAdmin/FirebaseAdmin/Internal/` directory in any public API. |
| 67 | +- **DON'T**: Introduce new third-party dependencies without a compelling reason and discussion with the maintainers. |
| 68 | + |
| 69 | +## Commit and Pull Request Generation |
| 70 | + |
| 71 | +After implementing and testing a change, you must create a commit and Pull Request following these rules: |
| 72 | + |
| 73 | +**1. Commit Message Format**: The commit message is critical and is used to generate the Pull Request. It MUST follow this structure: |
| 74 | + - **Title**: Use the [Conventional Commits](https://www.conventionalcommits.org/en/v1.0.0/) specification: `type(scope): subject`. |
| 75 | + - `scope` should be the service package changed (e.g., `auth`, `db`, `deps`). |
| 76 | + - **Note on Scopes**: Some services use specific abbreviations. Use the abbreviation if one exists. Common abbreviations include: |
| 77 | + - `messaging` -> `fcm` |
| 78 | + - `dataconnect` -> `fdc` |
| 79 | + - `database` -> `rtdb` |
| 80 | + - `appcheck` -> `fac` |
| 81 | + - Example: `fix(auth): Resolve issue with custom token verification` |
| 82 | + - Example: `feat(fcm): Add support for multicast messages` |
| 83 | + - **Body**: The body is separated from the title by a blank line and MUST contain the following, in order: |
| 84 | + 1. A brief explanation of the problem and the solution. |
| 85 | + 2. A summary of the testing strategy (e.g., "Added a new unit test to verify the fix."). |
| 86 | + 3. A **Context Sources** section that lists the `id` and repository path of every `AGENTS.md` file you used. |
| 87 | + |
| 88 | +**2. Example Commit Message**: |
| 89 | + ``` |
| 90 | + feat(fcm): Add support for multicast messages |
| 91 | +
|
| 92 | + This change introduces a new `SendMulticast` method to the messaging client, allowing developers to send a single message to multiple tokens efficiently. |
| 93 | +
|
| 94 | + Testing: Added unit tests in `messaging_test.cs` with a mock server and an integration test in `integration/messaging_test.cs`. |
| 95 | +
|
| 96 | + Context Sources Used: |
| 97 | + - id: firebase-admin-dotnet (`/AGENTS.md`) |
| 98 | + - id: firebase-admin-dotnet-messaging (`/messaging/AGENTS.md`) |
| 99 | + ``` |
| 100 | + |
| 101 | +**3. Pull Request**: The Pull Request title and description should be populated directly from the commit message's title and body. |
0 commit comments