Skip to content

Commit 8f81186

Browse files
committed
Consolidating methods and using projections
1 parent 828f576 commit 8f81186

File tree

4 files changed

+15
-72
lines changed

4 files changed

+15
-72
lines changed

ContosoUniversity/Features/Instructors/CreateEdit.cs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,7 @@ public class QueryHandler : IAsyncRequestHandler<Query, Command>
8484
{
8585
private readonly SchoolContext _db;
8686

87-
public QueryHandler(SchoolContext db)
88-
{
89-
_db = db;
90-
}
87+
public QueryHandler(SchoolContext db) => _db = db;
9188

9289
public async Task<Command> Handle(Query message)
9390
{
@@ -101,9 +98,8 @@ public async Task<Command> Handle(Query message)
10198
model = await _db.Instructors
10299
.Include(m => m.CourseAssignments)
103100
.ThenInclude(ca => ca.Course)
104-
.Include(m => m.OfficeAssignment)
105101
.Where(i => i.Id == message.Id)
106-
.Map()
102+
.ProjectTo<Command>()
107103
.SingleOrDefaultAsync<Command>();
108104

109105
//.ProjectTo<Command>()
@@ -134,10 +130,7 @@ public class CommandHandler : IAsyncRequestHandler<Command, int>
134130
{
135131
private readonly SchoolContext _db;
136132

137-
public CommandHandler(SchoolContext db)
138-
{
139-
_db = db;
140-
}
133+
public CommandHandler(SchoolContext db) => _db = db;
141134

142135
public async Task<int> Handle(Command message)
143136
{

ContosoUniversity/Features/Instructors/Index.cs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,23 +72,19 @@ public class Handler : IAsyncRequestHandler<Query, Model>
7272
{
7373
private readonly SchoolContext _db;
7474

75-
public Handler(SchoolContext db)
76-
{
77-
_db = db;
78-
}
75+
public Handler(SchoolContext db) => _db = db;
7976

8077
public async Task<Model> Handle(Query message)
8178
{
8279
var instructors = await _db.Instructors
83-
.Include(i => i.OfficeAssignment)
8480
.Include(i => i.CourseAssignments)
8581
.ThenInclude(c => c.Course)
8682
.OrderBy(i => i.LastName)
87-
.Map()
88-
.ToListAsync<Model.Instructor>()
83+
.ProjectTo<Model.Instructor>()
84+
.ToListAsync()
8985
;
9086

91-
// EF Core cannot project child collections
87+
// EF Core cannot project child collections w/o Include
9288
// See https://github.com/aspnet/EntityFrameworkCore/issues/9128
9389
//var instructors = await _db.Instructors
9490
// .OrderBy(i => i.LastName)
Lines changed: 3 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,11 @@
1-
using System.Collections.Generic;
2-
using System.Linq;
1+
using System.Linq;
32
using System.Threading.Tasks;
4-
using AutoMapper;
5-
using AutoMapper.QueryableExtensions;
6-
using Microsoft.EntityFrameworkCore;
73

84
namespace ContosoUniversity.Features
95
{
106
public static class MappingExtensions
117
{
12-
public static ICollectionMapperExpression Map<TSource>(this IEnumerable<TSource> items)
13-
=> new CollectionMapperExpression<TSource>(items);
14-
15-
public static IQueryableExpression Map<TSource>(this IQueryable<TSource> items)
16-
=> new QueryableExpression<TSource>(items);
17-
18-
private class CollectionMapperExpression<TSource> : ICollectionMapperExpression
19-
{
20-
private readonly IEnumerable<TSource> _items;
21-
22-
public CollectionMapperExpression(IEnumerable<TSource> items)
23-
=> _items = items;
24-
25-
public IList<TDestination> ToList<TDestination>() => _items.Select(Mapper.Map<TSource, TDestination>).ToList();
26-
}
27-
28-
private class QueryableExpression<TSource> : IQueryableExpression
29-
{
30-
private readonly IQueryable<TSource> _items;
31-
32-
public QueryableExpression(IQueryable<TSource> items)
33-
=> _items = items;
34-
35-
public async Task<IList<TDestination>> ToListAsync<TDestination>()
36-
=> (await _items.ToListAsync()).Select(src => Mapper.Map<TSource, TDestination>(src)).ToList();
37-
38-
public Task<PaginatedList<TDestination>> ToPaginatedListAsync<TDestination>(int pageNumber, int pageSize)
39-
=> PaginatedList<TDestination>.CreateAsync(_items.ProjectTo<TDestination>(), pageNumber, pageSize);
40-
41-
public async Task<TDestination> SingleOrDefaultAsync<TDestination>()
42-
=> Mapper.Map<TSource, TDestination>(await _items.SingleOrDefaultAsync());
43-
}
44-
}
45-
46-
public interface ICollectionMapperExpression
47-
{
48-
IList<TDestination> ToList<TDestination>();
49-
}
50-
51-
public interface IQueryableExpression
52-
{
53-
Task<IList<TDestination>> ToListAsync<TDestination>();
54-
Task<TDestination> SingleOrDefaultAsync<TDestination>();
55-
Task<PaginatedList<TDestination>> ToPaginatedListAsync<TDestination>(int pageNumber, int pageSize);
8+
public static Task<PaginatedList<TDestination>> PaginatedListAsync<TDestination>(this IQueryable<TDestination> queryable, int pageNumber, int pageSize)
9+
=> PaginatedList<TDestination>.CreateAsync(queryable, pageNumber, pageSize);
5610
}
5711
}

ContosoUniversity/Features/Students/Index.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.ComponentModel.DataAnnotations;
33
using System.Linq;
44
using System.Threading.Tasks;
5+
using AutoMapper.QueryableExtensions;
56
using ContosoUniversity.Data;
67
using ContosoUniversity.Models;
78
using MediatR;
@@ -42,10 +43,7 @@ public class QueryHandler : IAsyncRequestHandler<Query, Result>
4243
{
4344
private readonly SchoolContext _db;
4445

45-
public QueryHandler(SchoolContext db)
46-
{
47-
_db = db;
48-
}
46+
public QueryHandler(SchoolContext db) => _db = db;
4947

5048
public async Task<Result> Handle(Query message)
5149
{
@@ -92,7 +90,9 @@ public async Task<Result> Handle(Query message)
9290

9391
int pageSize = 3;
9492
int pageNumber = (message.Page ?? 1);
95-
model.Results = await students.Map().ToPaginatedListAsync<Model>(pageNumber, pageSize);
93+
model.Results = await students
94+
.ProjectTo<Model>()
95+
.PaginatedListAsync(pageNumber, pageSize);
9696

9797
return model;
9898
}

0 commit comments

Comments
 (0)