-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasicObjectStorage.cs
More file actions
698 lines (599 loc) · 21 KB
/
BasicObjectStorage.cs
File metadata and controls
698 lines (599 loc) · 21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
Store any C# object in mongodb property
When storing C# objects in MongoDB, you have several approaches depending on your needs. Here's how to handle different scenarios:
Basic Object Storage
For simple objects, the MongoDB C# driver handles serialization automatically:
csharp
public class User
{
public ObjectId Id { get; set; }
public string Name { get; set; }
public Address Address { get; set; } // Nested object
public List<string> Tags { get; set; }
}
public class Address
{
public string Street { get; set; }
public string City { get; set; }
public string Country { get; set; }
}
// Store the object
var collection = database.GetCollection<User>("users");
var user = new User
{
Name = "John Doe",
Address = new Address { Street = "123 Main St", City = "NYC", Country = "USA" },
Tags = new List<string> { "developer", "architect" }
};
await collection.InsertOneAsync(user);
Storing Complex/Dynamic Objects
For storing any C# object as a property, you can use BsonDocument or object type:
csharp
public class FlexibleDocument
{
public ObjectId Id { get; set; }
public string Name { get; set; }
public BsonDocument DynamicData { get; set; } // Can store any object
public object AnyObject { get; set; } // Alternative approach
}
// Usage
var doc = new FlexibleDocument
{
Name = "Sample Document",
DynamicData = new BsonDocument
{
["customProperty"] = "value",
["nestedObject"] = new BsonDocument
{
["prop1"] = 123,
["prop2"] = DateTime.Now
}
},
AnyObject = new { CustomProp = "test", Number = 42 }
};
Using BsonSerializer for Complex Types
For complete control over serialization:
csharp
public class Document
{
public ObjectId Id { get; set; }
public string Name { get; set; }
public BsonDocument SerializedObject { get; set; }
}
// Serialize any object to BsonDocument
public BsonDocument SerializeObject<T>(T obj)
{
return obj.ToBsonDocument();
}
// Deserialize back to specific type
public T DeserializeObject<T>(BsonDocument bsonDoc)
{
return BsonSerializer.Deserialize<T>(bsonDoc);
}
// Usage
var myComplexObject = new { Name = "Test", Values = new[] { 1, 2, 3 } };
var document = new Document
{
Name = "Container",
SerializedObject = SerializeObject(myComplexObject)
};
Custom Serialization with Attributes
For fine-grained control:
csharp
public class CustomDocument
{
public ObjectId Id { get; set; }
[BsonElement("custom_name")]
public string Name { get; set; }
[BsonIgnoreIfNull]
public object OptionalData { get; set; }
[BsonRepresentation(BsonType.String)]
public DateTime CreatedAt { get; set; }
}
Repository Pattern Implementation
Here's a clean architectural approach:
csharp
public interface IDocumentRepository<T>
{
Task<T> GetByIdAsync(ObjectId id);
Task InsertAsync(T document);
Task<T> UpdatePropertyAsync(ObjectId id, string propertyName, object value);
}
public class MongoDocumentRepository<T> : IDocumentRepository<T>
{
private readonly IMongoCollection<T> _collection;
public MongoDocumentRepository(IMongoDatabase database, string collectionName)
{
_collection = database.GetCollection<T>(collectionName);
}
public async Task<T> UpdatePropertyAsync(ObjectId id, string propertyName, object value)
{
var filter = Builders<T>.Filter.Eq("_id", id);
var update = Builders<T>.Update.Set(propertyName, value.ToBsonDocument());
return await _collection.FindOneAndUpdateAsync(filter, update,
new FindOneAndUpdateOptions<T> { ReturnDocument = ReturnDocument.After });
}
}
Best Practices
Use strongly typed models when the structure is known
Use BsonDocument for truly dynamic scenarios
Consider indexing on properties you'll query frequently
Implement proper error handling for serialization failures
Use the Repository pattern to abstract MongoDB operations
The MongoDB C# driver is quite flexible and can handle most object serialization scenarios automatically, but these approaches give you control when you need it.
Store c# dictionary
Here are several ways to store C# dictionaries in MongoDB, each with different use cases:
Basic Dictionary Storage
MongoDB C# driver handles Dictionary<string, T> automatically:
csharp
public class DocumentWithDictionary
{
public ObjectId Id { get; set; }
public string Name { get; set; }
public Dictionary<string, string> Properties { get; set; }
public Dictionary<string, int> Scores { get; set; }
public Dictionary<string, object> DynamicData { get; set; }
}
// Usage
var document = new DocumentWithDictionary
{
Name = "Sample Document",
Properties = new Dictionary<string, string>
{
["color"] = "blue",
["size"] = "large",
["category"] = "electronics"
},
Scores = new Dictionary<string, int>
{
["performance"] = 95,
["reliability"] = 87,
["usability"] = 92
},
DynamicData = new Dictionary<string, object>
{
["created"] = DateTime.Now,
["active"] = true,
["metadata"] = new { version = "1.0", author = "system" }
}
};
await collection.InsertOneAsync(document);
Complex Value Types in Dictionary
csharp
public class User
{
public string Name { get; set; }
public string Email { get; set; }
public DateTime LastLogin { get; set; }
}
public class DocumentWithComplexDictionary
{
public ObjectId Id { get; set; }
public Dictionary<string, User> Users { get; set; }
public Dictionary<string, List<string>> Tags { get; set; }
public Dictionary<string, Dictionary<string, object>> NestedData { get; set; }
}
// Usage
var document = new DocumentWithComplexDictionary
{
Users = new Dictionary<string, User>
{
["admin"] = new User { Name = "Admin User", Email = "admin@example.com", LastLogin = DateTime.Now },
["guest"] = new User { Name = "Guest User", Email = "guest@example.com", LastLogin = DateTime.Now.AddDays(-1) }
},
Tags = new Dictionary<string, List<string>>
{
["frontend"] = new List<string> { "angular", "typescript", "css" },
["backend"] = new List<string> { "c#", "mongodb", "webapi" }
}
};
Non-String Keys with Custom Serialization
For dictionaries with non-string keys, you need custom serialization:
csharp
public class IntKeyDictionary
{
public ObjectId Id { get; set; }
public string Name { get; set; }
[BsonDictionaryOptions(DictionaryRepresentation.ArrayOfDocuments)]
public Dictionary<int, string> IntegerKeys { get; set; }
[BsonDictionaryOptions(DictionaryRepresentation.ArrayOfDocuments)]
public Dictionary<DateTime, decimal> DateTimeKeys { get; set; }
}
// Alternative: Convert to string keys manually
public class ConvertedKeyDictionary
{
public ObjectId Id { get; set; }
public Dictionary<string, string> Data { get; set; }
// Helper methods
public void SetIntValue(int key, string value)
{
Data ??= new Dictionary<string, string>();
Data[key.ToString()] = value;
}
public string GetIntValue(int key)
{
return Data?.GetValueOrDefault(key.ToString());
}
}
Repository Pattern for Dictionary Operations
csharp
public interface IDictionaryRepository
{
Task UpdateDictionaryPropertyAsync(ObjectId id, string dictionaryField, string key, object value);
Task RemoveDictionaryKeyAsync(ObjectId id, string dictionaryField, string key);
Task<Dictionary<string, object>> GetDictionaryAsync(ObjectId id, string dictionaryField);
}
public class MongoDictionaryRepository : IDictionaryRepository
{
private readonly IMongoCollection<BsonDocument> _collection;
public MongoDictionaryRepository(IMongoDatabase database, string collectionName)
{
_collection = database.GetCollection<BsonDocument>(collectionName);
}
public async Task UpdateDictionaryPropertyAsync(ObjectId id, string dictionaryField, string key, object value)
{
var filter = Builders<BsonDocument>.Filter.Eq("_id", id);
var update = Builders<BsonDocument>.Update.Set($"{dictionaryField}.{key}", value);
await _collection.UpdateOneAsync(filter, update);
}
public async Task RemoveDictionaryKeyAsync(ObjectId id, string dictionaryField, string key)
{
var filter = Builders<BsonDocument>.Filter.Eq("_id", id);
var update = Builders<BsonDocument>.Update.Unset($"{dictionaryField}.{key}");
await _collection.UpdateOneAsync(filter, update);
}
public async Task<Dictionary<string, object>> GetDictionaryAsync(ObjectId id, string dictionaryField)
{
var filter = Builders<BsonDocument>.Filter.Eq("_id", id);
var projection = Builders<BsonDocument>.Projection.Include(dictionaryField);
var document = await _collection.Find(filter).Project(projection).FirstOrDefaultAsync();
if (document?.Contains(dictionaryField) == true)
{
var bsonDict = document[dictionaryField].AsBsonDocument;
return bsonDict.ToDictionary(kvp => kvp.Name, kvp => BsonTypeMapper.MapToDotNetValue(kvp.Value));
}
return new Dictionary<string, object>();
}
}
Using BsonDocument for Maximum Flexibility
csharp
public class FlexibleDocument
{
public ObjectId Id { get; set; }
public string Name { get; set; }
public BsonDocument DynamicProperties { get; set; }
}
// Helper class for dictionary operations
public static class DictionaryHelpers
{
public static BsonDocument ToBsonDocument<T>(this Dictionary<string, T> dictionary)
{
var bsonDoc = new BsonDocument();
foreach (var kvp in dictionary)
{
bsonDoc[kvp.Key] = BsonValue.Create(kvp.Value);
}
return bsonDoc;
}
public static Dictionary<string, T> ToDictionary<T>(this BsonDocument bsonDoc)
{
return bsonDoc.ToDictionary(
kvp => kvp.Name,
kvp => BsonSerializer.Deserialize<T>(kvp.Value)
);
}
}
// Usage
var dictionary = new Dictionary<string, object>
{
["name"] = "John Doe",
["age"] = 30,
["active"] = true,
["scores"] = new List<int> { 85, 92, 78 }
};
var document = new FlexibleDocument
{
Name = "User Document",
DynamicProperties = dictionary.ToBsonDocument()
};
Querying Dictionary Fields
csharp
// Query documents where dictionary contains specific key
var filter = Builders<DocumentWithDictionary>.Filter.Exists("Properties.color");
// Query by dictionary value
var colorFilter = Builders<DocumentWithDictionary>.Filter.Eq("Properties.color", "blue");
// Query nested dictionary values
var nestedFilter = Builders<DocumentWithDictionary>.Filter.Eq("DynamicData.metadata.version", "1.0");
// Find documents with dictionary key matching pattern
var regexFilter = Builders<BsonDocument>.Filter.Regex("Properties.category", new BsonRegularExpression("elect.*"));
var results = await collection.Find(colorFilter).ToListAsync();
Best Practices
Use string keys when possible for better MongoDB compatibility
Consider indexing frequently queried dictionary fields
Validate dictionary keys to avoid conflicts with MongoDB operators ($, .)
Use BsonDocument for maximum flexibility with dynamic data
Implement proper null checking when accessing dictionary values
Consider document size limits (16MB) when storing large dictionaries
The MongoDB C# driver handles most dictionary scenarios seamlessly, making it easy to work with key-value data in your applications.
Store C# dictionary and object in same collection property called document
Here are several approaches to store both C# dictionaries and objects in the same MongoDB collection property called "document":
Using object Type with Runtime Type Checking
csharp
public class FlexibleContainer
{
public ObjectId Id { get; set; }
public string Name { get; set; }
public string DocumentType { get; set; } // "dictionary" or "object"
public object Document { get; set; } // Can be Dictionary or any object
}
// Usage
var collection = database.GetCollection<FlexibleContainer>("containers");
// Store dictionary
var dictContainer = new FlexibleContainer
{
Name = "Dictionary Document",
DocumentType = "dictionary",
Document = new Dictionary<string, object>
{
["name"] = "John Doe",
["age"] = 30,
["skills"] = new[] { "C#", "MongoDB", "Angular" }
}
};
// Store custom object
var objContainer = new FlexibleContainer
{
Name = "Object Document",
DocumentType = "object",
Document = new User
{
Name = "Jane Smith",
Email = "jane@example.com",
CreatedAt = DateTime.Now
}
};
await collection.InsertOneAsync(dictContainer);
await collection.InsertOneAsync(objContainer);
Using BsonDocument for Universal Storage
csharp
public class UniversalContainer
{
public ObjectId Id { get; set; }
public string Name { get; set; }
public string DocumentType { get; set; }
public BsonDocument Document { get; set; } // Universal container
}
// Helper methods
public static class DocumentHelpers
{
public static BsonDocument ConvertToDocument<T>(T obj)
{
if (obj is Dictionary<string, object> dict)
{
return new BsonDocument(dict.ToDictionary(kvp => kvp.Key, kvp => BsonValue.Create(kvp.Value)));
}
return obj.ToBsonDocument();
}
public static T ConvertFromDocument<T>(BsonDocument document)
{
return BsonSerializer.Deserialize<T>(document);
}
public static Dictionary<string, object> ConvertToDictionary(BsonDocument document)
{
return document.ToDictionary(
kvp => kvp.Name,
kvp => BsonTypeMapper.MapToDotNetValue(kvp.Value)
);
}
}
// Usage
var container1 = new UniversalContainer
{
Name = "Dictionary Container",
DocumentType = "dictionary",
Document = DocumentHelpers.ConvertToDocument(new Dictionary<string, object>
{
["title"] = "Software Architect",
["experience"] = 10,
["technologies"] = new[] { "C#", "MongoDB", "iOS" }
})
};
var container2 = new UniversalContainer
{
Name = "Object Container",
DocumentType = "object",
Document = DocumentHelpers.ConvertToDocument(new User
{
Name = "Bob Wilson",
Email = "bob@example.com",
IsActive = true
})
};
Generic Repository with Type Safety
csharp
public interface IDocumentRepository
{
Task<string> StoreDocumentAsync<T>(string name, T document);
Task<T> GetDocumentAsync<T>(ObjectId id);
Task<Dictionary<string, object>> GetDictionaryAsync(ObjectId id);
Task<object> GetDocumentAsync(ObjectId id);
Task UpdateDocumentAsync<T>(ObjectId id, T document);
}
public class MongoDocumentRepository : IDocumentRepository
{
private readonly IMongoCollection<UniversalContainer> _collection;
public MongoDocumentRepository(IMongoDatabase database)
{
_collection = database.GetCollection<UniversalContainer>("documents");
}
public async Task<string> StoreDocumentAsync<T>(string name, T document)
{
var container = new UniversalContainer
{
Name = name,
DocumentType = typeof(T).Name,
Document = DocumentHelpers.ConvertToDocument(document)
};
await _collection.InsertOneAsync(container);
return container.Id.ToString();
}
public async Task<T> GetDocumentAsync<T>(ObjectId id)
{
var container = await _collection.Find(x => x.Id == id).FirstOrDefaultAsync();
if (container?.Document != null)
{
return DocumentHelpers.ConvertFromDocument<T>(container.Document);
}
return default(T);
}
public async Task<Dictionary<string, object>> GetDictionaryAsync(ObjectId id)
{
var container = await _collection.Find(x => x.Id == id).FirstOrDefaultAsync();
if (container?.Document != null)
{
return DocumentHelpers.ConvertToDictionary(container.Document);
}
return new Dictionary<string, object>();
}
public async Task<object> GetDocumentAsync(ObjectId id)
{
var container = await _collection.Find(x => x.Id == id).FirstOrDefaultAsync();
if (container?.Document != null)
{
// Return based on stored type
return container.DocumentType switch
{
"Dictionary`2" or "dictionary" => DocumentHelpers.ConvertToDictionary(container.Document),
_ => container.Document
};
}
return null;
}
public async Task UpdateDocumentAsync<T>(ObjectId id, T document)
{
var filter = Builders<UniversalContainer>.Filter.Eq(x => x.Id, id);
var update = Builders<UniversalContainer>.Update
.Set(x => x.Document, DocumentHelpers.ConvertToDocument(document))
.Set(x => x.DocumentType, typeof(T).Name);
await _collection.UpdateOneAsync(filter, update);
}
}
Advanced Pattern with Polymorphic Serialization
csharp
// Base interface for all document types
public interface IDocument
{
string GetDocumentType();
}
// Dictionary wrapper
public class DictionaryDocument : Dictionary<string, object>, IDocument
{
public string GetDocumentType() => "dictionary";
}
// Custom object base
public abstract class ObjectDocument : IDocument
{
public abstract string GetDocumentType();
}
public class UserDocument : ObjectDocument
{
public string Name { get; set; }
public string Email { get; set; }
public DateTime CreatedAt { get; set; }
public override string GetDocumentType() => "user";
}
public class ProductDocument : ObjectDocument
{
public string Title { get; set; }
public decimal Price { get; set; }
public List<string> Categories { get; set; }
public override string GetDocumentType() => "product";
}
// Container with polymorphic support
public class PolymorphicContainer
{
public ObjectId Id { get; set; }
public string Name { get; set; }
public IDocument Document { get; set; }
}
// Registration for polymorphic serialization
public static class MongoConfig
{
public static void RegisterPolymorphicTypes()
{
BsonClassMap.RegisterClassMap<IDocument>();
BsonClassMap.RegisterClassMap<DictionaryDocument>();
BsonClassMap.RegisterClassMap<UserDocument>();
BsonClassMap.RegisterClassMap<ProductDocument>();
}
}
Service Layer Implementation
csharp
public class DocumentService
{
private readonly IDocumentRepository _repository;
public DocumentService(IDocumentRepository repository)
{
_repository = repository;
}
// Store dictionary
public async Task<string> StoreUserPreferencesAsync(Dictionary<string, object> preferences)
{
return await _repository.StoreDocumentAsync("user_preferences", preferences);
}
// Store typed object
public async Task<string> StoreUserProfileAsync(UserProfile profile)
{
return await _repository.StoreDocumentAsync("user_profile", profile);
}
// Retrieve with type checking
public async Task<T> GetTypedDocumentAsync<T>(string documentId)
{
if (!ObjectId.TryParse(documentId, out var id))
throw new ArgumentException("Invalid document ID");
return await _repository.GetDocumentAsync<T>(id);
}
// Smart retrieval based on stored type
public async Task<object> GetDocumentAsync(string documentId)
{
if (!ObjectId.TryParse(documentId, out var id))
throw new ArgumentException("Invalid document ID");
return await _repository.GetDocumentAsync(id);
}
}
Usage Examples
csharp
// Setup
var repository = new MongoDocumentRepository(database);
var service = new DocumentService(repository);
// Store dictionary
var preferences = new Dictionary<string, object>
{
["theme"] = "dark",
["language"] = "en-US",
["notifications"] = true,
["features"] = new[] { "advanced_search", "auto_save" }
};
var dictId = await service.StoreUserPreferencesAsync(preferences);
// Store object
var profile = new UserProfile
{
Name = "Software Architect",
Skills = new[] { "C#", "Objective-C", "Angular", "MongoDB" },
YearsExperience = 10
};
var objId = await service.StoreUserProfileAsync(profile);
// Retrieve
var retrievedDict = await service.GetTypedDocumentAsync<Dictionary<string, object>>(dictId);
var retrievedProfile = await service.GetTypedDocumentAsync<UserProfile>(objId);
// Smart retrieval
var unknownDoc1 = await service.GetDocumentAsync(dictId); // Returns dictionary
var unknownDoc2 = await service.GetDocumentAsync(objId); // Returns UserProfile
Best Practices
Use type indicators to safely deserialize documents
Implement proper error handling for type mismatches
Consider indexing the DocumentType field for queries
Use BsonDocument for maximum flexibility
Validate data before storage to prevent corruption
Consider document size limits when storing large objects
Use repository pattern to abstract storage logic
This approach gives you maximum flexibility while maintaining type safety and performance in your MongoDB operations.