Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mongodb driver update per Tyrone's request #16

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added binaries/MongoDB.Bson.dll
Binary file not shown.
Binary file added binaries/MongoDB.Driver.dll
Binary file not shown.
Binary file modified binaries/SimpleCqrs.EventStore.MongoDb.dll
Binary file not shown.
Binary file modified binaries/SimpleCqrs.EventStore.MongoDb.pdb
Binary file not shown.
Binary file modified binaries/SimpleCqrs.Utilites.dll
Binary file not shown.
Binary file modified binaries/SimpleCqrs.Utilites.pdb
Binary file not shown.
Binary file modified binaries/SimpleCqrs.dll
Binary file not shown.
Binary file modified binaries/SimpleCqrs.pdb
Binary file not shown.
Binary file modified binaries/eventstores/file/SimpleCqrs.EventStore.File.dll
Binary file not shown.
Binary file modified binaries/eventstores/file/SimpleCqrs.EventStore.File.pdb
Binary file not shown.
Binary file added binaries/eventstores/mongodb/MongoDB.Bson.dll
Binary file not shown.
Binary file added binaries/eventstores/mongodb/MongoDB.Driver.dll
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified binaries/extension/nservicebus/SimpleCqrs.NServiceBus.dll
Binary file not shown.
Binary file modified binaries/extension/nservicebus/SimpleCqrs.NServiceBus.pdb
Binary file not shown.
Binary file modified binaries/extension/nservicebus/SimpleCqrs.dll
Binary file not shown.
Binary file modified binaries/extension/nservicebus/SimpleCqrs.pdb
Binary file not shown.
Binary file modified binaries/extension/rabbitmq/SimpleCqrs.RabbitMQ.dll
Binary file not shown.
Binary file modified binaries/extension/rabbitmq/SimpleCqrs.RabbitMQ.pdb
Binary file not shown.
Binary file modified binaries/extension/rabbitmq/SimpleCqrs.dll
Binary file not shown.
Binary file modified binaries/extension/rabbitmq/SimpleCqrs.pdb
Binary file not shown.
Binary file modified binaries/servicelocators/unity/Microsoft.Practices.Unity.dll
Binary file not shown.
Binary file modified binaries/servicelocators/unity/SimpleCqrs.Unity.dll
Binary file not shown.
Binary file modified binaries/servicelocators/unity/SimpleCqrs.Unity.pdb
Binary file not shown.
Binary file modified lib/Microsoft.Practices.Unity.dll
Binary file not shown.
Binary file added lib/MongoDB.Bson.dll
Binary file not shown.
Binary file added lib/MongoDB.Driver.dll
Binary file not shown.
Binary file removed lib/MongoDB.GridFS.dll
Binary file not shown.
Binary file removed lib/MongoDB.dll
Binary file not shown.
108 changes: 22 additions & 86 deletions src/EventStores/SimpleCqrs.EventStore.MongoDb/MongoEventStore.cs
Original file line number Diff line number Diff line change
@@ -1,118 +1,54 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using MongoDB;
using MongoDB.Configuration;
using MongoDB.Configuration.Builders;
using MongoDB.Bson;
using MongoDB.Bson.Serialization;
using MongoDB.Driver;
using MongoDB.Driver.Builders;
using SimpleCqrs.Eventing;

namespace SimpleCqrs.EventStore.MongoDb
{
public class MongoEventStore : IEventStore
{
private static readonly MethodInfo MapMethod = typeof(MappingStoreBuilder).GetMethod("Map", Type.EmptyTypes);
private readonly MongoConfiguration configuration;
private readonly string databaseName;
private readonly MongoCollection<DomainEvent> _collection;

public MongoEventStore(string connectionString, ITypeCatalog typeCatalog)
{
var connectionStringBuilder = new MongoConnectionStringBuilder(connectionString);
databaseName = connectionStringBuilder.Database;
configuration = BuildMongoConfiguration(typeCatalog, connectionString);
}

private static MongoConfiguration BuildMongoConfiguration(ITypeCatalog domainEventTypeCatalog, string connectionString)
{
var configurationBuilder = new MongoConfigurationBuilder();
configurationBuilder.ConnectionString(connectionString);
configurationBuilder.Mapping(mapping =>
{
mapping.DefaultProfile(profile => profile.SubClassesAre(t => t.IsSubclassOf(typeof(DomainEvent))));
domainEventTypeCatalog
.GetDerivedTypes(typeof(DomainEvent))
.ToList()
.ForEach(type => MapEventType(type, mapping));
});
typeCatalog.GetDerivedTypes(typeof(DomainEvent)).ToList().
ForEach(x => BsonClassMap.LookupClassMap(x));

return configurationBuilder.BuildConfiguration();
_collection = MongoServer.Create(connectionString).GetDatabase("events").GetCollection<DomainEvent>("events");
}

public IEnumerable<DomainEvent> GetEvents(Guid aggregateRootId, int startSequence)
{
using(var mongo = new Mongo(configuration))
{
mongo.Connect();

var database = mongo.GetDatabase(databaseName);
var eventsCollection = database.GetCollection<DomainEvent>("events").Linq();

return (from domainEvent in eventsCollection
where domainEvent.AggregateRootId == aggregateRootId
where domainEvent.Sequence > startSequence
select domainEvent).ToList();
}
return _collection.Find(
Query.And(
Query.EQ("AggregateRootId", aggregateRootId),
Query.GT("Sequence", startSequence))).
SetFields(Fields.Exclude("_id")).ToList();
}

public void Insert(IEnumerable<DomainEvent> domainEvents)
{
using(var mongo = new Mongo(configuration))
{
mongo.Connect();

var database = mongo.GetDatabase(databaseName);
var eventsCollection = database.GetCollection<DomainEvent>("events");
eventsCollection.Insert(domainEvents);
}
_collection.InsertBatch(domainEvents);
}

public IEnumerable<DomainEvent> GetEventsByEventTypes(IEnumerable<Type> domainEventTypes, DateTime startDate, DateTime endDate)
{
using(var mongo = new Mongo(configuration))
{
mongo.Connect();

var database = mongo.GetDatabase(databaseName);
var selector = new Document
{
{ "_t", new Document { { "$in", domainEventTypes.Select(t => t.Name).ToArray() } } },
{ "EventDate", new Document { { "$gte", startDate }, { "$lte", endDate } } }
};

var cursor = database.GetCollection<DomainEvent>("events").Find(selector);

return cursor.Documents.ToList();
}
}

public IEnumerable<DomainEvent> GetEventsBySelector(Document selector)
{
using(var mongo = new Mongo(configuration))
{
mongo.Connect();

var database = mongo.GetDatabase(databaseName);
var cursor = database.GetCollection<DomainEvent>("events").Find(selector);
return cursor.Documents.ToList();
}
return _collection.Find(
Query.And(
Query.In("_t", domainEventTypes.Select(t => new BsonString(t.Name)).ToArray()),
Query.GTE("EventDate", startDate),
Query.LTE("EventDate", endDate))).
SetFields(Fields.Exclude("_id")).ToList();
}

public IEnumerable<DomainEvent> GetEventsBySelector(Document selector, int skip, int limit)
public IEnumerable<DomainEvent> GetEventsBySelector(IMongoQuery selector, int skip, int limit)
{
using(var mongo = new Mongo(configuration))
{
mongo.Connect();

var database = mongo.GetDatabase(databaseName);
var cursor = database.GetCollection<DomainEvent>("events").Find(selector);
return cursor.Skip(skip).Limit(limit).Documents.ToList();
}
return _collection.Find(selector).SetSkip(skip).SetLimit(limit);
}

private static void MapEventType(Type type, MappingStoreBuilder mapping)
{
MapMethod.MakeGenericMethod(type)
.Invoke(mapping, new object[] {});
}
}
}
94 changes: 33 additions & 61 deletions src/EventStores/SimpleCqrs.EventStore.MongoDb/MongoSnapshotStore.cs
Original file line number Diff line number Diff line change
@@ -1,62 +1,34 @@
using System;
using System.Linq;
using System.Reflection;
using MongoDB;
using MongoDB.Configuration;
using MongoDB.Configuration.Builders;
using SimpleCqrs.Domain;
using SimpleCqrs.Eventing;

namespace SimpleCqrs.EventStore.MongoDb
{
public class MongoSnapshotStore : ISnapshotStore
{
private static readonly MethodInfo MapMethod = typeof(MappingStoreBuilder).GetMethod("Map", Type.EmptyTypes);
private readonly IMongoDatabase database;

public MongoSnapshotStore(string connectionString, ITypeCatalog snapshotTypeCatalog)
{
var configuration = BuildMongoConfiguration(snapshotTypeCatalog, connectionString);
var mongo = new Mongo(configuration);
mongo.Connect();

database = mongo.GetDatabase("snapshotstore");
}

public Snapshot GetSnapshot(Guid aggregateRootId)
{
var snapshotsCollection = database.GetCollection<Snapshot>("snapshots").Linq();
return (from snapshot in snapshotsCollection
where snapshot.AggregateRootId == aggregateRootId
select snapshot).SingleOrDefault();
}

public void SaveSnapshot<TSnapshot>(TSnapshot snapshot) where TSnapshot : Snapshot
{
var snapshotsCollection = database.GetCollection<TSnapshot>("snapshots");
snapshotsCollection.Update(snapshot, new { snapshot.AggregateRootId }, UpdateFlags.Upsert);
}

private static MongoConfiguration BuildMongoConfiguration(ITypeCatalog snapshotTypeCatalog, string connectionString)
{
var configurationBuilder = new MongoConfigurationBuilder();
configurationBuilder.ConnectionString(connectionString);
configurationBuilder.Mapping(mapping =>
{
mapping.DefaultProfile(profile => profile.SubClassesAre(type => type.IsSubclassOf(typeof(Snapshot))));
snapshotTypeCatalog
.GetDerivedTypes(typeof(Snapshot))
.ToList()
.ForEach(type => MapEventType(type, mapping));
});

return configurationBuilder.BuildConfiguration();
}

private static void MapEventType(Type type, MappingStoreBuilder mapping)
{
MapMethod.MakeGenericMethod(type)
.Invoke(mapping, new object[] {});
}
}
using System;
using System.Linq;
using MongoDB.Bson.Serialization;
using MongoDB.Driver;
using MongoDB.Driver.Builders;
using SimpleCqrs.Domain;
using SimpleCqrs.Eventing;

namespace SimpleCqrs.EventStore.MongoDb
{
public class MongoSnapshotStore : ISnapshotStore
{
private readonly MongoCollection<Snapshot> _collection;

public MongoSnapshotStore(string connectionString, ITypeCatalog snapshotTypeCatalog)
{
snapshotTypeCatalog.GetDerivedTypes(typeof(Snapshot)).ToList().
ForEach(x => BsonClassMap.LookupClassMap(x));

_collection = MongoServer.Create(connectionString).GetDatabase("snapshotstore").GetCollection<Snapshot>("snapshots");
}

public Snapshot GetSnapshot(Guid aggregateRootId)
{
return _collection.Find(Query.EQ("AggregateRoootId", aggregateRootId)).
SetFields(Fields.Exclude("_id")).Single();
}

public void SaveSnapshot<TSnapshot>(TSnapshot snapshot) where TSnapshot : Snapshot
{
_collection.Save(snapshot);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,16 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="MongoDB, Version=0.90.0.1, Culture=neutral, PublicKeyToken=f61bd00ba2535278, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\..\lib\MongoDB.dll</HintPath>
<Reference Include="MongoDB.Bson">
<HintPath>..\..\..\lib\MongoDB.Bson.dll</HintPath>
</Reference>
<Reference Include="MongoDB.GridFS, Version=0.90.0.1, Culture=neutral, PublicKeyToken=f61bd00ba2535278, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\..\lib\MongoDB.GridFS.dll</HintPath>
<Reference Include="MongoDB.Driver">
<HintPath>..\..\..\lib\MongoDB.Driver.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.XML" />
</ItemGroup>
<ItemGroup>
Expand Down
5 changes: 2 additions & 3 deletions src/SimpleCQRS.sln
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{EF3C709C-758C-451D-8B6C-4222D1BE8EDF}"
ProjectSection(SolutionItems) = preProject
Local.testsettings = Local.testsettings
Simple CQRS.vsmdi = Simple CQRS.vsmdi
SimpleCQRS.vsmdi = SimpleCQRS.vsmdi
TraceAndTestImpact.testsettings = TraceAndTestImpact.testsettings
EndProjectSection
EndProject
Expand Down Expand Up @@ -38,7 +38,7 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SimpleCqrs.RabbitMQ", "Exte
EndProject
Global
GlobalSection(TestCaseManagementSettings) = postSolution
CategoryFile = Simple CQRS.vsmdi
CategoryFile = SimpleCQRS.vsmdi
EndGlobalSection
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -90,7 +90,6 @@ Global
{D3DFE07B-B78C-4AF2-A886-002A09D9FAA0} = {BE8A5F95-23AD-4B5D-BA21-004909E80F00}
{201A4DF5-2217-4202-80E8-E784737366BC} = {77CC485D-D260-45FC-A161-3DDF3A571C41}
{65B52205-E45D-4D6B-8F48-AFBC4406BEE8} = {77CC485D-D260-45FC-A161-3DDF3A571C41}
{BEEBFC8E-91EC-4E24-8223-395C2302C1B3} = {77CC485D-D260-45FC-A161-3DDF3A571C41}
{38E51E8F-7008-47EC-AC9F-862A1F377532} = {D3DFE07B-B78C-4AF2-A886-002A09D9FAA0}
{00634E6C-A282-404A-B08B-338FD4CA41A9} = {0C8AE82D-CACF-44CD-BE4F-222041AC4699}
{07C2BF77-9646-4C39-BDB2-852B76AA6C75} = {51C20E77-8726-49E8-9061-161DF28973E7}
Expand Down
6 changes: 6 additions & 0 deletions src/SimpleCQRS.vsmdi
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<TestLists xmlns="http://microsoft.com/schemas/VisualStudio/TeamTest/2010">
<TestList name="Lists of Tests" id="8c43106b-9dc1-4907-a29f-aa66a61bf5b6">
<RunConfiguration id="560176c4-f30f-4c38-b69b-44a2ad5c3c15" name="Trace and Test Impact" storage="traceandtestimpact.testsettings" type="Microsoft.VisualStudio.TestTools.Common.TestRunConfiguration, Microsoft.VisualStudio.QualityTools.Common, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</TestList>
</TestLists>
3 changes: 0 additions & 3 deletions src/SimpleCqrs.Utilites/SimpleCqrs.Utilites.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="MongoDB">
<HintPath>..\..\lib\MongoDB.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Data.DataSetExtensions" />
Expand Down