public class CarRepository : IVehicleRepository{ public async Task AddAsync(Car car) { // Add car to database using asynchronous database API await _dbContext.Cars.AddAsync(car); await _dbContext.SaveChangesAsync(); } } var carRepo = new CarRepository(); await carRepo.AddAsync(new Car { Make = "Toyota", Model = "Camry", Year = 2021 });
public class BikeRepository : IVehicleRepositoryIn this example, the BikeRepository also implements the IVehicleRepository interface, but provides a different implementation of the AddAsync method for the Bike type. The implementation adds the new Bike object to an in-memory dictionary rather than a database. Package library: Entity Framework Core, for example: Microsoft.EntityFrameworkCore.{ private readonly BikeDbContext _dbContext; public BikeRepository(BikeDbContext dbContext) { _dbContext = dbContext; } public async Task AddAsync(Bike bike) { // Add bike to in-memory dictionary _dbContext.Bikes.Add(bike); await Task.CompletedTask; } } var bikeRepo = new BikeRepository(new BikeDbContext()); await bikeRepo.AddAsync(new Bike { Make = "Schwinn", Model = "Classic Cruiser", Year = 2021 });