Skip to content

Commit 15333d6

Browse files
committed
Removed FluentMongo, upgraded to MongoSharpDriver 1.4.2, crossed fingers...
1 parent 95ca054 commit 15333d6

18 files changed

Lines changed: 25625 additions & 6678 deletions
Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
using System;
22
using System.Linq;
3-
using Norm;
3+
using MongoDB.Bson;
4+
using MongoDB.Driver;
5+
using MongoDB.Driver.Builders;
6+
using MongoDB.Driver.Linq;
47
using SharpRepository.Repository;
58
using SharpRepository.Repository.Caching;
69
using SharpRepository.Repository.FetchStrategies;
@@ -9,8 +12,8 @@ namespace SharpRepository.MongoDbRepository
912
{
1013
public class MongoDbRepositoryBase<T, TKey> : LinqRepositoryBase<T, TKey> where T : class, new()
1114
{
12-
private IMongo _provider;
13-
private IMongoDatabase _database;
15+
private MongoServer _server;
16+
private MongoDatabase _database;
1417

1518
internal MongoDbRepositoryBase(ICachingStrategy<T, TKey> cachingStrategy = null)
1619
: base(cachingStrategy)
@@ -21,59 +24,49 @@ internal MongoDbRepositoryBase(ICachingStrategy<T, TKey> cachingStrategy = null)
2124
internal MongoDbRepositoryBase(string connectionString, ICachingStrategy<T, TKey> cachingStrategy = null)
2225
: base(cachingStrategy)
2326
{
24-
Initialize(Mongo.Create(connectionString));
27+
Initialize(MongoServer.Create(connectionString));
2528
}
2629

27-
internal MongoDbRepositoryBase(IMongo mongoProvider, ICachingStrategy<T, TKey> cachingStrategy = null)
30+
internal MongoDbRepositoryBase(MongoServer mongoServer, ICachingStrategy<T, TKey> cachingStrategy = null)
2831
: base(cachingStrategy)
2932
{
30-
Initialize(mongoProvider);
33+
Initialize(mongoServer);
3134
}
3235

33-
private void Initialize(IMongo mongoProvider = null)
36+
private void Initialize(MongoServer mongoServer = null)
3437
{
35-
_provider = mongoProvider ?? Mongo.Create("mongodb://127.0.0.1/Test?strict=false");
36-
_database = _provider.Database;
38+
_server = mongoServer ?? MongoServer.Create("mongodb://localhost");
39+
_database = _server.GetDatabase(TypeName);
3740
}
3841

3942
protected override IQueryable<T> BaseQuery(IFetchStrategy<T> fetchStrategy = null)
4043
{
41-
return _database.GetCollection<T>().AsQueryable();
44+
return _database.GetCollection<T>(TypeName).AsQueryable();
4245
}
4346

4447
protected override T GetQuery(TKey key)
4548
{
46-
return _database.GetCollection<T>().AsQueryable().ToList()
47-
.FirstOrDefault(x => MatchOnPrimaryKey(x, key));
48-
}
49-
50-
private bool MatchOnPrimaryKey(T item, TKey keyValue)
51-
{
52-
TKey value;
53-
return GetPrimaryKey(item, out value) && keyValue.Equals(value);
49+
return BaseQuery().FirstOrDefault();
5450
}
5551

5652
protected override void AddItem(T entity)
5753
{
58-
TKey id;
59-
60-
if (GetPrimaryKey(entity, out id) && Equals(id, default(TKey)))
61-
{
62-
id = GeneratePrimaryKey();
63-
SetPrimaryKey(entity, id);
64-
}
65-
66-
_database.GetCollection<T>().Save(entity);
54+
_database.GetCollection<T>(TypeName).Insert(entity);
6755
}
68-
56+
6957
protected override void DeleteItem(T entity)
7058
{
71-
_database.GetCollection<T>().Delete(entity);
59+
// Yikes.
60+
//IMongoQuery mq = new QueryDocument(entity.ToBsonDocument());
61+
TKey pkValue;
62+
GetPrimaryKey(entity, out pkValue);
63+
_database.GetCollection<T>(TypeName).Remove(Query.EQ("_id", new ObjectId(pkValue.ToString())));
64+
//_database.GetCollection<T>(TypeName).Remove(mq);
7265
}
7366

7467
protected override void UpdateItem(T entity)
7568
{
76-
_database.GetCollection<T>().Save(entity);
69+
_database.GetCollection<T>(TypeName).Save(entity);
7770
}
7871

7972
protected override void SaveChanges()
@@ -93,19 +86,26 @@ private TKey GeneratePrimaryKey()
9386
return (TKey)Convert.ChangeType(Guid.NewGuid(), typeof(TKey));
9487
}
9588

96-
if (typeof(TKey) == typeof(Int32))
97-
{
98-
var nextInt = Convert.ToInt32(_database.GetCollection<T>().GenerateId());
99-
return (TKey)Convert.ChangeType(nextInt, typeof(TKey));
100-
}
89+
//if (typeof(TKey) == typeof(Int32))
90+
//{
91+
// var nextInt = Convert.ToInt32(_database.GetCollection<T>(TypeName).GenerateId());
92+
// return (TKey)Convert.ChangeType(nextInt, typeof(TKey));
93+
//}
10194

102-
if (typeof(TKey) == typeof(Int64))
103-
{
104-
var nextLong = _database.GetCollection<T>().GenerateId();
105-
return (TKey)Convert.ChangeType(nextLong, typeof(TKey));
106-
}
95+
//if (typeof(TKey) == typeof(Int64))
96+
//{
97+
// var nextLong = _database.GetCollection<T>().GenerateId();
98+
// return (TKey)Convert.ChangeType(nextLong, typeof(TKey));
99+
//}
107100

108101
throw new InvalidOperationException("Primary key could not be generated. This only works for GUID, Int32 and Int64.");
109102
}
103+
104+
105+
private bool MatchOnPrimaryKey(T item, TKey keyValue)
106+
{
107+
TKey value;
108+
return GetPrimaryKey(item, out value) && keyValue.Equals(value);
109+
}
110110
}
111111
}

SharpRepository.MongoDbRepository/SharpRepository.MongoDbRepository.csproj

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,13 @@
3131
<WarningLevel>4</WarningLevel>
3232
</PropertyGroup>
3333
<ItemGroup>
34-
<Reference Include="Norm">
35-
<HintPath>..\packages\NoRM.0.9.8\lib\Norm.dll</HintPath>
34+
<Reference Include="MongoDB.Bson, Version=1.4.2.4500, Culture=neutral, PublicKeyToken=f686731cfb9cc103, processorArchitecture=MSIL">
35+
<SpecificVersion>False</SpecificVersion>
36+
<HintPath>..\packages\mongocsharpdriver.1.4.2\lib\net35\MongoDB.Bson.dll</HintPath>
37+
</Reference>
38+
<Reference Include="MongoDB.Driver, Version=1.4.2.4500, Culture=neutral, PublicKeyToken=f686731cfb9cc103, processorArchitecture=MSIL">
39+
<SpecificVersion>False</SpecificVersion>
40+
<HintPath>..\packages\mongocsharpdriver.1.4.2\lib\net35\MongoDB.Driver.dll</HintPath>
3641
</Reference>
3742
<Reference Include="System" />
3843
<Reference Include="System.Core" />
@@ -50,15 +55,15 @@
5055
<Compile Include="MongoDbRepository.cs" />
5156
<Compile Include="MongoDbRepositoryBase.cs" />
5257
</ItemGroup>
53-
<ItemGroup>
54-
<None Include="packages.config" />
55-
</ItemGroup>
5658
<ItemGroup>
5759
<ProjectReference Include="..\SharpRepository.Repository\SharpRepository.Repository.csproj">
5860
<Project>{710DEE79-25CE-4F68-B8B1-D08A135AD154}</Project>
5961
<Name>SharpRepository.Repository</Name>
6062
</ProjectReference>
6163
</ItemGroup>
64+
<ItemGroup>
65+
<None Include="packages.config" />
66+
</ItemGroup>
6267
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
6368
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
6469
Other similar extension points exist, see Microsoft.Common.targets.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
3-
<package id="NoRM" version="0.9.8" />
3+
<package id="mongocsharpdriver" version="1.4.2" />
44
</packages>

SharpRepository.Tests.Integration/Data/MongoDbDataDirectoryFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public class MongoDbDataDirectoryFactory
99

1010
public static string Build(string type)
1111
{
12-
var connectionString = String.Format("mongodb://127.0.0.1/{0}{1}?strict=false", type, _num);
12+
var connectionString = String.Format("mongodb://127.0.0.1");
1313
_num++;
1414

1515
return connectionString;
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using MongoDB.Bson;
5+
using MongoDB.Driver;
6+
using MongoDB.Driver.Builders;
7+
using MongoDB.Driver.Linq;
8+
using NUnit.Framework;
9+
using SharpRepository.Repository;
10+
using SharpRepository.Repository.Specifications;
11+
using SharpRepository.Tests.Integration.TestAttributes;
12+
using SharpRepository.Tests.Integration.TestObjects;
13+
using Should;
14+
using SharpRepository.MongoDbRepository;
15+
16+
namespace SharpRepository.Tests.Integration
17+
{
18+
public class Order
19+
{
20+
public ObjectId Id { get; set; }
21+
public string Name { get; set; }
22+
}
23+
24+
[TestFixture]
25+
public class MongoRepositoryTests : TestBase
26+
{
27+
[Test]
28+
public void Mongo()
29+
{
30+
MongoServer server = MongoServer.Create("mongodb://localhost");
31+
server.DropDatabase("Order");
32+
MongoDatabase database = server.GetDatabase("Order");
33+
MongoCollection<Order> orders = database.GetCollection<Order>("Order");
34+
35+
Console.WriteLine("* CREATE *");
36+
37+
var create = new Order { Name = "Big sale" };
38+
database.GetCollection<Order>("Order").Insert(create);
39+
40+
foreach (var order in database.GetCollection<Order>("Order").AsQueryable())
41+
{
42+
Console.WriteLine(order.Name + ", " + order.Id);
43+
}
44+
45+
Console.WriteLine("* READ *");
46+
47+
var read = orders.AsQueryable().FirstOrDefault(e => e.Id == create.Id);
48+
read.Name.ShouldEqual(create.Name);
49+
50+
Console.WriteLine("* UPDATE *");
51+
52+
read.Name = "Really big sale";
53+
database.GetCollection<Order>("Order").Save(read);
54+
55+
foreach (var order in database.GetCollection<Order>("Order").AsQueryable())
56+
{
57+
Console.WriteLine(order.Name + ", " + order.Id);
58+
}
59+
60+
var update = database.GetCollection<Order>("Order").AsQueryable().FirstOrDefault(e => e.Id == read.Id);
61+
update.Id.ShouldEqual(read.Id);
62+
update.Name.ShouldEqual(read.Name);
63+
64+
Console.WriteLine("* DELETE *");
65+
66+
var delete = database.GetCollection<Order>("Order").AsQueryable().FirstOrDefault(e => e.Id == update.Id);
67+
68+
database.GetCollection<Order>("Order").Remove(Query.EQ("_id", delete.Id));
69+
70+
foreach (var order in database.GetCollection<Order>("Order").AsQueryable())
71+
{
72+
Console.WriteLine(order.Name + ", " + order.Id);
73+
}
74+
75+
database.GetCollection<Order>("Order").RemoveAll();
76+
77+
Console.WriteLine("* DELETE ALL *");
78+
79+
foreach (var order in database.GetCollection<Order>("Order").AsQueryable())
80+
{
81+
Console.WriteLine(order.Name + ", " + order.Id);
82+
}
83+
84+
server.DropDatabase("Order");
85+
}
86+
87+
[Test]
88+
public void MongoRepository_Supports_Basic_Crud_Operations()
89+
{
90+
const string connectionString = "mongodb://127.0.0.1";
91+
var repo = new MongoDbRepository<Order, ObjectId>(connectionString);
92+
93+
// Create
94+
var create = new Order { Name = "Big sale" };
95+
repo.Add(create);
96+
97+
// Read
98+
var read = repo.Get(create.Id);
99+
read.Name.ShouldEqual(create.Name);
100+
101+
// Update
102+
read.Name = "Really big sale";
103+
repo.Update(read);
104+
105+
var update = repo.Get(read.Id);
106+
update.Id.ShouldEqual(read.Id);
107+
update.Name.ShouldEqual(read.Name);
108+
109+
// Delete
110+
repo.Delete(update);
111+
var delete = repo.Get(read.Id);
112+
delete.ShouldBeNull();
113+
}
114+
}
115+
}

SharpRepository.Tests.Integration/SharpRepository.Tests.Integration.csproj

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,20 @@
5656
<Reference Include="Lucene.Net.Contrib.SpellChecker">
5757
<HintPath>..\packages\RavenDB-Embedded.1.0.888\lib\net40\Lucene.Net.Contrib.SpellChecker.dll</HintPath>
5858
</Reference>
59-
<Reference Include="MongoDB.Bson, Version=1.4.2.4500, Culture=neutral, PublicKeyToken=f686731cfb9cc103, processorArchitecture=MSIL" />
59+
<Reference Include="MongoDB.Bson, Version=1.4.2.4500, Culture=neutral, PublicKeyToken=f686731cfb9cc103, processorArchitecture=MSIL">
60+
<SpecificVersion>False</SpecificVersion>
61+
<HintPath>..\packages\mongocsharpdriver.1.4.2\lib\net35\MongoDB.Bson.dll</HintPath>
62+
</Reference>
63+
<Reference Include="MongoDB.Driver, Version=1.4.2.4500, Culture=neutral, PublicKeyToken=f686731cfb9cc103, processorArchitecture=MSIL">
64+
<SpecificVersion>False</SpecificVersion>
65+
<HintPath>..\packages\mongocsharpdriver.1.4.2\lib\net35\MongoDB.Driver.dll</HintPath>
66+
</Reference>
6067
<Reference Include="Newtonsoft.Json">
6168
<HintPath>..\packages\Newtonsoft.Json.4.0.8\lib\net40\Newtonsoft.Json.dll</HintPath>
6269
</Reference>
6370
<Reference Include="NLog">
6471
<HintPath>..\packages\NLog.2.0.0.2000\lib\net40\NLog.dll</HintPath>
6572
</Reference>
66-
<Reference Include="Norm">
67-
<HintPath>..\packages\NoRM.0.9.8\lib\Norm.dll</HintPath>
68-
</Reference>
6973
<Reference Include="nunit.framework">
7074
<HintPath>..\packages\NUnit.2.6.0.12054\lib\nunit.framework.dll</HintPath>
7175
</Reference>
@@ -121,6 +125,7 @@
121125
<Compile Include="Data\EfDataDirectoryFactory.cs" />
122126
<Compile Include="Data\MongoDbDataDirectoryFactory.cs" />
123127
<Compile Include="Helpers\CurrentDirectory.cs" />
128+
<Compile Include="MongoRepositoryTests.cs" />
124129
<Compile Include="Spikes\RavenDbEmbeddedTests.cs" />
125130
<Compile Include="TestAttributes\ExecuteForAllRepositoriesAttribute.cs" />
126131
<Compile Include="TestAttributes\ExecuteForRepositoriesAttribute.cs" />

SharpRepository.Tests.Integration/TestObjects/Contact.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
using System;
21
using System.Collections.Generic;
3-
using Norm;
2+
using MongoDB.Bson;
43

54
namespace SharpRepository.Tests.Integration.TestObjects
65
{
76
public class Contact
87
{
9-
[MongoIdentifier]
8+
public ObjectId Id { get; set; }
109
public int ContactId { get; set; }
1110
public string Name { get; set; }
1211
public string Title { get; set; }

SharpRepository.Tests.Integration/packages.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
3+
<package id="mongocsharpdriver" version="1.4.2" />
34
<package id="Newtonsoft.Json" version="4.0.8" />
45
<package id="NLog" version="2.0.0.2000" />
56
<package id="NUnit" version="2.6.0.12054" />
-287 KB
Binary file not shown.

0 commit comments

Comments
 (0)