2017-12-07 7 views
0

필터링 내가 페이징하는 방법을했다 정렬 및 차량의 필터링이 만드는 서비스가 : 내 코드의 리뷰를 검토 한, 페이징을 분리 정렬 내 ASP.NET MVC 응용 프로그램에서

 public class VehicleService : IVehicleService 
{ 
    private readonly DbContext _context; 

    public VehicleService(DbContext context) 
    { 
     _context = context; 
    } 

     public async Task<IPagedList<VehicleMake>> GetVehicleMakesWithPaginationAsync(string search, int? page, string sort) 
    { 
     var makes = _context.VehicleMakes.AsQueryable(); 

     switch (sort) 
     { 
      case "Name desc": 
       makes = makes.OrderByDescending(x => x.Name); 
       break; 
      default: 
       makes = makes.OrderBy(x => x.Name); 
       break; 
     } 

     return await makes.Where(x => x.Name.StartsWith(search) || search == null).ToPagedListAsync(page ?? 1, 5); 
} 
} 

을, 나는이었다 정렬, 필터링 및 페이징은 인터페이스가있는 별도의 클래스에 있어야한다고 말했습니다.

이 정렬 : 나는 다음과 같은 방법으로하는 것이 구현

internal class Sorting : ISorting 
{ 
    private readonly DbContext _context; 

    public Sorting(DbContext context) 
    { 
     _context = context; 
    } 

    public IEnumerable<VehicleMake> SortMakes(string sort) 
    { 
     var makes = _context.VehicleMakes.AsQueryable(); 

     makes = sort == "Name desc" ? makes.OrderByDescending(x => x.Name) : makes.OrderBy(x => x.Name); 
     return makes; 
    } 
} 

페이징 :

class Paging : IPaging 
{ 
    private readonly ISorting _sorting; 

    public Paging(DbContext context) 
    { 
     _sorting = new Sorting(context); 
    } 

    public async Task<IPagedList<VehicleMake>> GetPagedListOfSortedMakes(string search, int? page, string sort) 
    { 
     var sortedMakes = _sorting.SortMakes(sort).AsQueryable(); 
     return await sortedMakes.Where(x => x.Name.StartsWith(search) || search == null).ToPagedListAsync(page ?? 1, 5); 
    } 
} 

다음 내 서비스 :이 잘 작동

public class VehicleMakeService : IVehicleMakeService 
{ 
    private readonly DbContext _context; 
    private readonly IPaging _paging; 

    public VehicleMakeService(DbContext context) 
    { 
     _context = context; 
     _paging = new Paging(context); 
    } 

    public async Task<IPagedList<VehicleMake>> GetVehicleMakesWithPaginationAsync(string search, int? page, string sort) 
    { 
     return await _paging.GetPagedListOfSortedMakes(search, page, sort); 
    } 
} 

, 그러나 나는 imp인지 잘 모르겠다. 이것을 정확하게 바꿨다. 이 작업을 수행하는 더 좋은 (클리너) 방법이 있습니까?

+0

이것은 좋은 질문/코드 검토 자와 같은 것처럼 보입니다. 훌륭한 코드 검토자는 멘토 및 코치 역할을하며 방향 및 추론을 제공 할 수 있습니다. 이제는 개인적인 견해로, 기능을 자체 클래스/인터페이스로 추출해야하는 경우가 있지만, 필자는 이것이 이러한 경우 중 하나라고 확신하지 못합니다. 그렇지 않으면이 방법으로 할 것입니다. 하지만 다시 팀원/건축가/코드 검토 자와상의하십시오. 그리고 행운을 빌어! –

+0

https://codereview.stackexchange.com/에서이 질문을 해보십시오. –

답변

관련 문제