Язык программирования C#9 и платформа .NET5 - Троелсен Эндрю
using AutoLot.Models.Entities;using AutoLot.Dal.Repos.Base;namespace AutoLot.Dal.Repos.Interfaces{ public interface IMakeRepo : IRepo<Make> { }}Интерфейс хранилища данных о заказах
Откройте файл
IOrderRepo.csusingusing System.Collections.Generic;using System.Linq;using AutoLot.Models.Entities;using AutoLot.Dal.Repos.Base;using AutoLot.Models.ViewModels;Измените интерфейс на
publicIRepo<Order>namespace AutoLot.Dal.Repos.Interfaces{ public interface IOrderRepo : IRepo<Order> { IQueryable<CustomerOrderViewModel> GetOrdersViewModel(); }}Интерфейс на этом завершен, т.к. все необходимые конечные точки API раскрыты в базовом классе.
Реализация классов хранилищ, специфичных для сущностей
Большую часть своей функциональности реализуемые классы хранилищ получают от базового класса. Далее будут описаны функциональные средства, которые добавляются или переопределяют возможности, предлагаемые базовым классом хранилища. Создайте в каталоге
ReposAutoLot.DalCarRepo.csCreditRiskRepo.csCustomerRepo.csMakeRepo.csOrderRepo.csКлассы хранилищ будут реализованы в последующих разделах.
Хранилище данных об автомобилях
Откройте файл класса
CarRepo.csusingusing System.Collections.Generic;using System.Data;using System.Linq;using AutoLot.Dal.EfStructures;using AutoLot.Models.Entities;using AutoLot.Dal.Repos.Base;using AutoLot.Dal.Repos.Interfaces;using Microsoft.Data.SqlClient;using Microsoft.EntityFrameworkCore;Измените класс на
publicBaseRepo<Car>ICarReponamespace AutoLot.Dal.Repos{ <b>public</b> class CarRepo : <b>BaseRepo<Car>, ICarRepo</b> { }}Каждый класс хранилища должен реализовывать два конструктора из
BaseRepopublic CarRepo(ApplicationDbContext context) : base(context){}internal CarRepo(DbContextOptions<ApplicationDbContext> options) : base(options){}Добавьте переопределенные версии методов
GetAll()GetAllIgnoreQueryFilters()MakeNavigationPetNamepublic override IEnumerable<Car> GetAll() => Table .Include(c => c.MakeNavigation) .OrderBy(o => o.PetName);public override IEnumerable<Car> GetAllIgnoreQueryFilters() => Table .Include(c => c.MakeNavigation) .OrderBy(o => o.PetName) .IgnoreQueryFilters();Реализуйте метод
GetAllBy()MakePetNamepublic IEnumerable<Car> GetAllBy(int makeId){ return Table .Where(x => x.MakeId == makeId) .Include(c => c.MakeNavigation) .OrderBy(c => c.PetName);}Добавьте переопределенную версию
Find()MakeNavigationpublic override Car? Find(int? id) => Table