Permalink
Browse files

When wiring up Mongo persistence, can supply a delegate to return a c…

…onnection string (in conjunction with the exisitng connectionName). (Cherrypicked from v4 branch)
  • Loading branch information...
1 parent 8635f71 commit d810307197240cd6d931ea9a89ed1c7d336924da @damianh damianh committed Sep 16, 2013
View
19 src/NEventStore.Persistence.MongoDB.Tests/AcceptanceTestMongoPersistenceFactory.cs
@@ -1,28 +1,27 @@
namespace NEventStore.Persistence.MongoDB.Tests
{
using System;
- using NEventStore.Persistence.MongoDB;
using NEventStore.Serialization;
public class AcceptanceTestMongoPersistenceFactory : MongoPersistenceFactory
{
private const string EnvVarKey = "NEventStore.MongoDB";
- public AcceptanceTestMongoPersistenceFactory() : base("", new DocumentObjectSerializer())
+ public AcceptanceTestMongoPersistenceFactory()
+ : base(GetConnectionString, new DocumentObjectSerializer())
{}
- protected override string GetConnectionString()
+ private static string GetConnectionString()
{
string connectionString = Environment.GetEnvironmentVariable(EnvVarKey, EnvironmentVariableTarget.Process);
if (connectionString == null)
{
- string message =
- string.Format(
- "Cannot initialize acceptance tests for Mongo. Cannot find the '{0}' environment variable. Please ensure " +
- "you have correctly setup the connection string environment variables. Refer to the " +
- "NEventStore wiki for details.",
- EnvVarKey);
+ string message = string.Format(
+ "Cannot initialize acceptance tests for Mongo. Cannot find the '{0}' environment variable. Please ensure " +
+ "you have correctly setup the connection string environment variables. Refer to the " +
+ "NEventStore wiki for details.",
+ EnvVarKey);
throw new InvalidOperationException(message);
}
@@ -31,4 +30,4 @@ protected override string GetConnectionString()
return connectionString;
}
}
-}
+}
View
22 src/NEventStore.Persistence.MongoDB/MongoPersistenceFactory.cs
@@ -1,36 +1,26 @@
namespace NEventStore.Persistence.MongoDB
{
- using System.Configuration;
+ using System;
using global::MongoDB.Driver;
using NEventStore.Serialization;
public class MongoPersistenceFactory : IPersistenceFactory
{
- private readonly string _connectionName;
+ private readonly Func<string> _connectionStringProvider;
private readonly IDocumentSerializer _serializer;
- public MongoPersistenceFactory(string connectionName, IDocumentSerializer serializer)
+ public MongoPersistenceFactory(Func<string> connectionStringProvider, IDocumentSerializer serializer)
{
- _connectionName = connectionName;
+ _connectionStringProvider = connectionStringProvider;
_serializer = serializer;
}
public virtual IPersistStreams Build()
{
- string connectionString = TransformConnectionString(GetConnectionString());
+ string connectionString = _connectionStringProvider();
var builder = new MongoUrlBuilder(connectionString);
MongoDatabase database = (new MongoClient(connectionString)).GetServer().GetDatabase(builder.DatabaseName);
return new MongoPersistenceEngine(database, _serializer);
}
-
- protected virtual string GetConnectionString()
- {
- return ConfigurationManager.ConnectionStrings[_connectionName].ConnectionString;
- }
-
- protected virtual string TransformConnectionString(string connectionString)
- {
- return connectionString;
- }
}
-}
+}
View
13 src/NEventStore.Persistence.MongoDB/MongoPersistenceWireup.cs
@@ -1,6 +1,8 @@
-// ReSharper disable once CheckNamespace
-namespace NEventStore
+// ReSharper disable CheckNamespace
+
+namespace NEventStore // ReSharper restore CheckNamespace
{
+ using System;
using System.Transactions;
using NEventStore.Logging;
using NEventStore.Persistence.MongoDB;
@@ -10,7 +12,8 @@ public class MongoPersistenceWireup : PersistenceWireup
{
private static readonly ILog Logger = LogFactory.BuildLogger(typeof (MongoPersistenceWireup));
- public MongoPersistenceWireup(Wireup inner, string connectionName, IDocumentSerializer serializer) : base(inner)
+ public MongoPersistenceWireup(Wireup inner, Func<string> connectionStringProvider, IDocumentSerializer serializer)
+ : base(inner)
{
Logger.Debug("Configuring Mongo persistence engine.");
@@ -20,7 +23,7 @@ public MongoPersistenceWireup(Wireup inner, string connectionName, IDocumentSeri
Logger.Warn("MongoDB does not participate in transactions using TransactionScope.");
}
- Container.Register(c => new MongoPersistenceFactory(connectionName, serializer).Build());
+ Container.Register(c => new MongoPersistenceFactory(connectionStringProvider, serializer).Build());
}
}
-}
+}
View
9 src/NEventStore.Persistence.MongoDB/MongoPersistenceWireupExtensions.cs
@@ -1,13 +1,20 @@
// ReSharper disable once CheckNamespace
namespace NEventStore
{
+ using System;
+ using System.Configuration;
using NEventStore.Serialization;
public static class MongoPersistenceWireupExtensions
{
public static PersistenceWireup UsingMongoPersistence(this Wireup wireup, string connectionName, IDocumentSerializer serializer)
{
- return new MongoPersistenceWireup(wireup, connectionName, serializer);
+ return new MongoPersistenceWireup(wireup, () => ConfigurationManager.ConnectionStrings[connectionName].ConnectionString, serializer);
+ }
+
+ public static PersistenceWireup UsingMongoPersistence(this Wireup wireup, Func<string> connectionStringProvider, IDocumentSerializer serializer)
+ {
+ return new MongoPersistenceWireup(wireup, connectionStringProvider, serializer);
}
}
}

0 comments on commit d810307

Please sign in to comment.