Untitled Deck Flashcards
(30 cards)
What design principle does using a configuration object like LenderApiOptions support?
Config-driven design, allowing runtime environment flexibility without code changes.
Why would you define an interface like ILenderApiService in a service-oriented architecture?
To enable mocking, testing, and abstraction from the concrete implementation.
What is the benefit of using a strongly typed return model like MortgageRateResponse?
It ensures type safety, self-documenting code, and easier maintenance downstream.
Why is using HttpClient via dependency injection preferable over creating new instances manually?
It ensures reuse, avoids socket exhaustion, and supports configuration via factory extensions like Polly.
What advantage does structured logging with message templates (e.g., {LenderId}) provide?
Enables efficient log querying, structured diagnostics, and better observability.
Why should CancellationToken be included in async methods?
To allow graceful cancellation of operations, aiding scalability and responsiveness.
What does EnsureSuccessStatusCode() help prevent in HTTP responses?
It forces a fast fail if the HTTP status code is not successful, preventing invalid deserialization.
What benefit does using var response = await … bring in HTTP calls?
It ensures the HttpResponseMessage is properly disposed of to avoid memory leaks.
Why catch HttpRequestException separately in an API call?
To distinguish transient network errors from more critical failures and apply different handling logic.
What’s the purpose of logging both warnings and errors distinctly in service calls?
It helps differentiate between recoverable/transient issues and serious system faults.
How can Polly enhance HTTP client resilience?
By adding retries, circuit breakers, and fallback strategies to handle transient failures.
What’s a clean way to add authentication headers to outgoing HTTP requests?
Use a DelegatingHandler or configure it via HttpClientFactory for automatic token injection.
Why integrate OpenTelemetry spans around external service calls?
To trace requests end-to-end across distributed systems for better observability and debugging.
What risks arise from instantiating HttpClient manually instead of using DI?
Manual instantiation can lead to socket exhaustion due to unclosed connections and missing lifecycle control.
What’s the benefit of separating configuration into an Options class bound via IOptions<T>?</T>
It promotes separation of concerns, centralizes configuration, and simplifies testing.
Why should services avoid directly reading configuration values (e.g., Configuration[“…”]) inline?
It reduces testability and tightens coupling to configuration sources, violating SRP.
Why is it important for services to be interface-backed in unit testing?
To enable mocking/stubbing dependencies and testing behavior in isolation.
What makes a return type like Task<T?> preferable to throwing in some service failures?
It allows downstream services to handle missing data gracefully without exceptions, improving robustness.
What should be considered when using JsonSerializer.Deserialize<T> in production services?</T>
Handle nulls, malformed data, versioning, and potential casing/property name mismatches.
What happens if response.Content.ReadAsStringAsync() is called after the HttpResponseMessage is disposed?
It throws an ObjectDisposedException, since the response stream is no longer accessible.
What’s a common improvement when deserializing JSON in typed models?
Add JsonPropertyName attributes or use a JsonSerializerOptions with naming policies for casing consistency.
Why separate transient errors from fatal ones in error handling logic?
To allow retries or fallbacks on recoverable issues while alerting on critical faults.
How can you prevent a flood of error logs in a failing dependency scenario?
Use rate-limited logging, circuit breakers, or aggregate health checks with thresholds.
When should retry logic not be used with HTTP requests?
When calling non-idempotent endpoints (e.g., POST/PUT that modify state), unless the endpoint is retry-safe.