5
5
using AutoMapper ;
6
6
using Microsoft . EntityFrameworkCore ;
7
7
8
+ // Sample Application 1
9
+ // This application demonstrates the most basic usage of specifications.
10
+ // We're utilizing the provided built-in repositories in this sample
11
+ // - Define your IRepository interface and inherit from IRepositoryBase
12
+ // - Define your Repository and inherit from RepositoryBase. Pass your concrete DbContext to the base class.
13
+ // - Register the interface in DI
14
+ // You're good to go!
15
+
8
16
var builder = WebApplication . CreateBuilder ( args ) ;
9
17
10
18
var connectionString = builder . Configuration . GetConnectionString ( "DbConnection" ) ;
11
19
builder . Services . AddDbContext < AppDbContext > ( options => options . UseSqlServer ( connectionString ) ) ;
12
20
builder . Services . AddScoped ( typeof ( IRepository < > ) , typeof ( Repository < > ) ) ;
13
- builder . Services . AddScoped ( typeof ( IReadRepository < > ) , typeof ( ReadRepository < > ) ) ;
14
21
15
22
builder . Services . AddAutoMapper ( typeof ( MappingProfile ) . Assembly ) ;
16
23
builder . Services . AddEndpointsApiExplorer ( ) ;
21
28
app . UseHttpsRedirection ( ) ;
22
29
23
30
24
- app . MapGet ( "/customers" , async ( IReadRepository < Customer > repo , IMapper mapper , CancellationToken cancellationToken ) =>
31
+ app . MapGet ( "/customers" , async ( IRepository < Customer > repo , IMapper mapper , CancellationToken cancellationToken ) =>
25
32
{
26
33
var spec = new CustomerSpec ( ) ;
27
34
var customers = await repo . ListAsync ( spec , cancellationToken ) ;
28
35
var customersDto = mapper . Map < List < CustomerDto > > ( customers ) ;
29
36
return Results . Ok ( customersDto ) ;
30
37
} ) ;
31
38
32
- app . MapGet ( "/customers/{id}" , async ( IReadRepository < Customer > repo , IMapper mapper , int id , CancellationToken cancellationToken ) =>
39
+ app . MapGet ( "/customers/{id}" , async ( IRepository < Customer > repo , IMapper mapper , int id , CancellationToken cancellationToken ) =>
33
40
{
34
41
var spec = new CustomerByIdSpec ( id ) ;
35
42
var customer = await repo . FirstOrDefaultAsync ( spec , cancellationToken ) ;
@@ -80,25 +87,17 @@ public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
80
87
}
81
88
}
82
89
83
- public interface IRepository < T > : IRepositoryBase < T > where T : class , IAggregateRoot
84
- {
85
- }
86
- public interface IReadRepository < T > : IReadRepositoryBase < T > where T : class
90
+ public interface IRepository < T > : IRepositoryBase < T > where T : class
87
91
{
88
92
}
89
- public class Repository < T > : RepositoryBase < T > , IRepository < T > where T : class , IAggregateRoot
93
+ public class Repository < T > : RepositoryBase < T > , IRepository < T > where T : class
90
94
{
91
95
public Repository ( AppDbContext dbContext ) : base ( dbContext )
92
96
{
93
97
}
94
98
}
95
- public class ReadRepository < T > : RepositoryBase < T > , IReadRepository < T > where T : class
96
- {
97
- public ReadRepository ( AppDbContext dbContext ) : base ( dbContext )
98
- {
99
- }
100
- }
101
99
100
+ // AutoMapper configuration
102
101
public class MappingProfile : Profile
103
102
{
104
103
public MappingProfile ( )
@@ -107,39 +106,3 @@ public MappingProfile()
107
106
CreateMap < Customer , CustomerDto > ( ) ;
108
107
}
109
108
}
110
-
111
- public static class WebApplicationExtensions
112
- {
113
- public static async Task InitializeDbAsync ( this WebApplication app )
114
- {
115
- using var scope = app . Services . CreateScope ( ) ;
116
- var dbContext = scope . ServiceProvider . GetRequiredService < AppDbContext > ( ) ;
117
- await dbContext . Database . EnsureDeletedAsync ( ) ;
118
- await dbContext . Database . EnsureCreatedAsync ( ) ;
119
- var customers = new List < Customer > ( )
120
- {
121
- new ( )
122
- {
123
- Name = "Customer1" ,
124
- Age = 20 ,
125
- Addresses = new ( )
126
- {
127
- new ( ) { Street = "Street1_1" } ,
128
- new ( ) { Street = "Street1_2" }
129
- }
130
- } ,
131
- new ( )
132
- {
133
- Name = "Customer2" ,
134
- Age = 30 ,
135
- Addresses = new ( )
136
- {
137
- new ( ) { Street = "Street2_1" } ,
138
- new ( ) { Street = "Street3_2" }
139
- }
140
- }
141
- } ;
142
- dbContext . Customers . AddRange ( customers ) ;
143
- await dbContext . SaveChangesAsync ( ) ;
144
- }
145
- }
0 commit comments