Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion Ardalis.Specification.sln
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Ardalis.Sample.Domain", "sa
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Ardalis.Sample.App1", "sample\Ardalis.Sample.App1\Ardalis.Sample.App1.csproj", "{EBABFB82-50D0-41A2-AEAC-F0E2C103ED08}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Ardalis.Sample.App2", "sample\Ardalis.Sample.App2\Ardalis.Sample.App2.csproj", "{ECBFDD7F-E60F-48C4-8279-DD6B3B03D27A}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Ardalis.Sample.App3", "sample\Ardalis.Sample.App3\Ardalis.Sample.App3.csproj", "{ECBFDD7F-E60F-48C4-8279-DD6B3B03D27A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Ardalis.Sample.App2", "sample\Ardalis.Sample.App2\Ardalis.Sample.App2.csproj", "{BDEF2EFE-6690-4022-86EF-AF7626366AD0}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -88,6 +90,10 @@ Global
{ECBFDD7F-E60F-48C4-8279-DD6B3B03D27A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{ECBFDD7F-E60F-48C4-8279-DD6B3B03D27A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{ECBFDD7F-E60F-48C4-8279-DD6B3B03D27A}.Release|Any CPU.Build.0 = Release|Any CPU
{BDEF2EFE-6690-4022-86EF-AF7626366AD0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BDEF2EFE-6690-4022-86EF-AF7626366AD0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BDEF2EFE-6690-4022-86EF-AF7626366AD0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BDEF2EFE-6690-4022-86EF-AF7626366AD0}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand All @@ -102,6 +108,7 @@ Global
{4386E123-F4CA-4607-BD8F-8EB11D92458C} = {1FCFDF4F-D0E2-4E30-8ABC-FE6DC3628228}
{EBABFB82-50D0-41A2-AEAC-F0E2C103ED08} = {1FCFDF4F-D0E2-4E30-8ABC-FE6DC3628228}
{ECBFDD7F-E60F-48C4-8279-DD6B3B03D27A} = {1FCFDF4F-D0E2-4E30-8ABC-FE6DC3628228}
{BDEF2EFE-6690-4022-86EF-AF7626366AD0} = {1FCFDF4F-D0E2-4E30-8ABC-FE6DC3628228}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {C153A625-42F7-49A7-B99A-6A78B4B866B2}
Expand Down
63 changes: 13 additions & 50 deletions sample/Ardalis.Sample.App1/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,19 @@
using AutoMapper;
using Microsoft.EntityFrameworkCore;

// Sample Application 1
// This application demonstrates the most basic usage of specifications.
// We're utilizing the provided built-in repositories in this sample
// - Define your IRepository interface and inherit from IRepositoryBase
// - Define your Repository and inherit from RepositoryBase. Pass your concrete DbContext to the base class.
// - Register the interface in DI
// You're good to go!

var builder = WebApplication.CreateBuilder(args);

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

builder.Services.AddAutoMapper(typeof(MappingProfile).Assembly);
builder.Services.AddEndpointsApiExplorer();
Expand All @@ -21,15 +28,15 @@
app.UseHttpsRedirection();


app.MapGet("/customers", async (IReadRepository<Customer> repo, IMapper mapper, CancellationToken cancellationToken) =>
app.MapGet("/customers", async (IRepository<Customer> repo, IMapper mapper, CancellationToken cancellationToken) =>
{
var spec = new CustomerSpec();
var customers = await repo.ListAsync(spec, cancellationToken);
var customersDto = mapper.Map<List<CustomerDto>>(customers);
return Results.Ok(customersDto);
});

app.MapGet("/customers/{id}", async (IReadRepository<Customer> repo, IMapper mapper, int id, CancellationToken cancellationToken) =>
app.MapGet("/customers/{id}", async (IRepository<Customer> repo, IMapper mapper, int id, CancellationToken cancellationToken) =>
{
var spec = new CustomerByIdSpec(id);
var customer = await repo.FirstOrDefaultAsync(spec, cancellationToken);
Expand Down Expand Up @@ -80,25 +87,17 @@ public AppDbContext(DbContextOptions<AppDbContext> options) : base(options)
}
}

public interface IRepository<T> : IRepositoryBase<T> where T : class, IAggregateRoot
{
}
public interface IReadRepository<T> : IReadRepositoryBase<T> where T : class
public interface IRepository<T> : IRepositoryBase<T> where T : class
{
}
public class Repository<T> : RepositoryBase<T>, IRepository<T> where T : class, IAggregateRoot
public class Repository<T> : RepositoryBase<T>, IRepository<T> where T : class
{
public Repository(AppDbContext dbContext) : base(dbContext)
{
}
}
public class ReadRepository<T> : RepositoryBase<T>, IReadRepository<T> where T : class
{
public ReadRepository(AppDbContext dbContext) : base(dbContext)
{
}
}

// AutoMapper configuration
public class MappingProfile : Profile
{
public MappingProfile()
Expand All @@ -107,39 +106,3 @@ public MappingProfile()
CreateMap<Customer, CustomerDto>();
}
}

public static class WebApplicationExtensions
{
public static async Task InitializeDbAsync(this WebApplication app)
{
using var scope = app.Services.CreateScope();
var dbContext = scope.ServiceProvider.GetRequiredService<AppDbContext>();
await dbContext.Database.EnsureDeletedAsync();
await dbContext.Database.EnsureCreatedAsync();
var customers = new List<Customer>()
{
new()
{
Name = "Customer1",
Age = 20,
Addresses = new()
{
new() { Street = "Street1_1" },
new() { Street = "Street1_2" }
}
},
new()
{
Name = "Customer2",
Age = 30,
Addresses = new()
{
new() { Street = "Street2_1" },
new() { Street = "Street3_2" }
}
}
};
dbContext.Customers.AddRange(customers);
await dbContext.SaveChangesAsync();
}
}
37 changes: 37 additions & 0 deletions sample/Ardalis.Sample.App1/WebApplicationExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using Ardalis.Sample.Domain;

public static class WebApplicationExtensions
{
public static async Task InitializeDbAsync(this WebApplication app)
{
using var scope = app.Services.CreateScope();
var dbContext = scope.ServiceProvider.GetRequiredService<AppDbContext>();
await dbContext.Database.EnsureDeletedAsync();
await dbContext.Database.EnsureCreatedAsync();
var customers = new List<Customer>()
{
new()
{
Name = "Customer1",
Age = 20,
Addresses = new()
{
new() { Street = "Street1_1" },
new() { Street = "Street1_2" }
}
},
new()
{
Name = "Customer2",
Age = 30,
Addresses = new()
{
new() { Street = "Street2_1" },
new() { Street = "Street3_2" }
}
}
};
dbContext.Customers.AddRange(customers);
await dbContext.SaveChangesAsync();
}
}
Loading