6
6
using JsonApiDotNetCore . Internal ;
7
7
using JsonApiDotNetCore . Models ;
8
8
using Microsoft . EntityFrameworkCore ;
9
+ using Microsoft . Extensions . Logging ;
9
10
10
11
namespace JsonApiDotNetCore . Builders
11
12
{
13
+ public interface IContextGraphBuilder
14
+ {
15
+ /// <summary>
16
+ /// Construct the <see cref="ContextGraph"/>
17
+ /// </summary>
18
+ IContextGraph Build ( ) ;
19
+
20
+ /// <summary>
21
+ /// Add a json:api resource
22
+ /// </summary>
23
+ /// <typeparam name="TResource">The resource model type</typeparam>
24
+ /// <param name="pluralizedTypeName">The pluralized name that should be exposed by the API</param>
25
+ IContextGraphBuilder AddResource < TResource > ( string pluralizedTypeName ) where TResource : class , IIdentifiable < int > ;
26
+
27
+ /// <summary>
28
+ /// Add a json:api resource
29
+ /// </summary>
30
+ /// <typeparam name="TResource">The resource model type</typeparam>
31
+ /// <typeparam name="TId">The resource model identifier type</typeparam>
32
+ /// <param name="pluralizedTypeName">The pluralized name that should be exposed by the API</param>
33
+ IContextGraphBuilder AddResource < TResource , TId > ( string pluralizedTypeName ) where TResource : class , IIdentifiable < TId > ;
34
+
35
+ /// <summary>
36
+ /// Add all the models that are part of the provided <see cref="DbContext" />
37
+ /// that also implement <see cref="IIdentifiable"/>
38
+ /// </summary>
39
+ /// <typeparam name="T">The <see cref="DbContext"/> implementation type.</typeparam>
40
+ IContextGraphBuilder AddDbContext < T > ( ) where T : DbContext ;
41
+
42
+ /// <summary>
43
+ /// Which links to include. Defaults to <see cref="Link.All"/>.
44
+ /// </summary>
45
+ Link DocumentLinks { get ; set ; }
46
+ }
47
+
12
48
public class ContextGraphBuilder : IContextGraphBuilder
13
49
{
14
50
private List < ContextEntity > _entities = new List < ContextEntity > ( ) ;
51
+ private List < ValidationResult > _validationResults = new List < ValidationResult > ( ) ;
52
+
15
53
private bool _usesDbContext ;
16
54
public Link DocumentLinks { get ; set ; } = Link . All ;
17
55
@@ -20,7 +58,7 @@ public IContextGraph Build()
20
58
// this must be done at build so that call order doesn't matter
21
59
_entities . ForEach ( e => e . Links = GetLinkFlags ( e . EntityType ) ) ;
22
60
23
- var graph = new ContextGraph ( _entities , _usesDbContext ) ;
61
+ var graph = new ContextGraph ( _entities , _usesDbContext , _validationResults ) ;
24
62
25
63
return graph ;
26
64
}
@@ -117,7 +155,10 @@ public IContextGraphBuilder AddDbContext<T>() where T : DbContext
117
155
118
156
AssertEntityIsNotAlreadyDefined ( entityType ) ;
119
157
120
- _entities . Add ( GetEntity ( GetResourceName ( property ) , entityType , GetIdType ( entityType ) ) ) ;
158
+ var ( isJsonApiResource , idType ) = GetIdType ( entityType ) ;
159
+
160
+ if ( isJsonApiResource )
161
+ _entities . Add ( GetEntity ( GetResourceName ( property ) , entityType , idType ) ) ;
121
162
}
122
163
}
123
164
@@ -133,16 +174,18 @@ private string GetResourceName(PropertyInfo property)
133
174
return ( ( ResourceAttribute ) resourceAttribute ) . ResourceName ;
134
175
}
135
176
136
- private Type GetIdType ( Type resourceType )
177
+ private ( bool isJsonApiResource , Type idType ) GetIdType ( Type resourceType )
137
178
{
138
179
var interfaces = resourceType . GetInterfaces ( ) ;
139
180
foreach ( var type in interfaces )
140
181
{
141
182
if ( type . GetTypeInfo ( ) . IsGenericType && type . GetGenericTypeDefinition ( ) == typeof ( IIdentifiable < > ) )
142
- return type . GetGenericArguments ( ) [ 0 ] ;
183
+ return ( true , type . GetGenericArguments ( ) [ 0 ] ) ;
143
184
}
144
185
145
- throw new ArgumentException ( "Type does not implement 'IIdentifiable<TId>'" , nameof ( resourceType ) ) ;
186
+ _validationResults . Add ( new ValidationResult ( LogLevel . Warning , $ "{ resourceType } does not implement 'IIdentifiable<>'. ") ) ;
187
+
188
+ return ( false , null ) ;
146
189
}
147
190
148
191
private void AssertEntityIsNotAlreadyDefined ( Type entityType )
0 commit comments