Skip to content

Instantly share code, notes, and snippets.

View merken's full-sized avatar

Maarten Merken merken

View GitHub Profile
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Contract;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
namespace Products.API.Controllers
{
@merken
merken / HelloController.cs
Created October 16, 2019 07:25
HelloController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Contract;
namespace MyHost.Controllers
@merken
merken / MyHost.csproj
Created October 16, 2019 07:20
MyHost.csproj
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
<EnableDefaultContentItems>false</EnableDefaultContentItems>
</PropertyGroup>
<ItemGroup>
<Content Include="Plugins\*.dll">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
@merken
merken / HelloWorldPlugin.cs
Created October 16, 2019 07:05
HelloWorldPlugin.cs
using System;
using Contract;
using Prise.Infrastructure;
namespace HelloWorldPlugin
{
[Plugin(PluginType = typeof(IHelloWorldPlugin))]
public class HelloWorldPlugin : IHelloWorldPlugin
{
public string SayHello(string input)
@merken
merken / IHelloWorldPlugin.cs
Created October 16, 2019 06:53
IHelloWorldPlugin.cs
using System;
namespace Contract
{
public interface IHelloWorldPlugin
{
string SayHello(string input);
}
}
@merken
merken / UseConnectionPerTenant.cs
Created September 25, 2019 19:46
UseConnectionPerTenant
public static IServiceCollection UseConnectionPerTenant(this IServiceCollection services, IConfiguration configuration)
{
services.AddScoped<ProductsDbContext>((serviceProvider) =>
{
var tenant = serviceProvider.GetRequiredService<TenantInfo>(); // Get from parent service provider (ASP.NET MVC Pipeline)
var connectionString = configuration.GetConnectionString(tenant.Name);
var options = new DbContextOptionsBuilder<ProductsDbContext>()
.UseSqlServer(connectionString)
.Options;
var context = new ProductsDbContext(options);
@merken
merken / DatabaseInterceptor.cs
Created September 25, 2019 19:44
DatabaseInterceptor
using System.Data.Common;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore.Diagnostics;
namespace efcore_tenancy.Infrastructure
{
public class DatabaseInterceptor : DbCommandInterceptor
{
private readonly TenantInfo tenantInfo;
@merken
merken / ServiceCollectionExtensions.cs
Created September 25, 2019 19:36
ServiceCollectionExtensions
using efcore_tenancy.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
namespace efcore_tenancy.Infrastructure
{
public static class ServiceCollectionExtensions
{
@merken
merken / UseEFInterceptor.cs
Created September 25, 2019 19:34
UseEFInterceptor
private static IServiceCollection UseEFInterceptor<T>(this IServiceCollection services, IConfiguration configuration)
where T : class, IInterceptor
{
return services
.AddScoped<DbContextOptions>((serviceProvider) =>
{
var tenant = serviceProvider.GetRequiredService<TenantInfo>();
var efServices = new ServiceCollection();
efServices.AddEntityFrameworkSqlServer();
@merken
merken / TenantInfoMiddleware.cs
Created September 25, 2019 11:35
TenantInfoMiddleware
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
namespace efcore_tenancy.Infrastructure
{
public class TenantInfoMiddleware
{
private readonly RequestDelegate _next;