【问题标题】:Codefluent separate databases in Saas solutionSaas 解决方案中的 Codefluent 独立数据库
【发布时间】:2016-02-26 18:08:39
【问题描述】:

我有一个使用 CodeFluent 构建的应用程序,它作为 SAAS 解决方案托管。它使用 Ms Azure 数据库作为存储,但现在所有客户都在同一个数据库中。考虑到 SAAS 解决方案的最佳实践,最好将数据库分开。备份/恢复单独的客户端数据会更容易,而且从安全角度来看也更好。我们想使用 Azure 弹性数据库池。

然而,这并不简单。 Codefluent 使用固定的数据库连接,在 web.config 中设置。如果我能以某种方式改变它,我怎么能确定使用什么数据库。并不总是有会话或 httpcontext... 有没有人遇到过同样的挑战,您是如何解决的?

【问题讨论】:

    标签: saas codefluent


    【解决方案1】:

    每个用户将拥有一个数据库。这意味着您需要在查询数据库之前更改连接字符串。使用 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-11
      • 2011-01-24
      • 2018-06-07
      相关资源
      最近更新 更多