@@ -18,7 +18,7 @@ protected virtual void OnDatabaseCreating(CouchDatabaseBuilder databaseBuilder)
18
18
19
19
private static readonly MethodInfo InitDatabasesGenericMethod
20
20
= typeof ( CouchContext ) . GetMethods ( BindingFlags . NonPublic | BindingFlags . Instance )
21
- . Single ( mi => mi . Name == nameof ( InitDatabasesAsync ) ) ;
21
+ . Single ( mi => mi . Name == nameof ( InitDatabaseAsync ) ) ;
22
22
23
23
private static readonly MethodInfo ApplyDatabaseChangesGenericMethod
24
24
= typeof ( CouchContext ) . GetMethods ( BindingFlags . NonPublic | BindingFlags . Instance )
@@ -39,20 +39,9 @@ protected CouchContext(CouchOptions options)
39
39
#pragma warning restore CA2214 // Do not call overridable methods in constructors
40
40
41
41
Client = new CouchClient ( options ) ;
42
- IEnumerable < PropertyInfo > databasePropertyInfos = GetDatabaseProperties ( ) ;
43
42
44
- foreach ( PropertyInfo dbProperty in databasePropertyInfos )
45
- {
46
- Type documentType = dbProperty . PropertyType . GetGenericArguments ( ) [ 0 ] ;
47
-
48
- var initDatabasesTask = ( Task ) InitDatabasesGenericMethod . MakeGenericMethod ( documentType )
49
- . Invoke ( this , new object [ ] { dbProperty , options } ) ;
50
- initDatabasesTask . ConfigureAwait ( false ) . GetAwaiter ( ) . GetResult ( ) ;
51
-
52
- var applyDatabaseChangesTask = ( Task ) ApplyDatabaseChangesGenericMethod . MakeGenericMethod ( documentType )
53
- . Invoke ( this , new object [ ] { dbProperty , options , databaseBuilder } ) ;
54
- applyDatabaseChangesTask . ConfigureAwait ( false ) . GetAwaiter ( ) . GetResult ( ) ;
55
- }
43
+ SetupDiscriminators ( databaseBuilder ) ;
44
+ InitializeDatabases ( options , databaseBuilder ) ;
56
45
}
57
46
58
47
public async ValueTask DisposeAsync ( )
@@ -69,26 +58,76 @@ protected virtual async Task DisposeAsync(bool disposing)
69
58
}
70
59
}
71
60
72
- private async Task InitDatabasesAsync < TSource > ( PropertyInfo propertyInfo , CouchOptions options )
61
+ private static void SetupDiscriminators ( CouchDatabaseBuilder databaseBuilder )
62
+ {
63
+ // Get all options that share the database with another one
64
+ IEnumerable < KeyValuePair < Type , CouchDocumentBuilder > > ? sharedDatabase = databaseBuilder . DocumentBuilders
65
+ . Where ( opt => opt . Value . Database != null )
66
+ . GroupBy ( v => v . Value . Database )
67
+ . Where ( g => g . Count ( ) > 1 )
68
+ . SelectMany ( g => g ) ;
69
+ foreach ( KeyValuePair < Type , CouchDocumentBuilder > option in sharedDatabase )
70
+ {
71
+ option . Value . Discriminator = option . Key . Name ;
72
+ }
73
+ }
74
+
75
+ private void InitializeDatabases ( CouchOptions options , CouchDatabaseBuilder databaseBuilder )
76
+ {
77
+ foreach ( PropertyInfo dbProperty in GetDatabaseProperties ( ) )
78
+ {
79
+ Type documentType = dbProperty . PropertyType . GetGenericArguments ( ) [ 0 ] ;
80
+
81
+ var initDatabasesTask = ( Task ) InitDatabasesGenericMethod . MakeGenericMethod ( documentType )
82
+ . Invoke ( this , new object [ ] { dbProperty , options , databaseBuilder } ) ;
83
+ initDatabasesTask . ConfigureAwait ( false ) . GetAwaiter ( ) . GetResult ( ) ;
84
+
85
+ var applyDatabaseChangesTask = ( Task ) ApplyDatabaseChangesGenericMethod . MakeGenericMethod ( documentType )
86
+ . Invoke ( this , new object [ ] { dbProperty , options , databaseBuilder } ) ;
87
+ applyDatabaseChangesTask . ConfigureAwait ( false ) . GetAwaiter ( ) . GetResult ( ) ;
88
+ }
89
+ }
90
+
91
+ private async Task InitDatabaseAsync < TSource > ( PropertyInfo propertyInfo , CouchOptions options , CouchDatabaseBuilder databaseBuilder )
73
92
where TSource : CouchDocument
74
93
{
75
- ICouchDatabase < TSource > database = options . CheckDatabaseExists
76
- ? await Client . GetOrCreateDatabaseAsync < TSource > ( ) . ConfigureAwait ( false )
77
- : Client . GetDatabase < TSource > ( ) ;
94
+ ICouchDatabase < TSource > database ;
95
+ Type documentType = typeof ( TSource ) ;
96
+
97
+ if ( databaseBuilder . DocumentBuilders . ContainsKey ( documentType ) )
98
+ {
99
+ var documentBuilder = ( CouchDocumentBuilder < TSource > ) databaseBuilder . DocumentBuilders [ documentType ] ;
100
+ var databaseName = documentBuilder . Database ?? Client . GetClassName ( documentType ) ;
101
+ database = options . CheckDatabaseExists
102
+ ? await Client . GetOrCreateDatabaseAsync < TSource > ( databaseName , documentBuilder . Shards , documentBuilder . Replicas , documentBuilder . Discriminator ) . ConfigureAwait ( false )
103
+ : Client . GetDatabase < TSource > ( databaseName , documentBuilder . Discriminator ) ;
104
+ }
105
+ else
106
+ {
107
+ database = options . CheckDatabaseExists
108
+ ? await Client . GetOrCreateDatabaseAsync < TSource > ( ) . ConfigureAwait ( false )
109
+ : Client . GetDatabase < TSource > ( ) ;
110
+ }
78
111
79
112
propertyInfo . SetValue ( this , database ) ;
80
113
}
81
114
82
115
private async Task ApplyDatabaseChangesAsync < TSource > ( PropertyInfo propertyInfo , CouchOptions options , CouchDatabaseBuilder databaseBuilder )
83
- where TSource : CouchDocument
116
+ where TSource : CouchDocument
84
117
{
85
- if ( ! databaseBuilder . DocumentBuilders . ContainsKey ( typeof ( TSource ) ) )
118
+ Type documentType = typeof ( TSource ) ;
119
+ if ( ! databaseBuilder . DocumentBuilders . ContainsKey ( documentType ) )
86
120
{
87
121
return ;
88
122
}
89
123
90
124
var database = ( CouchDatabase < TSource > ) propertyInfo . GetValue ( this ) ;
91
- var documentBuilder = ( CouchDocumentBuilder < TSource > ) databaseBuilder . DocumentBuilders [ typeof ( TSource ) ] ;
125
+ var documentBuilder = ( CouchDocumentBuilder < TSource > ) databaseBuilder . DocumentBuilders [ documentType ] ;
126
+
127
+ if ( ! documentBuilder . IndexDefinitions . Any ( ) )
128
+ {
129
+ return ;
130
+ }
92
131
93
132
List < IndexInfo > indexes = await database . GetIndexesAsync ( ) . ConfigureAwait ( false ) ;
94
133
@@ -125,7 +164,7 @@ await database.CreateIndexAsync(
125
164
{
126
165
return ;
127
166
}
128
-
167
+
129
168
IndexDefinition indexDefinition = database . NewIndexBuilder ( indexSetup . IndexBuilderAction ) . Build ( ) ;
130
169
if ( ! AreFieldsEqual ( currentIndex . Fields , indexDefinition . Fields ) )
131
170
{
0 commit comments