How to get SqlPersistence instance configured with connection string in Jonathan Oliver's EventStore?

854 views Asked by At

Code bellow will create eventstore for named connection.

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

I need something like:

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

The most easy way I found is to create custom IConnectionFactory, but this does not look right. Any suggestions?

2

There are 2 answers

0
Alex Duggleby On BEST ANSWER

I did not find a way to do it with any of the built-in classes, but there is the interface you mentioned with is more or less easy to use.

Using joliver's code I implemented a Connection String factory as follows:

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); }
        }
    }
}

To use it simply pass in a factory:

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

Wireup.Init().UsingSqlPersistence(fac)

If you wanted you could wrap that in an extension method:

Wireup.Init().UsingSqlPersistenceWithConnectionString("")
0
Robin van der Knaap On

Created a pull request for supporting configuration with connection string.

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

If not accepted, you can still use the code to enable configuration with connection string in your own project, it's only a few lines of code.

UPDATE: Pull request is accepted, and is available as of version 5.1 of NEventstore. Which is now available on nuget.