Язык программирования C#9 и платформа .NET5 - Троелсен Эндрю
using AutoLot.Models.Entities;
using AutoLot.Dal.Repos.Base;
namespace AutoLot.Dal.Repos.Interfaces
{
public interface IMakeRepo : IRepo<Make>
{
}
}
Интерфейс хранилища данных о заказах
Откройте файл
IOrderRepo.cs
using
using System.Collections.Generic;
using System.Linq;
using AutoLot.Models.Entities;
using AutoLot.Dal.Repos.Base;
using AutoLot.Models.ViewModels;
Измените интерфейс на
public
IRepo<Order>
namespace AutoLot.Dal.Repos.Interfaces
{
public interface IOrderRepo : IRepo<Order>
{
IQueryable<CustomerOrderViewModel> GetOrdersViewModel();
}
}
Интерфейс на этом завершен, т.к. все необходимые конечные точки API раскрыты в базовом классе.
Реализация классов хранилищ, специфичных для сущностей
Большую часть своей функциональности реализуемые классы хранилищ получают от базового класса. Далее будут описаны функциональные средства, которые добавляются или переопределяют возможности, предлагаемые базовым классом хранилища. Создайте в каталоге
Repos
AutoLot.Dal
CarRepo.cs
CreditRiskRepo.cs
CustomerRepo.cs
MakeRepo.cs
OrderRepo.cs
Классы хранилищ будут реализованы в последующих разделах.
Хранилище данных об автомобилях
Откройте файл класса
CarRepo.cs
using
using 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;
Измените класс на
public
BaseRepo<Car>
ICarRepo
namespace AutoLot.Dal.Repos
{
<b>public</b> class CarRepo : <b>BaseRepo<Car>, ICarRepo</b>
{
}
}
Каждый класс хранилища должен реализовывать два конструктора из
BaseRepo
public CarRepo(ApplicationDbContext context) : base(context)
{
}
internal CarRepo(DbContextOptions<ApplicationDbContext> options)
:
base(options)
{
}
Добавьте переопределенные версии методов
GetAll()
GetAllIgnoreQueryFilters()
MakeNavigation
PetName
public 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()
Make
PetName
public IEnumerable<Car> GetAllBy(int makeId)
{
return Table
.Where(x => x.MakeId == makeId)
.Include(c => c.MakeNavigation)
.OrderBy(c => c.PetName);
}
Добавьте переопределенную версию
Find()
MakeNavigation
public override Car? Find(int? id)
=> Table