How to get SqlPersistence instance configured with connectio

本文关键字:configured with connectio instance SqlPersistence to get How | 更新日期: 2023-09-27 18:21:04

下面的代码将为命名连接创建事件存储。

var es = Wireup.Init()                                
    .UsingSqlPersistence("DB")
    .Build();

我需要类似的东西:

var es = Wireup.Init()                                
    .UsingSqlPersistence("Data Source=TEST;Initial Catalog=App")
    .Build();

我发现最简单的方法是创建自定义IConnectionFactory,但这看起来并不正确。有什么建议吗?

How to get SqlPersistence instance configured with connectio

我没有找到用任何内置类实现这一点的方法,但您提到的接口或多或少很容易使用。

使用joliver的代码,我实现了一个连接字符串工厂,如下所示:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Common;
using System.Configuration;
using System.Data;
using EventStore.Persistence.SqlPersistence;
using EventStore.Persistence;
namespace Project.Factories
{
    public class ConnectionStringConnectionFactory : IConnectionFactory
    {
        private static readonly IDictionary<string, DbProviderFactory> CachedFactories =
            new Dictionary<string, DbProviderFactory>();
        private string m_connectionString;
        private string m_providerName;
        private string m_replicaConnectionString;
        private string m_replicaProviderName;
        public ConnectionStringConnectionFactory(string connectionString, string providerName)
            : this(connectionString, providerName, connectionString, providerName)
        {
        }
        public ConnectionStringConnectionFactory(
            string connectionString,
            string providerName,
            string replicaConnectionString,
            string replicaProviderName)
        {
            m_connectionString = connectionString;
            m_providerName = providerName;
            m_replicaConnectionString = replicaConnectionString;
            m_replicaProviderName = replicaProviderName;
        }

        public virtual IDbConnection OpenMaster(Guid streamId)
        {
            return this.Open(streamId, m_connectionString, m_providerName);
        }
        public virtual IDbConnection OpenReplica(Guid streamId)
        {
            return this.Open(streamId, m_replicaConnectionString, m_replicaProviderName);
        }
        protected virtual IDbConnection Open(Guid streamId, string connectionString, string providerName)
        {
            return new ConnectionScope(connectionString, () => this.Open(connectionString, providerName));
        }
        protected virtual IDbConnection Open(string connectionString, string providerName)
        {
            var factory = this.GetFactory(providerName);
            var connection = factory.CreateConnection();
            if (connection == null)
                throw new ConfigurationErrorsException("Invalid provider name");
            connection.ConnectionString = connectionString;
            try
            {
                connection.Open();
            }
            catch (Exception e)
            {
                throw new StorageUnavailableException(e.Message, e);
            }
            return connection;
        }
        protected virtual DbProviderFactory GetFactory(string providerName)
        {
            lock (CachedFactories)
            {
                DbProviderFactory factory;
                if (CachedFactories.TryGetValue(providerName, out factory))
                    return factory;
                factory = DbProviderFactories.GetFactory(providerName);
                return CachedFactories[providerName] = factory;
            }
        }
        public ConnectionStringSettings Settings
        {
            get { return new ConnectionStringSettings("Default", m_connectionString, m_providerName); }
        }
    }
}

要使用它,只需在工厂中通过:

var fac = new ConnectionStringConnectionFactory(
          "Your Connection String",
          "System.Data.SqlClient");
Wireup.Init().UsingSqlPersistence(fac)

如果你愿意,你可以用一种扩展方法来包装它:

Wireup.Init().UsingSqlPersistenceWithConnectionString("")

创建了一个pull请求,用于支持带有连接字符串的配置。

https://github.com/NEventStore/NEventStore/pull/358

如果不被接受,您仍然可以在自己的项目中使用该代码启用带有连接字符串的配置,这只是几行代码。

更新:Pull请求已被接受,并且从NEventstore的5.1版本起可用。现在可以在nuget上使用。