Created
January 8, 2025 17:42
-
-
Save davepcallan/de32b09fcdbbe28f3e3d90831bf26502 to your computer and use it in GitHub Desktop.
Strategy Pattern using .NET Keyed Services examples (one using strings and the other using an enum for the service key)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.AspNetCore.Mvc; | |
var builder = WebApplication.CreateBuilder(args); | |
// 1. Define the Pricing Strategy Interface | |
public interface ITicketPricingStrategy | |
{ | |
decimal CalculatePrice(int basePrice, int seatTier); | |
} | |
// 2. Implement Two Sport Pricing Strategies | |
public class FootballPricing : ITicketPricingStrategy | |
{ | |
public decimal CalculatePrice(int basePrice, int seatTier) | |
{ | |
return basePrice * (1 + (seatTier * 0.2m)); // Football pricing logic | |
} | |
} | |
public class BasketballPricing : ITicketPricingStrategy | |
{ | |
public decimal CalculatePrice(int basePrice, int seatTier) | |
{ | |
return basePrice * (1 + (seatTier * 0.15m)); // Basketball pricing logic | |
} | |
} | |
// 3. Register the Keyed Services in the DI Container | |
public enum SportType | |
{ | |
Football, | |
Basketball | |
} | |
builder.Services.AddKeyedTransient<ITicketPricingStrategy, FootballPricing>(SportType.Football); | |
builder.Services.AddKeyedTransient<ITicketPricingStrategy, BasketballPricing>(SportType.Basketball); | |
var app = builder.Build(); | |
// 4. Implement a Single Endpoint to Calculate Ticket Prices | |
app.MapGet("/ticket-price", (IServiceProvider serviceProvider, string sport, int basePrice, int seatTier) => | |
{ | |
if (Enum.TryParse<SportType>(sport, true, out var sportType)) | |
{ | |
var pricingStrategy = serviceProvider.GetKeyedService<ITicketPricingStrategy>(sportType); | |
if (pricingStrategy != null) | |
{ | |
var price = pricingStrategy.CalculatePrice(basePrice, seatTier); | |
return Results.Ok(new { Sport = sport, Price = price }); | |
} | |
} | |
return Results.BadRequest("Invalid sport type. Choose Football or Basketball."); | |
}); | |
app.Run(); | |
/* | |
Example Requests: | |
1. Football Ticket - /ticket-price?sport=Football&basePrice=100&seatTier=2 | |
2. Basketball Ticket - /ticket-price?sport=Basketball&basePrice=80&seatTier=1 | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.AspNetCore.Mvc; | |
var builder = WebApplication.CreateBuilder(args); | |
// 1. Define the Pricing Strategy Interface | |
public interface ITicketPricingStrategy | |
{ | |
decimal CalculatePrice(int basePrice, int seatTier); | |
} | |
// 2. Implement Two Sport Pricing Strategies | |
public class FootballPricing : ITicketPricingStrategy | |
{ | |
public decimal CalculatePrice(int basePrice, int seatTier) | |
{ | |
return basePrice * (1 + (seatTier * 0.2m)); // Football pricing logic | |
} | |
} | |
public class BasketballPricing : ITicketPricingStrategy | |
{ | |
public decimal CalculatePrice(int basePrice, int seatTier) | |
{ | |
return basePrice * (1 + (seatTier * 0.15m)); // Basketball pricing logic | |
} | |
} | |
// 3. Register the Keyed Services in the DI Container | |
builder.Services.AddKeyedTransient<ITicketPricingStrategy, FootballPricing>("Football"); | |
builder.Services.AddKeyedTransient<ITicketPricingStrategy, BasketballPricing>("Basketball"); | |
var app = builder.Build(); | |
// 4. Implement a Single Endpoint to Calculate Ticket Prices | |
app.MapGet("/ticket-price", (IServiceProvider serviceProvider, string sport, int basePrice, int seatTier) => | |
{ | |
var pricingStrategy = serviceProvider.GetKeyedService<ITicketPricingStrategy>(sport); | |
if (pricingStrategy != null) | |
{ | |
var price = pricingStrategy.CalculatePrice(basePrice, seatTier); | |
return Results.Ok(new { Sport = sport, Price = price }); | |
} | |
return Results.BadRequest("Invalid sport type. Choose Football or Basketball."); | |
}); | |
app.Run(); | |
/* | |
Example Requests: | |
1. Football Ticket - /ticket-price?sport=Football&basePrice=100&seatTier=2 | |
2. Basketball Ticket - /ticket-price?sport=Basketball&basePrice=80&seatTier=1 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment