-
Notifications
You must be signed in to change notification settings - Fork 15
Open
Labels
.NETPull requests that update .NET codePull requests that update .NET codeenhancementNew feature or requestNew feature or requestgood first issueGood for newcomersGood for newcomers
Description
Description
Refactor PlayerService to use the ASP.NET Core recommended IHostEnvironment interface for environment detection instead of directly accessing environment variables via Environment.GetEnvironmentVariable.
Current Implementation
PlayerService currently checks the environment using:
if (Environment.GetEnvironmentVariable(AspNetCore_Environment) == Development)
{
await SimulateRepositoryDelayAsync();
}This approach:
- Uses string-based comparison (prone to typos)
- Requires manual constant definitions for environment variable name and value
- Is less idiomatic for ASP.NET Core applications
Proposed solution
Inject IHostEnvironment and use the built-in helper method:
public class PlayerService(
IPlayerRepository playerRepository,
ILogger<PlayerService> logger,
IMemoryCache memoryCache,
IMapper mapper,
IHostEnvironment environment // Add this
) : IPlayerService
{
// Replace line 71 with:
if (environment.IsDevelopment())
{
await SimulateRepositoryDelayAsync();
}
}Benefits
- ✅ Type-safe: Uses strongly-typed methods instead of string comparisons
- ✅ Cleaner code: Removes need for
AspNetCore_EnvironmentandDevelopmentconstants - ✅ Consistent: Aligns with
ExceptionMiddlewareimplementation (see Add custom middleware for global exception handling #184) - ✅ Best practice: Follows ASP.NET Core recommended patterns
- ✅ Built-in helpers: Access to
IsDevelopment(),IsProduction(),IsStaging()
Acceptance Criteria
- Inject
IHostEnvironmentintoPlayerServiceconstructor - Replace
Environment.GetEnvironmentVariable(AspNetCore_Environment) == Developmentwithenvironment.IsDevelopment() - Remove unused constants:
AspNetCore_EnvironmentandDevelopment - Verify all existing unit tests still pass
- Update
PlayerServiceTestsif needed to mockIHostEnvironment
References
- 🔗 Use multiple environments in ASP.NET Core
- 🔗 Related to Add custom middleware for global exception handling #184 (ExceptionMiddleware uses
IHostEnvironment.IsDevelopment())
Labels
enhancement- Code improvementrefactor- Code refactoringgood first issue- Straightforward refactoring task.NET- .NET code changes
Metadata
Metadata
Assignees
Labels
.NETPull requests that update .NET codePull requests that update .NET codeenhancementNew feature or requestNew feature or requestgood first issueGood for newcomersGood for newcomers