零框架多租户设计方案
This commit is contained in:
1 parent
b7caaa2d0c
commit
b4a704526d
42 files changed
+1002
-1
No files matched your search
+27
@@ -0,0 +1,27 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using MultiTenancySample.DatabaseIsolationService.Entities;
|
||||
using MultiTenancySample.DatabaseIsolationService.EntityFrameworks;
|
||||
|
||||
namespace MultiTenancySample.DatabaseIsolationService.Controllers
|
||||
{
|
||||
[Route("api/{tenant}/[controller]")]
|
||||
[ApiController]
|
||||
public class ProductsController(IDbContextFactory<DatabaseIsolationServiceDbContext> dbContextFactory) : ControllerBase
|
||||
{
|
||||
private readonly DatabaseIsolationServiceDbContext _dbContext = dbContextFactory.CreateDbContext();
|
||||
|
||||
[HttpGet]
|
||||
public IActionResult GetProducts(string? keyword)
|
||||
{
|
||||
IQueryable<Product> products = _dbContext.Set<Product>();
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(keyword))
|
||||
{
|
||||
products = products.Where(p => p.Name.Contains(keyword));
|
||||
}
|
||||
|
||||
return Ok(products);
|
||||
}
|
||||
}
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
namespace MultiTenancySample.DatabaseIsolationService.Entities
|
||||
{
|
||||
public class Product
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
|
||||
public required string Name { get; set; }
|
||||
}
|
||||
}
|
||||
+43
@@ -0,0 +1,43 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using MultiTenancySample.DatabaseIsolationService.Entities;
|
||||
using MultiTenancySample.ServiceDefaults;
|
||||
|
||||
namespace MultiTenancySample.DatabaseIsolationService.EntityFrameworks
|
||||
{
|
||||
public class DataSeeding(IDbContextFactory<DatabaseIsolationServiceDbContext> dbContext, ICurrentTenant currentTenant)
|
||||
{
|
||||
public async Task SeedDataAsync()
|
||||
{
|
||||
currentTenant.SetTenant("Tenant1");
|
||||
|
||||
DatabaseIsolationServiceDbContext dbContext1 = await dbContext.CreateDbContextAsync();
|
||||
|
||||
if (await dbContext1.Database.EnsureCreatedAsync())
|
||||
{
|
||||
await dbContext1.AddRangeAsync(new List<Product> {
|
||||
new() { Id = Guid.NewGuid(), Name = "Product1"},
|
||||
new() { Id = Guid.NewGuid(), Name = "Product3"},
|
||||
new() { Id = Guid.NewGuid(), Name = "Product5"}
|
||||
});
|
||||
|
||||
await dbContext1.SaveChangesAsync();
|
||||
}
|
||||
|
||||
currentTenant.SetTenant("Tenant2");
|
||||
|
||||
DatabaseIsolationServiceDbContext dbContext2 = await dbContext.CreateDbContextAsync();
|
||||
|
||||
if (await dbContext2.Database.EnsureCreatedAsync())
|
||||
{
|
||||
await dbContext2.AddRangeAsync(new List<Product>
|
||||
{
|
||||
new() { Id = Guid.NewGuid(), Name = "Product2"},
|
||||
new() { Id = Guid.NewGuid(), Name = "Product4"},
|
||||
new() { Id = Guid.NewGuid(), Name = "Product6"}
|
||||
});
|
||||
|
||||
await dbContext2.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using MultiTenancySample.DatabaseIsolationService.Entities;
|
||||
using MultiTenancySample.ServiceDefaults;
|
||||
|
||||
|
||||
namespace MultiTenancySample.DatabaseIsolationService.EntityFrameworks
|
||||
{
|
||||
public class DatabaseIsolationServiceDbContext(DbContextOptions<DatabaseIsolationServiceDbContext> options, ICurrentTenant currentTenant, IConfiguration configuration) : DbContext(options)
|
||||
{
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
modelBuilder.Entity<Product>().Property(p => p.Name).HasMaxLength(32);
|
||||
|
||||
base.OnModelCreating(modelBuilder);
|
||||
}
|
||||
|
||||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||||
{
|
||||
if (currentTenant.TenantId != null)
|
||||
{
|
||||
string? connectionString = configuration.GetConnectionString(currentTenant.TenantId);
|
||||
|
||||
optionsBuilder.UseNpgsql(connectionString);
|
||||
}
|
||||
|
||||
base.OnConfiguring(optionsBuilder);
|
||||
}
|
||||
}
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.2" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\MultiTenancySample.ServiceDefaults\MultiTenancySample.ServiceDefaults.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,42 @@
|
||||
using MultiTenancySample.DatabaseIsolationService.EntityFrameworks;
|
||||
using MultiTenancySample.ServiceDefaults;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
// Add services to the container.
|
||||
|
||||
builder.Services.AddControllers();
|
||||
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
builder.Services.AddSwaggerGen();
|
||||
|
||||
builder.Services.AddDbContextFactory<DatabaseIsolationServiceDbContext>(lifetime: ServiceLifetime.Scoped);
|
||||
|
||||
builder.Services.AddScoped<DataSeeding>();
|
||||
|
||||
builder.Services.AddHttpContextAccessor();
|
||||
|
||||
builder.Services.AddMultiTenancy();
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
app.UseMultiTenancy();
|
||||
|
||||
var serviceProvider = app.Services.CreateScope().ServiceProvider;
|
||||
var dataSeeding = serviceProvider.GetRequiredService<DataSeeding>();
|
||||
await dataSeeding.SeedDataAsync();
|
||||
|
||||
// Configure the HTTP request pipeline.
|
||||
if (app.Environment.IsDevelopment())
|
||||
{
|
||||
app.UseSwagger();
|
||||
app.UseSwaggerUI();
|
||||
}
|
||||
|
||||
app.UseHttpsRedirection();
|
||||
|
||||
app.UseAuthorization();
|
||||
|
||||
app.MapControllers();
|
||||
|
||||
app.Run();
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
{
|
||||
"$schema": "http://json.schemastore.org/launchsettings.json",
|
||||
"iisSettings": {
|
||||
"windowsAuthentication": false,
|
||||
"anonymousAuthentication": true,
|
||||
"iisExpress": {
|
||||
"applicationUrl": "http://localhost:62504",
|
||||
"sslPort": 44361
|
||||
}
|
||||
},
|
||||
"profiles": {
|
||||
"http": {
|
||||
"commandName": "Project",
|
||||
"dotnetRunMessages": true,
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "swagger",
|
||||
"applicationUrl": "http://localhost:5220",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
},
|
||||
"https": {
|
||||
"commandName": "Project",
|
||||
"dotnetRunMessages": true,
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "swagger",
|
||||
"applicationUrl": "https://localhost:7102;http://localhost:5220",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
},
|
||||
"IIS Express": {
|
||||
"commandName": "IISExpress",
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "swagger",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
}
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*",
|
||||
"ConnectionStrings": {
|
||||
"Tenant1": "Host=localhost;Port=5432;Database=DatabaseIsolationService1;Username=postgres;Password=postgres",
|
||||
"Tenant2": "Host=localhost;Port=5432;Database=DatabaseIsolationService2;Username=postgres;Password=postgres"
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user