如何在Jonathan Oliver的EventStore中使用连接字符串配置SqlPersistence实例?

时间:2012-01-23 14:08:12

标签: c# event-sourcing event-store neventstore

代码bellow将为命名连接创建eventstore。

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

我需要类似的东西:

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

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

2 个答案:

答案 0 :(得分:3)

我没有找到使用任何内置类的方法,但是你提到的界面或多或少都很容易使用。

使用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("")

答案 1 :(得分:0)

创建了一个支持配置连接字符串的拉取请求。

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

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

更新:接受拉取请求,自NEventstore版本5.1起可用。现在可以在nuget上找到。