How featbit/featbit-dotnet-sdk works
Main Components
• Application using the SDK: Represent a client application (console app or web API) that instantiates the SDK.
• Core SDK Client (FbClient): This is the heart of the system. It mediates configuration, flag evaluation, event handling, and connectivity.
• Options & Configuration: The components built using FbOptions and FbOptionsBuilder, which configure endpoints (streaming, events), logging, offline mode, etc.
• Bootstrapping Module: Contains providers (like JsonBootstrapProvider and NullBootstrapProvider) used to initialize feature flag data when the client starts, particularly in offline mode.
• Data Synchronizer: Module (e.g., WebSocketDataSynchronizer) that manages the connection to the FeatBit server. This component uses a WebSocket (within the Transport namespace) to listen for push updates for feature flags and segments.
• In-Memory Store: The memory store (DefaultMemoryStore and related store abstractions) where the latest flag data is stored and from where the evaluation layer reads.
• Evaluation Engine: A suite of classes (Evaluator, ConditionMatcher, RuleMatcher, SegmentMatcher, etc.) that process incoming flag evaluation requests by consulting the data in the store.
• Events Pipeline: Consisting of event buffer, event dispatcher, event processor, sender, and serializer. This chain captures evaluation events (or custom events like experiments) and sends them to the FeatBit server.
• Transport Layer: Composed of classes in the Transport namespace (such as FbWebSocket, WebSocketTransport, DuplexPipe, etc.) that encapsulate low-level communication details over WebSocket.
• Retry Mechanism: Modules providing retry policies (IRetryPolicy, DefaultRetryPolicy, BackoffAndJitterRetryPolicy) to handle transient network issues.
• Dependency Injection Support: Provided via the DependencyInjection extensions (such as ServiceCollectionExtensions and FbClientHostedService) that integrate the SDK into ASP.NET Core DI frameworks.
• Logging Integration: The SDK integrates with Microsoft.Extensions.Logging so that events and operations can be logged.
Relationship and Interactions
• The client application instantiates the FbClient (configured with FbOptions) and uses it for flag evaluation.
• During initialization, FbClient uses the Bootstrapping module to load an initial snapshot of feature flag data (either from a JSON string in offline mode or via remote retrieval in online mode).
• The Data Synchronizer (using WebSocket Transport) maintains a live connection to the FeatBit server. When changes occur remotely, synchronized updates populate the in-memory store.
• The Evaluation Engine is triggered when the application calls methods such as BoolVariation or BoolVariationDetail. It reads from the in-memory store and applies matching logic (using ConditionMatcher, RuleMatcher, and SegmentMatcher).
• When evaluations or user actions occur, events are generated. These events are captured by the Event Pipeline (event buffer, dispatcher, etc.) and then sent to the specified remote endpoint.
• The Retry and Error-Handling components wrap around the data synchronization and event sending operations to ensure resiliency.
• The Dependency Injection extensions help seamlessly integrate all these components into an ASP.NET Core environment.
Key Architectural Patterns and Principles
• Modularization: The SDK breaks down functionality (bootstrapping, synchronization, evaluation, events) into separate namespaces and modules.
• Separation of Concerns: Each module (data synchronization, evaluation, events, transport) is isolated so that changes in one do not affect the others.
• Abstraction and Extensibility: Interfaces like IFbClient, IBootstrapProvider, IDataSynchronizer, etc. allow for custom implementations (e.g., a custom bootstrap provider or an alternative in-memory store).
• Dependency Injection: The use of extension methods to register services via DI fosters easy integration and testing.
• Resilience: Retry policies and fallback components (for offline mode or null implementations) showcase defensive design.