11using System ;
22using System . Linq ;
3- using Norm ;
3+ using MongoDB . Bson ;
4+ using MongoDB . Driver ;
5+ using MongoDB . Driver . Builders ;
6+ using MongoDB . Driver . Linq ;
47using SharpRepository . Repository ;
58using SharpRepository . Repository . Caching ;
69using 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}
0 commit comments