@@ -22,12 +22,25 @@ public class CollectionBase<TEntity>
22
22
23
23
private readonly IMongoDatabase mongoDatabase ;
24
24
private readonly IMongoClient mongoClient ;
25
- private readonly Lazy < IMongoCollection < TEntity > > mongoCollection ;
25
+ private readonly Action < MongoCollectionSettings > collectionConfigurator ;
26
+ private IMongoCollection < TEntity > mongoCollection ;
27
+ private readonly object mongoCollectionInitializerLock = new ( ) ;
26
28
private readonly bool createShardKey ;
27
29
28
30
protected IMongoCollection < TEntity > Collection
29
31
{
30
- get { return mongoCollection . Value ; }
32
+ get
33
+ {
34
+ if ( mongoCollection == null )
35
+ {
36
+ lock ( mongoCollectionInitializerLock )
37
+ {
38
+ mongoCollection ??= CreateCollection ( collectionConfigurator ) ;
39
+ }
40
+ }
41
+
42
+ return mongoCollection ;
43
+ }
31
44
}
32
45
33
46
protected IMongoDatabase Database
@@ -44,9 +57,9 @@ protected CollectionBase(IMongoClient mongoClient, string databaseName,
44
57
Action < MongoCollectionSettings > collectionConfigurator , bool createShardKey )
45
58
{
46
59
this . mongoClient = mongoClient ;
60
+ this . collectionConfigurator = collectionConfigurator ;
47
61
48
62
mongoDatabase = mongoClient . GetDatabase ( databaseName ) ;
49
- mongoCollection = CreateCollection ( collectionConfigurator ) ;
50
63
51
64
this . createShardKey = createShardKey ;
52
65
}
@@ -65,51 +78,40 @@ protected virtual void SetupCollection(IMongoCollection<TEntity> collection)
65
78
{
66
79
}
67
80
68
- private Lazy < IMongoCollection < TEntity > > CreateCollection ( Action < MongoCollectionSettings > collectionConfigurator )
81
+ private IMongoCollection < TEntity > CreateCollection ( Action < MongoCollectionSettings > collectionConfigurator )
69
82
{
70
- return new Lazy < IMongoCollection < TEntity > > ( ( ) =>
71
- {
72
- var collectionFilter = new ListCollectionNamesOptions
73
- {
74
- Filter = Builders < BsonDocument > . Filter . Eq ( "name" , CollectionName ( ) )
75
- } ;
83
+ var collectionName = CollectionName ( ) ;
76
84
77
- if ( ! mongoDatabase . ListCollectionNames ( collectionFilter ) . Any ( ) )
78
- {
79
- mongoDatabase . CreateCollection ( CollectionName ( ) ) ;
80
- }
81
-
82
- var collectionSettings = CollectionSettings ( ) ?? new MongoCollectionSettings ( ) ;
85
+ var collectionSettings = CollectionSettings ( ) ?? new MongoCollectionSettings ( ) ;
83
86
84
- collectionConfigurator ? . Invoke ( collectionSettings ) ;
87
+ collectionConfigurator ? . Invoke ( collectionSettings ) ;
85
88
86
- var databaseCollection = mongoDatabase . GetCollection < TEntity > (
87
- CollectionName ( ) ,
88
- collectionSettings ) ;
89
+ var databaseCollection = mongoDatabase . GetCollection < TEntity > (
90
+ collectionName ,
91
+ collectionSettings ) ;
89
92
90
- if ( this . createShardKey )
93
+ if ( createShardKey )
94
+ {
95
+ try
91
96
{
92
- try
97
+ mongoClient . GetDatabase ( "admin" ) . RunCommand < BsonDocument > ( new BsonDocument
93
98
{
94
- Database . RunCommand < BsonDocument > ( new BsonDocument
99
+ [ "shardCollection" ] = $ "{ mongoDatabase . DatabaseNamespace . DatabaseName } .{ collectionName } ",
100
+ [ "key" ] = new BsonDocument
95
101
{
96
- [ "key" ] = new BsonDocument
97
- {
98
- [ "_id" ] = "hashed"
99
- } ,
100
- [ "shardCollection" ] = $ "{ mongoDatabase . DatabaseNamespace . DatabaseName } .{ CollectionName ( ) } "
101
- } ) ;
102
- }
103
- catch ( MongoException )
104
- {
105
- // Shared key probably created already.
106
- }
102
+ [ "_id" ] = "hashed"
103
+ }
104
+ } ) ;
105
+ }
106
+ catch ( MongoException )
107
+ {
108
+ // Shared key probably created already.
107
109
}
110
+ }
108
111
109
- SetupCollection ( databaseCollection ) ;
112
+ SetupCollection ( databaseCollection ) ;
110
113
111
- return databaseCollection ;
112
- } ) ;
114
+ return databaseCollection ;
113
115
}
114
116
}
115
117
}
0 commit comments