@@ -48,6 +48,12 @@ public interface IContextGraphBuilder
4848 /// <typeparam name="T">The <see cref="DbContext"/> implementation type.</typeparam>
4949 IContextGraphBuilder AddDbContext < T > ( ) where T : DbContext ;
5050
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+
5157 /// <summary>
5258 /// Which links to include. Defaults to <see cref="Link.All"/>.
5359 /// </summary>
@@ -60,6 +66,8 @@ public class ContextGraphBuilder : IContextGraphBuilder
6066 private List < ValidationResult > _validationResults = new List < ValidationResult > ( ) ;
6167
6268 private bool _usesDbContext ;
69+ private IResourceNameFormatter _resourceNameFormatter = new DefaultResourceNameFormatter ( ) ;
70+
6371 public Link DocumentLinks { get ; set ; } = Link . All ;
6472
6573 public IContextGraph Build ( )
@@ -71,12 +79,15 @@ public IContextGraph Build()
7179 return graph ;
7280 }
7381
82+ /// <inheritdoc />
7483 public IContextGraphBuilder AddResource < TResource > ( string pluralizedTypeName ) where TResource : class , IIdentifiable < int >
7584 => AddResource < TResource , int > ( pluralizedTypeName ) ;
7685
86+ /// <inheritdoc />
7787 public IContextGraphBuilder AddResource < TResource , TId > ( string pluralizedTypeName ) where TResource : class , IIdentifiable < TId >
7888 => AddResource ( typeof ( TResource ) , typeof ( TId ) , pluralizedTypeName ) ;
7989
90+ /// <inheritdoc />
8091 public IContextGraphBuilder AddResource ( Type entityType , Type idType , string pluralizedTypeName )
8192 {
8293 AssertEntityIsNotAlreadyDefined ( entityType ) ;
@@ -152,6 +163,7 @@ protected virtual Type GetRelationshipType(RelationshipAttribute relation, Prope
152163
153164 private Type GetResourceDefinitionType ( Type entityType ) => typeof ( ResourceDefinition < > ) . MakeGenericType ( entityType ) ;
154165
166+ /// <inheritdoc />
155167 public IContextGraphBuilder AddDbContext < T > ( ) where T : DbContext
156168 {
157169 _usesDbContext = true ;
@@ -174,14 +186,14 @@ public IContextGraphBuilder AddDbContext<T>() where T : DbContext
174186 var ( isJsonApiResource , idType ) = GetIdType ( entityType ) ;
175187
176188 if ( isJsonApiResource )
177- _entities . Add ( GetEntity ( GetResourceName ( property , entityType ) , entityType , idType ) ) ;
189+ _entities . Add ( GetEntity ( GetResourceNameFromDbSetProperty ( property , entityType ) , entityType , idType ) ) ;
178190 }
179191 }
180192
181193 return this ;
182194 }
183195
184- private string GetResourceName ( PropertyInfo property , Type resourceType )
196+ private string GetResourceNameFromDbSetProperty ( PropertyInfo property , Type resourceType )
185197 {
186198 // check the class definition first
187199 // [Resource("models"] public class Model : Identifiable { /* ... */ }
@@ -194,7 +206,7 @@ private string GetResourceName(PropertyInfo property, Type resourceType)
194206 return resourceAttribute . ResourceName ;
195207
196208 // fallback to dsherized...this should actually check for a custom IResourceNameFormatter
197- return property . Name . Dasherize ( ) ;
209+ return _resourceNameFormatter . FormatResourceName ( resourceType ) ;
198210 }
199211
200212 private ( bool isJsonApiResource , Type idType ) GetIdType ( Type resourceType )
@@ -213,5 +225,12 @@ private void AssertEntityIsNotAlreadyDefined(Type entityType)
213225 if ( _entities . Any ( e => e . EntityType == entityType ) )
214226 throw new InvalidOperationException ( $ "Cannot add entity type { entityType } to context graph, there is already an entity of that type configured.") ;
215227 }
228+
229+ /// <inheritdoc />
230+ public IContextGraphBuilder UseNameFormatter ( IResourceNameFormatter resourceNameFormatter )
231+ {
232+ _resourceNameFormatter = resourceNameFormatter ;
233+ return this ;
234+ }
216235 }
217236}
0 commit comments