99using MongoDB . Bson . Serialization . IdGenerators ;
1010using MongoDB . Bson . Serialization . Options ;
1111using MongoDB . Driver ;
12- using MongoDB . Driver . Builders ;
1312using MongoDB . Driver . Linq ;
1413using SharpRepository . Repository ;
1514using SharpRepository . Repository . Caching ;
1615using SharpRepository . Repository . FetchStrategies ;
1716using SharpRepository . Repository . Helpers ;
1817using SharpRepository . Repository . Specifications ;
18+ using MongoDB . Bson . Serialization . Serializers ;
1919
2020namespace SharpRepository . MongoDbRepository
2121{
2222 public class MongoDbRepositoryBase < T , TKey > : LinqRepositoryBase < T , TKey > where T : class , new ( )
2323 {
2424 private readonly string _databaseName ;
25- protected MongoDatabase Database ;
26- protected MongoServer Server ;
27-
28- private readonly Dictionary < Type , BsonType > _keyTypeToBsonType = new Dictionary < Type , BsonType >
29- {
30- { typeof ( string ) , BsonType . String } ,
31- { typeof ( Guid ) , BsonType . ObjectId } ,
32- { typeof ( ObjectId ) , BsonType . ObjectId } ,
33- { typeof ( byte [ ] ) , BsonType . ObjectId }
34- } ;
35-
36- private readonly Dictionary < Type , IIdGenerator > _keyTypeToBsonGenerator = new Dictionary < Type , IIdGenerator >
37- {
38- { typeof ( string ) , new StringObjectIdGenerator ( ) } ,
39- { typeof ( Guid ) , new GuidGenerator ( ) } ,
40- { typeof ( ObjectId ) , new ObjectIdGenerator ( ) } ,
41- { typeof ( byte [ ] ) , new BsonBinaryDataGuidGenerator ( GuidRepresentation . Standard ) }
42- } ;
25+ protected IMongoDatabase Database ;
26+
27+ private readonly Dictionary < Type , BsonType > _keyTypeToBsonType =
28+ new Dictionary < Type , BsonType >
29+ {
30+ { typeof ( string ) , BsonType . String } ,
31+ { typeof ( Guid ) , BsonType . ObjectId } ,
32+ { typeof ( ObjectId ) , BsonType . ObjectId } ,
33+ { typeof ( byte [ ] ) , BsonType . ObjectId }
34+ } ;
35+
36+ private readonly Dictionary < Type , IIdGenerator > _keyTypeToBsonGenerator =
37+ new Dictionary < Type , IIdGenerator >
38+ {
39+ { typeof ( string ) , new StringObjectIdGenerator ( ) } ,
40+ { typeof ( Guid ) , new GuidGenerator ( ) } ,
41+ { typeof ( ObjectId ) , new ObjectIdGenerator ( ) } ,
42+ { typeof ( byte [ ] ) , new BsonBinaryDataGuidGenerator ( GuidRepresentation . Standard ) }
43+ } ;
4344
4445 internal MongoDbRepositoryBase ( ICachingStrategy < T , TKey > cachingStrategy = null )
4546 : base ( cachingStrategy )
@@ -51,50 +52,51 @@ internal MongoDbRepositoryBase(string connectionString, ICachingStrategy<T, TKey
5152 : base ( cachingStrategy )
5253 {
5354 _databaseName = MongoUrl . Create ( connectionString ) . DatabaseName ;
54- Initialize ( new MongoClient ( connectionString ) . GetServer ( ) ) ;
55+ var cli = new MongoClient ( connectionString ) ;
56+ Initialize ( cli . GetDatabase ( _databaseName ) ) ;
5557 }
5658
57- internal MongoDbRepositoryBase ( MongoServer mongoServer , ICachingStrategy < T , TKey > cachingStrategy = null )
59+ internal MongoDbRepositoryBase ( IMongoDatabase mongoDatabase , ICachingStrategy < T , TKey > cachingStrategy = null )
5860 : base ( cachingStrategy )
5961 {
60- Initialize ( mongoServer ) ;
62+ Initialize ( mongoDatabase ) ;
6163 }
6264
6365 private string DatabaseName
6466 {
6567 get { return string . IsNullOrEmpty ( _databaseName ) ? TypeName : _databaseName ; }
6668 }
6769
68- private void Initialize ( MongoServer mongoServer = null )
70+ private void Initialize ( IMongoDatabase mongoDatabase = null )
6971 {
70- Server = mongoServer ?? new MongoClient ( "mongodb://localhost" ) . GetServer ( ) ;
71- Database = Server . GetDatabase ( DatabaseName ) ;
72+ Database = mongoDatabase ?? new MongoClient ( "mongodb://localhost" ) . GetDatabase ( MongoUrl . Create ( "mongodb://localhost" ) . DatabaseName ) ;
7273
7374 if ( ! BsonClassMap . IsClassMapRegistered ( typeof ( T ) ) )
7475 {
7576 var primaryKeyPropInfo = GetPrimaryKeyPropertyInfo ( ) ;
7677 var primaryKeyName = primaryKeyPropInfo . Name ;
7778
7879 BsonClassMap . RegisterClassMap < T > ( cm =>
79- {
80- cm . AutoMap ( ) ;
81- if ( cm . IdMemberMap == null )
82- {
83- cm . SetIdMember ( cm . GetMemberMap ( primaryKeyName ) ) ;
84-
85- if ( _keyTypeToBsonType . ContainsKey ( typeof ( TKey ) ) && ( _keyTypeToBsonGenerator . ContainsKey ( typeof ( TKey ) ) ) )
86- {
87- cm . IdMemberMap . SetRepresentation ( _keyTypeToBsonType [ typeof ( TKey ) ] ) ;
88- cm . IdMemberMap . SetIdGenerator ( _keyTypeToBsonGenerator [ typeof ( TKey ) ] ) ;
89- }
90- }
91-
92- cm . Freeze ( ) ;
93- } ) ;
80+ {
81+ cm . AutoMap ( ) ;
82+ if ( cm . IdMemberMap == null )
83+ {
84+ cm . SetIdMember ( cm . GetMemberMap ( primaryKeyName ) ) ;
85+
86+ if ( _keyTypeToBsonType . ContainsKey ( typeof ( TKey ) ) && ( _keyTypeToBsonGenerator . ContainsKey ( typeof ( TKey ) ) ) )
87+ {
88+ cm . IdMemberMap . SetSerializer ( new StringSerializer ( _keyTypeToBsonType [ typeof ( TKey ) ] ) ) ;
89+ cm . IdMemberMap . SetIdGenerator ( _keyTypeToBsonGenerator [ typeof ( TKey ) ] ) ;
90+ }
91+ }
92+
93+ cm . Freeze ( ) ;
94+ }
95+ ) ;
9496 }
9597 }
9698
97- private MongoCollection < T > BaseCollection ( )
99+ private IMongoCollection < T > BaseCollection ( )
98100 {
99101 return Database . GetCollection < T > ( TypeName ) ;
100102 }
@@ -106,11 +108,13 @@ protected override IQueryable<T> BaseQuery(IFetchStrategy<T> fetchStrategy = nul
106108
107109 protected override T GetQuery ( TKey key , IFetchStrategy < T > fetchStrategy )
108110 {
109- var keyBsonType = ( ( RepresentationSerializationOptions ) BsonClassMap . LookupClassMap ( typeof ( T ) ) . IdMemberMap . SerializationOptions ) . Representation ;
110-
111- return IsValidKey ( key )
112- ? BaseCollection ( ) . FindOneById ( BsonTypeMapper . MapToBsonValue ( key , keyBsonType ) )
113- : default ( T ) ;
111+ var keyBsonType = ( ( StringSerializer ) BsonClassMap . LookupClassMap ( typeof ( T ) ) . IdMemberMap . GetSerializer ( ) ) . Representation ;
112+ var keyMemberName = BsonClassMap . LookupClassMap ( typeof ( T ) ) . IdMemberMap . MemberName ;
113+ if ( IsValidKey ( key ) ) {
114+ var keyBsonValue = BsonTypeMapper . MapToBsonValue ( key , keyBsonType ) ;
115+ var filter = Builders < T > . Filter . Eq ( keyMemberName , keyBsonValue ) ;
116+ return BaseCollection ( ) . Find ( filter ) . FirstOrDefault ( ) ;
117+ } else return default ( T ) ;
114118 }
115119
116120 public override int Sum ( ISpecification < T > criteria , Expression < Func < T , int > > selector )
@@ -295,7 +299,7 @@ public override double Average(ISpecification<T> criteria, Expression<Func<T, lo
295299
296300 protected override void AddItem ( T entity )
297301 {
298- BaseCollection ( ) . Insert ( entity ) ;
302+ BaseCollection ( ) . InsertOne ( entity ) ;
299303 }
300304
301305 protected override void DeleteItem ( T entity )
@@ -305,15 +309,46 @@ protected override void DeleteItem(T entity)
305309
306310 if ( IsValidKey ( pkValue ) )
307311 {
308- var keyMemberMap = BsonClassMap . LookupClassMap ( typeof ( T ) ) . IdMemberMap ;
309- var keyBsonType = ( ( RepresentationSerializationOptions ) keyMemberMap . SerializationOptions ) . Representation ;
310- BaseCollection ( ) . Remove ( Query . EQ ( keyMemberMap . ElementName , BsonTypeMapper . MapToBsonValue ( pkValue , keyBsonType ) ) ) ;
312+ var keyPropertyName = BsonClassMap . LookupClassMap ( typeof ( T ) ) . IdMemberMap . ElementName ;
313+ var keyPair = GetMongoProperty ( entity , keyPropertyName ) ;
314+ var filter = Builders < T > . Filter . Eq ( keyPair . Key , keyPair . Value ) ;
315+
316+ BaseCollection ( ) . DeleteOne ( filter ) ;
311317 }
312318 }
313319
314320 protected override void UpdateItem ( T entity )
315321 {
316- BaseCollection ( ) . Save ( entity ) ;
322+ TKey pkValue ;
323+ GetPrimaryKey ( entity , out pkValue ) ;
324+ if ( IsValidKey ( pkValue ) )
325+ {
326+ var keyPropertyName = BsonClassMap . LookupClassMap ( typeof ( T ) ) . IdMemberMap . ElementName ;
327+ var keyPair = GetMongoProperty ( entity , keyPropertyName ) ;
328+ var filter = Builders < T > . Filter . Eq ( keyPair . Key , keyPair . Value ) ;
329+
330+
331+ var bsonMembers = BsonClassMap . LookupClassMap ( typeof ( T ) ) . AllMemberMaps . Where ( m => m . MemberName != keyPropertyName ) ;
332+ var updates = new List < UpdateDefinition < T > > ( ) ;
333+ foreach ( var members in bsonMembers )
334+ {
335+ var propPair = GetMongoProperty ( entity , members . MemberName ) ;
336+ updates . Add ( Builders < T > . Update . Set ( propPair . Key , propPair . Value ) ) ;
337+ }
338+
339+ BaseCollection ( ) . UpdateOne ( filter , Builders < T > . Update . Combine ( updates ) ) ;
340+ }
341+
342+ }
343+
344+ public static KeyValuePair < string , BsonValue > GetMongoProperty ( T entity , string propertyName )
345+ {
346+ var value = typeof ( T ) . GetProperty ( propertyName ) . GetValue ( entity ) ;
347+ var memberMap = BsonClassMap . LookupClassMap ( typeof ( TKey ) ) . GetMemberMap ( propertyName ) ;
348+ var keyBsonType = ( ( StringSerializer ) memberMap . GetSerializer ( ) ) . Representation ;
349+ var bsonPropertyValue = BsonTypeMapper . MapToBsonValue ( value , keyBsonType ) ;
350+
351+ return new KeyValuePair < string , BsonValue > ( propertyName , bsonPropertyValue ) ;
317352 }
318353
319354 protected override void SaveChanges ( )
0 commit comments