Created
September 9, 2024 13:21
-
-
Save ajryan/43554dede141750ed2d1084c4eca0c4b to your computer and use it in GitHub Desktop.
Don't accept IEnumerable
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
public class Vehicle | |
{ | |
public required string Make { get; init; } | |
public required string Model { get; init; } | |
public required int Year { get; init; } | |
public required decimal Price { get; init; } | |
} | |
public class BadVehicleProcessor | |
{ | |
private IEnumerable<Vehicle> vehicles; | |
public BadVehicleProcessor(IEnumerable<Vehicle> vehicles) | |
{ | |
this.vehicles = vehicles; | |
} | |
public Vehicle? GetOldestVehicle() | |
{ | |
return this.vehicles.OrderBy(v => v.Year).FirstOrDefault(); | |
} | |
public Vehicle? GetMostExpensiveVehicle() | |
{ | |
return this.vehicles.OrderByDescending(v => v.Price).FirstOrDefault(); | |
} | |
} | |
public class GoodVehicleProcessor | |
{ | |
private IReadOnlyCollection<Vehicle> vehicles; | |
public BadVehicleProcessor(IReadOnlyCollection<Vehicle> vehicles) | |
{ | |
this.vehicles = vehicles; | |
} | |
// Identical implementation to BadVehicleProcessor | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment