Skip to content

Commit

Permalink
Putting in configuration for connectionstring and database
Browse files Browse the repository at this point in the history
Related to #807
  • Loading branch information
einari committed Apr 13, 2017
1 parent 6f96dee commit e05bdf6
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
10 changes: 9 additions & 1 deletion Source/Bifrost.Events.MongoDB/EventStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ namespace Bifrost.Events.MongoDB
/// </summary>
public class EventStore : IEventStore
{
const string EventStoreCollectionName = "Events";

IMongoCollection<BsonDocument> _collection;

static EventStore()
Expand All @@ -27,9 +29,15 @@ static EventStore()
/// <summary>
/// Initializes a new instance of <see cref="EventStore"/>
/// </summary>
public EventStore()
/// <param name="connectionDetailsProvider"><see cref="ICanProvideConnectionDetails">Connection details provider</see></param>
public EventStore(ICanProvideConnectionDetails connectionDetailsProvider)
{
_collection = null;
var connectionDetails = connectionDetailsProvider();
var settings = MongoClientSettings.FromUrl(new MongoUrl(connectionDetails.Item1));
var client = new MongoClient(settings);
var database = client.GetDatabase(connectionDetails.Item2);
_collection = database.GetCollection<BsonDocument>(EventStoreCollectionName);
}

/// <inheritdoc/>
Expand Down
30 changes: 30 additions & 0 deletions Source/Bifrost.Events.MongoDB/EventStoreConfigurationExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) 2008-2017 Dolittle. All rights reserved.
* Licensed under the MIT License. See LICENSE in the project root for license information.
*--------------------------------------------------------------------------------------------*/
using System;
using Bifrost.Configuration;
using Bifrost.Events.MongoDB;

namespace Bifrost.Events
{
/// <summary>
/// Extensions for configuring <see cref="EventStoreConfiguration"/>
/// </summary>
public static class EventStoreConfigurationExtensions
{
/// <summary>
/// Configures <see cref="IEventStore"/> to be using <see cref="EventStore">Azure Tables</see>
/// </summary>
/// <param name="eventStoreConfiguration"><see cref="EventStoreConfiguration"/> to configure</param>
/// <param name="connectionString"><see cref="string">ConnectionString</see> for connecting to your Azure Storage account</param>
/// <param name="databaseName">Name of <see cref="string">Database</see></param>
/// <returns>Chained <see cref="EventStoreConfiguration"/></returns>
public static EventStoreConfiguration UsingTables(this EventStoreConfiguration eventStoreConfiguration, string connectionString, string databaseName)
{
eventStoreConfiguration.EventStore = typeof(EventStore);
Configure.Instance.Container.Bind<ICanProvideConnectionDetails>(() => new Tuple<string,string>(connectionString, databaseName));
return eventStoreConfiguration;
}
}
}
14 changes: 14 additions & 0 deletions Source/Bifrost.Events.MongoDB/ICanProvideConnectionDetails.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) 2008-2017 Dolittle. All rights reserved.
* Licensed under the MIT License. See LICENSE in the project root for license information.
*--------------------------------------------------------------------------------------------*/
using System;

namespace Bifrost.Events.MongoDB
{
/// <summary>
/// Delegate for providing connection string for <see cref="EventStore"/>
/// </summary>
/// <returns></returns>
public delegate Tuple<string,string> ICanProvideConnectionDetails();
}

0 comments on commit e05bdf6

Please sign in to comment.