1- using System ;
1+ using AutoMapper ;
2+ using AutoMapper . QueryableExtensions ;
3+ using Microsoft . EntityFrameworkCore ;
4+ using System ;
25using System . Collections . Generic ;
36using System . Linq ;
47using System . Threading . Tasks ;
5- using AutoMapper ;
6- using AutoMapper . QueryableExtensions ;
7- using Microsoft . EntityFrameworkCore ;
88
99namespace MockQueryable . Sample
1010{
11- public class MyService
12- {
13- private readonly IUserRepository _userRepository ;
14-
15- public static void Initialize ( )
11+ public class MyService
1612 {
17- Mapper . Initialize ( cfg => cfg . CreateMap < UserEntity , UserReport > ( )
18- . ForMember ( dto => dto . FirstName , conf => conf . MapFrom ( ol => ol . FirstName ) )
19- . ForMember ( dto => dto . LastName , conf => conf . MapFrom ( ol => ol . LastName ) ) ) ;
20- Mapper . Configuration . AssertConfigurationIsValid ( ) ;
13+ private readonly IUserRepository _userRepository ;
14+
15+ public static void Initialize ( )
16+ {
17+ Mapper . Initialize ( cfg => cfg . CreateMap < UserEntity , UserReport > ( )
18+ . ForMember ( dto => dto . FirstName , conf => conf . MapFrom ( ol => ol . FirstName ) )
19+ . ForMember ( dto => dto . LastName , conf => conf . MapFrom ( ol => ol . LastName ) ) ) ;
20+ Mapper . Configuration . AssertConfigurationIsValid ( ) ;
21+ }
22+
23+ public MyService ( IUserRepository userRepository )
24+ {
25+ _userRepository = userRepository ;
26+ }
27+
28+ public async Task CreateUserIfNotExist ( string firstName , string lastName , DateTime dateOfBirth )
29+ {
30+ var query = _userRepository . GetQueryable ( ) ;
31+
32+ if ( await query . AnyAsync ( x => x . LastName == lastName && x . DateOfBirth == dateOfBirth ) )
33+ {
34+ throw new ApplicationException ( "User already exist" ) ;
35+ }
36+
37+ var existUser = await query . FirstOrDefaultAsync ( x => x . FirstName == firstName ) ;
38+ if ( existUser != null )
39+ {
40+ throw new ApplicationException ( "User with FirstName already exist" ) ;
41+ }
42+
43+ if ( await query . CountAsync ( x => x . DateOfBirth == dateOfBirth . Date ) > 3 )
44+ {
45+ throw new ApplicationException ( "Users with DateOfBirth more than limit" ) ;
46+ }
47+
48+ await _userRepository . CreateUser ( new UserEntity
49+ {
50+ FirstName = firstName ,
51+ LastName = lastName ,
52+ DateOfBirth = dateOfBirth . Date ,
53+ } ) ;
54+
55+ }
56+
57+ public async Task < List < UserReport > > GetUserReports ( DateTime dateFrom , DateTime dateTo )
58+ {
59+ var query = _userRepository . GetQueryable ( ) ;
60+
61+ query = query . Where ( x => x . DateOfBirth >= dateFrom . Date ) ;
62+ query = query . Where ( x => x . DateOfBirth <= dateTo . Date ) ;
63+
64+ return await query . Select ( x => new UserReport
65+ {
66+ FirstName = x . FirstName ,
67+ LastName = x . LastName ,
68+ } ) . ToListAsync ( ) ;
69+ }
70+
71+
72+ public async Task < List < UserReport > > GetUserReportsAutoMap ( DateTime dateFrom , DateTime dateTo )
73+ {
74+ var query = _userRepository . GetQueryable ( ) ;
75+
76+ query = query . Where ( x => x . DateOfBirth >= dateFrom . Date ) ;
77+ query = query . Where ( x => x . DateOfBirth <= dateTo . Date ) ;
78+
79+ return await query . ProjectTo < UserReport > ( ) . ToListAsync ( ) ;
80+ }
2181 }
2282
23- public MyService ( IUserRepository userRepository )
83+ public interface IUserRepository
2484 {
25- _userRepository = userRepository ;
26- }
85+ IQueryable < UserEntity > GetQueryable ( ) ;
2786
28- public async Task CreateUserIfNotExist ( string firstName , string lastName , DateTime dateOfBirth )
29- {
30- var query = _userRepository . GetQueryable ( ) ;
31-
32- if ( await query . AnyAsync ( x => x . LastName == lastName && x . DateOfBirth == dateOfBirth ) )
33- {
34- throw new ApplicationException ( "User already exist" ) ;
35- }
36-
37- var existUser = await query . FirstOrDefaultAsync ( x => x . FirstName == firstName ) ;
38- if ( existUser != null )
39- {
40- throw new ApplicationException ( "User with FirstName already exist" ) ;
41- }
42-
43- if ( await query . CountAsync ( x => x . DateOfBirth == dateOfBirth . Date ) > 3 )
44- {
45- throw new ApplicationException ( "Users with DateOfBirth more than limit" ) ;
46- }
47-
48- await _userRepository . CreateUser ( new UserEntity
49- {
50- FirstName = firstName ,
51- LastName = lastName ,
52- DateOfBirth = dateOfBirth . Date ,
53- } ) ;
87+ Task CreateUser ( UserEntity user ) ;
5488
55- }
89+ Task < List < UserEntity > > GetAll ( ) ;
5690
57- public async Task < List < UserReport > > GetUserReports ( DateTime dateFrom , DateTime dateTo )
58- {
59- var query = _userRepository . GetQueryable ( ) ;
91+ IAsyncEnumerable < UserEntity > GetAllAsync ( ) ;
6092
61- query = query . Where ( x => x . DateOfBirth >= dateFrom . Date ) ;
62- query = query . Where ( x => x . DateOfBirth <= dateTo . Date ) ;
93+ Task < int > DeleteUserAsync ( Guid id ) ;
6394
64- return await query . Select ( x => new UserReport
65- {
66- FirstName = x . FirstName ,
67- LastName = x . LastName ,
68- } ) . ToListAsync ( ) ;
95+ Task < int > UpdateFirstNameByIdAsync ( Guid id , string firstName ) ;
6996 }
7097
71-
72- public async Task < List < UserReport > > GetUserReportsAutoMap ( DateTime dateFrom , DateTime dateTo )
98+ public class UserReport
7399 {
74- var query = _userRepository . GetQueryable ( ) ;
75-
76- query = query . Where ( x => x . DateOfBirth >= dateFrom . Date ) ;
77- query = query . Where ( x => x . DateOfBirth <= dateTo . Date ) ;
78-
79- return await query . ProjectTo < UserReport > ( ) . ToListAsync ( ) ;
100+ public string FirstName { get ; set ; }
101+ public string LastName { get ; set ; }
80102 }
81- }
82-
83- public interface IUserRepository
84- {
85- IQueryable < UserEntity > GetQueryable ( ) ;
86-
87- Task CreateUser ( UserEntity user ) ;
88-
89- Task < List < UserEntity > > GetAll ( ) ;
90103
91- IAsyncEnumerable < UserEntity > GetAllAsync ( ) ;
92- }
93-
94-
95- public class UserReport
96- {
97- public string FirstName { get ; set ; }
98- public string LastName { get ; set ; }
99- }
100-
101- public class UserEntity
102- {
103- public Guid Id { get ; set ; }
104- public string FirstName { get ; set ; }
105- public string LastName { get ; set ; }
106- public DateTime DateOfBirth { get ; set ; }
107- }
104+ public class UserEntity
105+ {
106+ public Guid Id { get ; set ; }
107+ public string FirstName { get ; set ; }
108+ public string LastName { get ; set ; }
109+ public DateTime DateOfBirth { get ; set ; }
110+ }
108111}
0 commit comments