Saas 解决方案中的 Codefluent 独立数据库
Codefluent separate databases in Saas solution
我有一个使用 CodeFluent 构建的应用程序,该应用程序作为 SAAS 解决方案托管。它使用 Ms Azure 数据库作为存储,但所有客户现在都在同一个数据库中。考虑到 SAAS 解决方案的最佳实践,分离数据库会更好。 backup/restore 分离客户端数据会更容易,从安全角度来看也更好。我们想使用 Azure 弹性数据库池。
然而,其实并不简单。 Codefluent 使用固定的数据库连接,设置在 web.config 中。如果我能以某种方式改变它,我该如何确定要使用的数据库。总是没有session或者httpcontext...有没有人遇到过同样的问题,你是怎么解决的?
每位用户将拥有一个数据库。这意味着您需要在查询数据库之前更改连接字符串。使用 CodeFluent 实体,您可以在运行时更改连接字符串:
CodeFluentContext context = CodeFluentContext.Get(MyApp.Constants.MyAppStoreName);
CodeFluentPersistence persistence = context.Persistence;
persistence.ConnectionString = GetCurrentTenantConnectionString();
var products = ProductCollection.LoadAll();
或者您可以创建自定义 CodeFluentPersistence
:
public class MultiTenantPersistence : CodeFluentPersistence
{
public MultiTenantPersistence(CodeFluentContext context) : base(context)
{
}
public override string ConnectionString
{
get
{
return GetCurrentTenantConnectionString();
}
set
{
base.ConnectionString = value;
}
}
private string GetCurrentTenantConnectionString()
{
// TODO Implement your own logic
return $"Server=sample;Database=sample_{Context.User.UserName};Trusted_Connection=True;";
}
}
然后需要在配置文件中注册MultiTenantPersistence
:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="Samples" type="CodeFluent.Runtime.CodeFluentConfigurationSectionHandler, CodeFluent.Runtime" />
</configSections>
<Samples persistenceHookTypeName="Sample.MultiTenantPersistence, Sample" />
</configuration>
我有一个使用 CodeFluent 构建的应用程序,该应用程序作为 SAAS 解决方案托管。它使用 Ms Azure 数据库作为存储,但所有客户现在都在同一个数据库中。考虑到 SAAS 解决方案的最佳实践,分离数据库会更好。 backup/restore 分离客户端数据会更容易,从安全角度来看也更好。我们想使用 Azure 弹性数据库池。
然而,其实并不简单。 Codefluent 使用固定的数据库连接,设置在 web.config 中。如果我能以某种方式改变它,我该如何确定要使用的数据库。总是没有session或者httpcontext...有没有人遇到过同样的问题,你是怎么解决的?
每位用户将拥有一个数据库。这意味着您需要在查询数据库之前更改连接字符串。使用 CodeFluent 实体,您可以在运行时更改连接字符串:
CodeFluentContext context = CodeFluentContext.Get(MyApp.Constants.MyAppStoreName);
CodeFluentPersistence persistence = context.Persistence;
persistence.ConnectionString = GetCurrentTenantConnectionString();
var products = ProductCollection.LoadAll();
或者您可以创建自定义 CodeFluentPersistence
:
public class MultiTenantPersistence : CodeFluentPersistence
{
public MultiTenantPersistence(CodeFluentContext context) : base(context)
{
}
public override string ConnectionString
{
get
{
return GetCurrentTenantConnectionString();
}
set
{
base.ConnectionString = value;
}
}
private string GetCurrentTenantConnectionString()
{
// TODO Implement your own logic
return $"Server=sample;Database=sample_{Context.User.UserName};Trusted_Connection=True;";
}
}
然后需要在配置文件中注册MultiTenantPersistence
:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="Samples" type="CodeFluent.Runtime.CodeFluentConfigurationSectionHandler, CodeFluent.Runtime" />
</configSections>
<Samples persistenceHookTypeName="Sample.MultiTenantPersistence, Sample" />
</configuration>