Skip to content

Commit af90cb4

Browse files
authored
Improved the sample apps. Added additional samples. (#354)
1 parent 444d6f1 commit af90cb4

19 files changed

+402
-129
lines changed

Ardalis.Specification.sln

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Ardalis.Sample.Domain", "sa
4444
EndProject
4545
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Ardalis.Sample.App1", "sample\Ardalis.Sample.App1\Ardalis.Sample.App1.csproj", "{EBABFB82-50D0-41A2-AEAC-F0E2C103ED08}"
4646
EndProject
47-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Ardalis.Sample.App2", "sample\Ardalis.Sample.App2\Ardalis.Sample.App2.csproj", "{ECBFDD7F-E60F-48C4-8279-DD6B3B03D27A}"
47+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Ardalis.Sample.App3", "sample\Ardalis.Sample.App3\Ardalis.Sample.App3.csproj", "{ECBFDD7F-E60F-48C4-8279-DD6B3B03D27A}"
48+
EndProject
49+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ardalis.Sample.App2", "sample\Ardalis.Sample.App2\Ardalis.Sample.App2.csproj", "{BDEF2EFE-6690-4022-86EF-AF7626366AD0}"
4850
EndProject
4951
Global
5052
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -88,6 +90,10 @@ Global
8890
{ECBFDD7F-E60F-48C4-8279-DD6B3B03D27A}.Debug|Any CPU.Build.0 = Debug|Any CPU
8991
{ECBFDD7F-E60F-48C4-8279-DD6B3B03D27A}.Release|Any CPU.ActiveCfg = Release|Any CPU
9092
{ECBFDD7F-E60F-48C4-8279-DD6B3B03D27A}.Release|Any CPU.Build.0 = Release|Any CPU
93+
{BDEF2EFE-6690-4022-86EF-AF7626366AD0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
94+
{BDEF2EFE-6690-4022-86EF-AF7626366AD0}.Debug|Any CPU.Build.0 = Debug|Any CPU
95+
{BDEF2EFE-6690-4022-86EF-AF7626366AD0}.Release|Any CPU.ActiveCfg = Release|Any CPU
96+
{BDEF2EFE-6690-4022-86EF-AF7626366AD0}.Release|Any CPU.Build.0 = Release|Any CPU
9197
EndGlobalSection
9298
GlobalSection(SolutionProperties) = preSolution
9399
HideSolutionNode = FALSE
@@ -102,6 +108,7 @@ Global
102108
{4386E123-F4CA-4607-BD8F-8EB11D92458C} = {1FCFDF4F-D0E2-4E30-8ABC-FE6DC3628228}
103109
{EBABFB82-50D0-41A2-AEAC-F0E2C103ED08} = {1FCFDF4F-D0E2-4E30-8ABC-FE6DC3628228}
104110
{ECBFDD7F-E60F-48C4-8279-DD6B3B03D27A} = {1FCFDF4F-D0E2-4E30-8ABC-FE6DC3628228}
111+
{BDEF2EFE-6690-4022-86EF-AF7626366AD0} = {1FCFDF4F-D0E2-4E30-8ABC-FE6DC3628228}
105112
EndGlobalSection
106113
GlobalSection(ExtensibilityGlobals) = postSolution
107114
SolutionGuid = {C153A625-42F7-49A7-B99A-6A78B4B866B2}

sample/Ardalis.Sample.App1/Program.cs

Lines changed: 13 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,19 @@
55
using AutoMapper;
66
using Microsoft.EntityFrameworkCore;
77

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+
816
var builder = WebApplication.CreateBuilder(args);
917

1018
var connectionString = builder.Configuration.GetConnectionString("DbConnection");
1119
builder.Services.AddDbContext<AppDbContext>(options => options.UseSqlServer(connectionString));
1220
builder.Services.AddScoped(typeof(IRepository<>), typeof(Repository<>));
13-
builder.Services.AddScoped(typeof(IReadRepository<>), typeof(ReadRepository<>));
1421

1522
builder.Services.AddAutoMapper(typeof(MappingProfile).Assembly);
1623
builder.Services.AddEndpointsApiExplorer();
@@ -21,15 +28,15 @@
2128
app.UseHttpsRedirection();
2229

2330

24-
app.MapGet("/customers", async (IReadRepository<Customer> repo, IMapper mapper, CancellationToken cancellationToken) =>
31+
app.MapGet("/customers", async (IRepository<Customer> repo, IMapper mapper, CancellationToken cancellationToken) =>
2532
{
2633
var spec = new CustomerSpec();
2734
var customers = await repo.ListAsync(spec, cancellationToken);
2835
var customersDto = mapper.Map<List<CustomerDto>>(customers);
2936
return Results.Ok(customersDto);
3037
});
3138

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) =>
3340
{
3441
var spec = new CustomerByIdSpec(id);
3542
var customer = await repo.FirstOrDefaultAsync(spec, cancellationToken);
@@ -80,25 +87,17 @@ public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
8087
}
8188
}
8289

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
8791
{
8892
}
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
9094
{
9195
public Repository(AppDbContext dbContext) : base(dbContext)
9296
{
9397
}
9498
}
95-
public class ReadRepository<T> : RepositoryBase<T>, IReadRepository<T> where T : class
96-
{
97-
public ReadRepository(AppDbContext dbContext) : base(dbContext)
98-
{
99-
}
100-
}
10199

100+
// AutoMapper configuration
102101
public class MappingProfile : Profile
103102
{
104103
public MappingProfile()
@@ -107,39 +106,3 @@ public MappingProfile()
107106
CreateMap<Customer, CustomerDto>();
108107
}
109108
}
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-
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using Ardalis.Sample.Domain;
2+
3+
public static class WebApplicationExtensions
4+
{
5+
public static async Task InitializeDbAsync(this WebApplication app)
6+
{
7+
using var scope = app.Services.CreateScope();
8+
var dbContext = scope.ServiceProvider.GetRequiredService<AppDbContext>();
9+
await dbContext.Database.EnsureDeletedAsync();
10+
await dbContext.Database.EnsureCreatedAsync();
11+
var customers = new List<Customer>()
12+
{
13+
new()
14+
{
15+
Name = "Customer1",
16+
Age = 20,
17+
Addresses = new()
18+
{
19+
new() { Street = "Street1_1" },
20+
new() { Street = "Street1_2" }
21+
}
22+
},
23+
new()
24+
{
25+
Name = "Customer2",
26+
Age = 30,
27+
Addresses = new()
28+
{
29+
new() { Street = "Street2_1" },
30+
new() { Street = "Street3_2" }
31+
}
32+
}
33+
};
34+
dbContext.Customers.AddRange(customers);
35+
await dbContext.SaveChangesAsync();
36+
}
37+
}

0 commit comments

Comments
 (0)