@@ -48,6 +48,12 @@ public interface IContextGraphBuilder
48
48
/// <typeparam name="T">The <see cref="DbContext"/> implementation type.</typeparam>
49
49
IContextGraphBuilder AddDbContext < T > ( ) where T : DbContext ;
50
50
51
+ /// <summary>
52
+ /// Specify the <see cref="IResourceNameFormatter"/> used to format resource names.
53
+ /// </summary>
54
+ /// <param name="resourceNameFormatter">Formatter used to define exposed resource names by convention.</param>
55
+ IContextGraphBuilder UseNameFormatter ( IResourceNameFormatter resourceNameFormatter ) ;
56
+
51
57
/// <summary>
52
58
/// Which links to include. Defaults to <see cref="Link.All"/>.
53
59
/// </summary>
@@ -60,6 +66,8 @@ public class ContextGraphBuilder : IContextGraphBuilder
60
66
private List < ValidationResult > _validationResults = new List < ValidationResult > ( ) ;
61
67
62
68
private bool _usesDbContext ;
69
+ private IResourceNameFormatter _resourceNameFormatter = new DefaultResourceNameFormatter ( ) ;
70
+
63
71
public Link DocumentLinks { get ; set ; } = Link . All ;
64
72
65
73
public IContextGraph Build ( )
@@ -71,12 +79,15 @@ public IContextGraph Build()
71
79
return graph ;
72
80
}
73
81
82
+ /// <inheritdoc />
74
83
public IContextGraphBuilder AddResource < TResource > ( string pluralizedTypeName ) where TResource : class , IIdentifiable < int >
75
84
=> AddResource < TResource , int > ( pluralizedTypeName ) ;
76
85
86
+ /// <inheritdoc />
77
87
public IContextGraphBuilder AddResource < TResource , TId > ( string pluralizedTypeName ) where TResource : class , IIdentifiable < TId >
78
88
=> AddResource ( typeof ( TResource ) , typeof ( TId ) , pluralizedTypeName ) ;
79
89
90
+ /// <inheritdoc />
80
91
public IContextGraphBuilder AddResource ( Type entityType , Type idType , string pluralizedTypeName )
81
92
{
82
93
AssertEntityIsNotAlreadyDefined ( entityType ) ;
@@ -152,6 +163,7 @@ protected virtual Type GetRelationshipType(RelationshipAttribute relation, Prope
152
163
153
164
private Type GetResourceDefinitionType ( Type entityType ) => typeof ( ResourceDefinition < > ) . MakeGenericType ( entityType ) ;
154
165
166
+ /// <inheritdoc />
155
167
public IContextGraphBuilder AddDbContext < T > ( ) where T : DbContext
156
168
{
157
169
_usesDbContext = true ;
@@ -174,14 +186,14 @@ public IContextGraphBuilder AddDbContext<T>() where T : DbContext
174
186
var ( isJsonApiResource , idType ) = GetIdType ( entityType ) ;
175
187
176
188
if ( isJsonApiResource )
177
- _entities . Add ( GetEntity ( GetResourceName ( property , entityType ) , entityType , idType ) ) ;
189
+ _entities . Add ( GetEntity ( GetResourceNameFromDbSetProperty ( property , entityType ) , entityType , idType ) ) ;
178
190
}
179
191
}
180
192
181
193
return this ;
182
194
}
183
195
184
- private string GetResourceName ( PropertyInfo property , Type resourceType )
196
+ private string GetResourceNameFromDbSetProperty ( PropertyInfo property , Type resourceType )
185
197
{
186
198
// check the class definition first
187
199
// [Resource("models"] public class Model : Identifiable { /* ... */ }
@@ -194,7 +206,7 @@ private string GetResourceName(PropertyInfo property, Type resourceType)
194
206
return resourceAttribute . ResourceName ;
195
207
196
208
// fallback to dsherized...this should actually check for a custom IResourceNameFormatter
197
- return property . Name . Dasherize ( ) ;
209
+ return _resourceNameFormatter . FormatResourceName ( resourceType ) ;
198
210
}
199
211
200
212
private ( bool isJsonApiResource , Type idType ) GetIdType ( Type resourceType )
@@ -213,5 +225,12 @@ private void AssertEntityIsNotAlreadyDefined(Type entityType)
213
225
if ( _entities . Any ( e => e . EntityType == entityType ) )
214
226
throw new InvalidOperationException ( $ "Cannot add entity type { entityType } to context graph, there is already an entity of that type configured.") ;
215
227
}
228
+
229
+ /// <inheritdoc />
230
+ public IContextGraphBuilder UseNameFormatter ( IResourceNameFormatter resourceNameFormatter )
231
+ {
232
+ _resourceNameFormatter = resourceNameFormatter ;
233
+ return this ;
234
+ }
216
235
}
217
236
}
0 commit comments