多租户应用程序设计
This commit is contained in:
parent
dbabaaadba
commit
49f2796cc2
@ -1,4 +1,7 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
// Copyright (c) HelloShop Corporation. All rights reserved.
|
||||
// See the license file in the project root for more information.
|
||||
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using MultiTenancySample.DatabaseIsolationService.Entities;
|
||||
using MultiTenancySample.DatabaseIsolationService.EntityFrameworks;
|
||||
@ -23,5 +26,14 @@ namespace MultiTenancySample.DatabaseIsolationService.Controllers
|
||||
|
||||
return Ok(products);
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> CreateProduct(Product product)
|
||||
{
|
||||
await _dbContext.AddAsync(product);
|
||||
await _dbContext.SaveChangesAsync();
|
||||
|
||||
return Ok(product);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,7 @@
|
||||
namespace MultiTenancySample.DatabaseIsolationService.Entities
|
||||
// Copyright (c) HelloShop Corporation. All rights reserved.
|
||||
// See the license file in the project root for more information.
|
||||
|
||||
namespace MultiTenancySample.DatabaseIsolationService.Entities
|
||||
{
|
||||
public class Product
|
||||
{
|
||||
|
@ -1,4 +1,7 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
// Copyright (c) HelloShop Corporation. All rights reserved.
|
||||
// See the license file in the project root for more information.
|
||||
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using MultiTenancySample.DatabaseIsolationService.Entities;
|
||||
using MultiTenancySample.ServiceDefaults;
|
||||
|
||||
|
@ -1,4 +1,7 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
// Copyright (c) HelloShop Corporation. All rights reserved.
|
||||
// See the license file in the project root for more information.
|
||||
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using MultiTenancySample.DatabaseIsolationService.Entities;
|
||||
using MultiTenancySample.ServiceDefaults;
|
||||
|
||||
|
@ -1,3 +1,6 @@
|
||||
// Copyright (c) HelloShop Corporation. All rights reserved.
|
||||
// See the license file in the project root for more information.
|
||||
|
||||
using MultiTenancySample.DatabaseIsolationService.EntityFrameworks;
|
||||
using MultiTenancySample.ServiceDefaults;
|
||||
|
||||
|
@ -1,4 +1,7 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
// Copyright (c) HelloShop Corporation. All rights reserved.
|
||||
// See the license file in the project root for more information.
|
||||
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using MultiTenancySample.FieldIsolationService.Entities;
|
||||
using MultiTenancySample.FieldIsolationService.EntityFrameworks;
|
||||
@ -23,5 +26,15 @@ namespace MultiTenancySample.FieldIsolationService.Controllers
|
||||
|
||||
return Ok(products);
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public async Task<IActionResult> CreateProduct(Product product)
|
||||
{
|
||||
await _dbContext.AddAsync(product);
|
||||
|
||||
await _dbContext.SaveChangesAsync();
|
||||
|
||||
return Ok(product);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,7 @@
|
||||
namespace MultiTenancySample.FieldIsolationService.Entities
|
||||
// Copyright (c) HelloShop Corporation. All rights reserved.
|
||||
// See the license file in the project root for more information.
|
||||
|
||||
namespace MultiTenancySample.FieldIsolationService.Entities
|
||||
{
|
||||
public interface IMultiTenant
|
||||
{
|
||||
|
@ -1,4 +1,7 @@
|
||||
using System.Text.Json.Serialization;
|
||||
// Copyright (c) HelloShop Corporation. All rights reserved.
|
||||
// See the license file in the project root for more information.
|
||||
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace MultiTenancySample.FieldIsolationService.Entities
|
||||
{
|
||||
|
@ -1,16 +1,19 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
// Copyright (c) HelloShop Corporation. All rights reserved.
|
||||
// See the license file in the project root for more information.
|
||||
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using MultiTenancySample.FieldIsolationService.Entities;
|
||||
using MultiTenancySample.ServiceDefaults;
|
||||
|
||||
namespace MultiTenancySample.FieldIsolationService.EntityFrameworks
|
||||
{
|
||||
public class DataSeeding(IDbContextFactory<FieldIsolationServiceDbContext> dbContext, ICurrentTenant currentTenant)
|
||||
public class DataSeeding(IDbContextFactory<FieldIsolationServiceDbContext> dbContextFactory, ICurrentTenant currentTenant)
|
||||
{
|
||||
public async Task SeedDataAsync()
|
||||
{
|
||||
currentTenant.SetTenant("Tenant1");
|
||||
|
||||
FieldIsolationServiceDbContext dbContext1 = await dbContext.CreateDbContextAsync();
|
||||
FieldIsolationServiceDbContext dbContext1 = await dbContextFactory.CreateDbContextAsync();
|
||||
|
||||
if (await dbContext1.Database.EnsureCreatedAsync())
|
||||
{
|
||||
@ -25,7 +28,7 @@ namespace MultiTenancySample.FieldIsolationService.EntityFrameworks
|
||||
|
||||
currentTenant.SetTenant("Tenant2");
|
||||
|
||||
FieldIsolationServiceDbContext dbContext2 = await dbContext.CreateDbContextAsync();
|
||||
FieldIsolationServiceDbContext dbContext2 = await dbContextFactory.CreateDbContextAsync();
|
||||
|
||||
if (await dbContext2.Set<Product>().IgnoreQueryFilters().CountAsync() < 6)
|
||||
{
|
||||
|
@ -1,4 +1,7 @@
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
||||
// Copyright (c) HelloShop Corporation. All rights reserved.
|
||||
// See the license file in the project root for more information.
|
||||
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
||||
using Microsoft.EntityFrameworkCore.Query;
|
||||
using System.Linq.Expressions;
|
||||
|
||||
|
@ -1,4 +1,7 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
// Copyright (c) HelloShop Corporation. All rights reserved.
|
||||
// See the license file in the project root for more information.
|
||||
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Metadata;
|
||||
using MultiTenancySample.FieldIsolationService.Entities;
|
||||
using MultiTenancySample.ServiceDefaults;
|
||||
|
@ -1,4 +1,7 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
// Copyright (c) HelloShop Corporation. All rights reserved.
|
||||
// See the license file in the project root for more information.
|
||||
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.ChangeTracking;
|
||||
using Microsoft.EntityFrameworkCore.Diagnostics;
|
||||
using MultiTenancySample.FieldIsolationService.Entities;
|
||||
|
@ -1,3 +1,6 @@
|
||||
// Copyright (c) HelloShop Corporation. All rights reserved.
|
||||
// See the license file in the project root for more information.
|
||||
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using MultiTenancySample.FieldIsolationService.EntityFrameworks;
|
||||
using MultiTenancySample.ServiceDefaults;
|
||||
|
@ -1,4 +1,7 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
// Copyright (c) HelloShop Corporation. All rights reserved.
|
||||
// See the license file in the project root for more information.
|
||||
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using MultiTenancySample.SchemaIsolationService.Entities;
|
||||
using MultiTenancySample.SchemaIsolationService.EntityFrameworks;
|
||||
|
@ -1,4 +1,7 @@
|
||||
namespace MultiTenancySample.SchemaIsolationService.Entities
|
||||
// Copyright (c) HelloShop Corporation. All rights reserved.
|
||||
// See the license file in the project root for more information.
|
||||
|
||||
namespace MultiTenancySample.SchemaIsolationService.Entities
|
||||
{
|
||||
public class Product
|
||||
{
|
||||
|
@ -1,4 +1,7 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
// Copyright (c) HelloShop Corporation. All rights reserved.
|
||||
// See the license file in the project root for more information.
|
||||
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using MultiTenancySample.SchemaIsolationService.Entities;
|
||||
using MultiTenancySample.ServiceDefaults;
|
||||
|
||||
|
@ -1,4 +1,7 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
// Copyright (c) HelloShop Corporation. All rights reserved.
|
||||
// See the license file in the project root for more information.
|
||||
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using MultiTenancySample.SchemaIsolationService.Entities;
|
||||
using MultiTenancySample.ServiceDefaults;
|
||||
|
||||
|
@ -1,3 +1,6 @@
|
||||
// Copyright (c) HelloShop Corporation. All rights reserved.
|
||||
// See the license file in the project root for more information.
|
||||
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using MultiTenancySample.SchemaIsolationService.EntityFrameworks;
|
||||
using MultiTenancySample.ServiceDefaults;
|
||||
|
@ -1,4 +1,7 @@
|
||||
namespace MultiTenancySample.ServiceDefaults
|
||||
// Copyright (c) HelloShop Corporation. All rights reserved.
|
||||
// See the license file in the project root for more information.
|
||||
|
||||
namespace MultiTenancySample.ServiceDefaults
|
||||
{
|
||||
public class CurrentTenant : ICurrentTenant
|
||||
{
|
||||
|
@ -1,4 +1,7 @@
|
||||
namespace MultiTenancySample.ServiceDefaults
|
||||
// Copyright (c) HelloShop Corporation. All rights reserved.
|
||||
// See the license file in the project root for more information.
|
||||
|
||||
namespace MultiTenancySample.ServiceDefaults
|
||||
{
|
||||
public interface ICurrentTenant
|
||||
{
|
||||
|
@ -1,4 +1,7 @@
|
||||
namespace MultiTenancySample.ServiceDefaults
|
||||
// Copyright (c) HelloShop Corporation. All rights reserved.
|
||||
// See the license file in the project root for more information.
|
||||
|
||||
namespace MultiTenancySample.ServiceDefaults
|
||||
{
|
||||
public interface ITenantIdProvider
|
||||
{
|
||||
|
@ -1,4 +1,7 @@
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
// Copyright (c) HelloShop Corporation. All rights reserved.
|
||||
// See the license file in the project root for more information.
|
||||
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace MultiTenancySample.ServiceDefaults
|
||||
|
@ -1,4 +1,7 @@
|
||||
using Microsoft.AspNetCore.Http;
|
||||
// Copyright (c) HelloShop Corporation. All rights reserved.
|
||||
// See the license file in the project root for more information.
|
||||
|
||||
using Microsoft.AspNetCore.Http;
|
||||
|
||||
namespace MultiTenancySample.ServiceDefaults
|
||||
{
|
||||
@ -10,7 +13,7 @@ namespace MultiTenancySample.ServiceDefaults
|
||||
|
||||
const string tenantKey = "tenant";
|
||||
|
||||
if(httpContext.User.FindAll(tenantKey).Any())
|
||||
if (httpContext.User.FindAll(tenantKey).Any())
|
||||
{
|
||||
return httpContext.User.FindFirst(tenantKey)?.Value;
|
||||
}
|
||||
|
@ -1,4 +1,7 @@
|
||||
using Microsoft.AspNetCore.Http;
|
||||
// Copyright (c) HelloShop Corporation. All rights reserved.
|
||||
// See the license file in the project root for more information.
|
||||
|
||||
using Microsoft.AspNetCore.Http;
|
||||
|
||||
namespace MultiTenancySample.ServiceDefaults
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user