Skip to content

Commit 354a1be

Browse files
committed
chore: fix NoEntityFrameworkExample project
1 parent 6a186a5 commit 354a1be

File tree

24 files changed

+93
-80
lines changed

24 files changed

+93
-80
lines changed

src/Examples/GettingStarted/Controllers/ArticlesController.cs

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using GettingStarted.Models;
22
using JsonApiDotNetCore.Configuration;
33
using JsonApiDotNetCore.Controllers;
4-
using JsonApiDotNetCore.Internal.Contracts;
54
using JsonApiDotNetCore.Services;
65

76
namespace GettingStarted

src/Examples/GettingStarted/Controllers/PeopleController.cs

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using GettingStarted.Models;
22
using JsonApiDotNetCore.Configuration;
33
using JsonApiDotNetCore.Controllers;
4-
using JsonApiDotNetCore.Internal.Contracts;
54
using JsonApiDotNetCore.Services;
65

76
namespace GettingStarted

src/Examples/GettingStarted/Data/SampleDbContext.cs

+1-4
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@ namespace GettingStarted
66
{
77
public class SampleDbContext : DbContext
88
{
9-
public SampleDbContext(DbContextOptions<SampleDbContext> options)
10-
: base(options)
11-
{ }
12-
9+
public SampleDbContext(DbContextOptions<SampleDbContext> options) : base(options) { }
1310
public DbSet<Article> Articles { get; set; }
1411
public DbSet<Person> People { get; set; }
1512
public DbSet<Model> Models { get; set; }

src/Examples/GettingStarted/Models/Person.cs

-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ public class Person : Identifiable
77
{
88
[Attr]
99
public string Name { get; set; }
10-
1110
[HasMany]
1211
public List<Article> Articles { get; set; }
1312
}

src/Examples/GettingStarted/ResourceDefinitionExample/ModelDefinition.cs

+7-9
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,14 @@ namespace GettingStarted.ResourceDefinitionExample
66
{
77
public class ModelDefinition : ResourceDefinition<Model>
88
{
9-
public ModelDefinition(IResourceContextProvider provider) : base(provider)
9+
public ModelDefinition(IResourceGraph resourceGraph) : base(resourceGraph)
1010
{
11+
// this allows POST / PATCH requests to set the value of a
12+
// property, but we don't include this value in the response
13+
// this might be used if the incoming value gets hashed or
14+
// encrypted prior to being persisted and this value should
15+
// never be sent back to the client
16+
HideFields(model => model.DontExpose);
1117
}
12-
13-
// this allows POST / PATCH requests to set the value of a
14-
// property, but we don't include this value in the response
15-
// this might be used if the incoming value gets hashed or
16-
// encrypted prior to being persisted and this value should
17-
// never be sent back to the client
18-
protected override List<AttrAttribute> OutputAttrs()
19-
=> Remove(model => model.DontExpose);
2018
}
2119
}

src/Examples/GettingStarted/ResourceDefinitionExample/ModelsController.cs

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using JsonApiDotNetCore.Configuration;
22
using JsonApiDotNetCore.Controllers;
3-
using JsonApiDotNetCore.Internal.Contracts;
43
using JsonApiDotNetCore.Services;
54

65
namespace GettingStarted.ResourceDefinitionExample

src/Examples/GettingStarted/Startup.cs

+2-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Threading.Tasks;
5-
using Microsoft.AspNetCore.Builder;
1+
using Microsoft.AspNetCore.Builder;
62
using Microsoft.AspNetCore.Hosting;
7-
using Microsoft.AspNetCore.Http;
83
using Microsoft.Extensions.DependencyInjection;
94
using Microsoft.EntityFrameworkCore;
105
using JsonApiDotNetCore.Extensions;
@@ -23,7 +18,7 @@ public void ConfigureServices(IServiceCollection services)
2318
var mvcBuilder = services.AddMvcCore();
2419
services.AddJsonApi(
2520
options => options.Namespace = "api",
26-
discover => discover.AddCurrentAssembly(), mvcBuilder);
21+
discover => discover.AddCurrentAssembly(), mvcBuilder: mvcBuilder);
2722
}
2823

2924
public void Configure(IApplicationBuilder app, IHostingEnvironment env, SampleDbContext context)

src/JsonApiDotNetCore/Builders/JsonApiApplicationBuilder.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ public void ConfigureServices()
154154
_services.AddScoped<IScopedServiceProvider, RequestScopedServiceProvider>();
155155
_services.AddScoped<IJsonApiWriter, JsonApiWriter>();
156156
_services.AddScoped<IJsonApiReader, JsonApiReader>();
157-
_services.AddScoped<IGenericServiceFactory, genericServiceFactory>();
157+
_services.AddScoped<IGenericServiceFactory, GenericServiceFactory>();
158158
_services.AddScoped(typeof(HasManyThroughUpdateHelper<>));
159159
_services.AddScoped<IQueryParameterDiscovery, QueryParameterDiscovery>();
160160
_services.AddScoped<ITargetedFields, TargetedFields>();

src/JsonApiDotNetCore/Data/DefaultResourceRepository.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -423,18 +423,18 @@ public class DefaultResourceRepository<TResource> : DefaultResourceRepository<TR
423423
{
424424
public DefaultResourceRepository(ITargetedFields targetedFields,
425425
IDbContextResolver contextResolver,
426-
IResourceGraph contextEntityProvider,
426+
IResourceGraph resourceContextProvider,
427427
IGenericServiceFactory genericServiceFactory)
428-
: base(targetedFields, contextResolver, contextEntityProvider, genericServiceFactory)
428+
: base(targetedFields, contextResolver, resourceContextProvider, genericServiceFactory)
429429
{
430430
}
431431

432432
public DefaultResourceRepository(ITargetedFields targetedFields,
433433
IDbContextResolver contextResolver,
434-
IResourceGraph contextEntityProvider,
434+
IResourceGraph resourceContextProvider,
435435
IGenericServiceFactory genericServiceFactory,
436436
ILoggerFactory loggerFactory = null)
437-
: base(targetedFields, contextResolver, contextEntityProvider, genericServiceFactory, loggerFactory)
437+
: base(targetedFields, contextResolver, resourceContextProvider, genericServiceFactory, loggerFactory)
438438
{
439439
}
440440
}

src/JsonApiDotNetCore/Hooks/Execution/HookExecutorHelper.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ internal class HookExecutorHelper : IHookExecutorHelper
1919
{
2020
private readonly IdentifiableComparer _comparer = new IdentifiableComparer();
2121
private readonly IJsonApiOptions _options;
22-
protected readonly IGenericProcessorFactory _genericProcessorFactory;
22+
protected readonly IGenericServiceFactory _genericProcessorFactory;
2323
protected readonly Dictionary<RightType, IResourceHookContainer> _hookContainers;
2424
protected readonly Dictionary<RightType, IHooksDiscovery> _hookDiscoveries;
2525
protected readonly List<ResourceHook> _targetedHooksForRelatedEntities;
2626

27-
public HookExecutorHelper(IGenericProcessorFactory genericProcessorFactory,
27+
public HookExecutorHelper(IGenericServiceFactory genericProcessorFactory,
2828
IJsonApiOptions options)
2929
{
3030
_options = options;
@@ -43,7 +43,7 @@ public IResourceHookContainer GetResourceHookContainer(RightType rightType, Reso
4343
/// so we need not even bother.
4444
if (!_hookContainers.TryGetValue(rightType, out IResourceHookContainer container))
4545
{
46-
container = (_genericProcessorFactory.GetProcessor<IResourceHookContainer>(typeof(ResourceDefinition<>), rightType));
46+
container = (_genericProcessorFactory.Get<IResourceHookContainer>(typeof(ResourceDefinition<>), rightType));
4747
_hookContainers[rightType] = container;
4848
}
4949
if (container == null) return container;
@@ -123,7 +123,7 @@ IHooksDiscovery GetHookDiscovery(Type entityType)
123123
{
124124
if (!_hookDiscoveries.TryGetValue(entityType, out IHooksDiscovery discovery))
125125
{
126-
discovery = _genericProcessorFactory.GetProcessor<IHooksDiscovery>(typeof(IHooksDiscovery<>), entityType);
126+
discovery = _genericProcessorFactory.Get<IHooksDiscovery>(typeof(IHooksDiscovery<>), entityType);
127127
_hookDiscoveries[entityType] = discovery;
128128
}
129129
return discovery;
@@ -138,7 +138,7 @@ IEnumerable<TResource> GetWhereAndInclude<TResource, TId>(IEnumerable<TId> ids,
138138

139139
IResourceReadRepository<TResource, TId> GetRepository<TResource, TId>() where TResource : class, IIdentifiable<TId>
140140
{
141-
return _genericProcessorFactory.GetProcessor<IResourceReadRepository<TResource, TId>>(typeof(IResourceReadRepository<,>), typeof(TResource), typeof(TId));
141+
return _genericProcessorFactory.Get<IResourceReadRepository<TResource, TId>>(typeof(IResourceReadRepository<,>), typeof(TResource), typeof(TId));
142142
}
143143

144144

src/JsonApiDotNetCore/Internal/Generics/GenericServiceFactory.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public interface IGenericServiceFactory
1515
/// </summary>
1616
/// <example>
1717
/// <code>
18-
/// GetProcessor&lt;IGenericProcessor&gt;(typeof(GenericProcessor&lt;&gt;), typeof(TResource));
18+
/// Get&lt;IGenericProcessor&gt;(typeof(GenericProcessor&lt;&gt;), typeof(TResource));
1919
/// </code>
2020
/// </example>
2121
TInterface Get<TInterface>(Type openGenericType, Type resourceType);
@@ -25,7 +25,7 @@ public interface IGenericServiceFactory
2525
/// </summary>
2626
/// <example>
2727
/// <code>
28-
/// GetProcessor&lt;IGenericProcessor&gt;(typeof(GenericProcessor&lt;,&gt;), typeof(TResource), typeof(TId));
28+
/// Get&lt;IGenericProcessor&gt;(typeof(GenericProcessor&lt;,&gt;), typeof(TResource), typeof(TId));
2929
/// </code>
3030
/// </example>
3131
TInterface Get<TInterface>(Type openGenericType, Type resourceType, Type keyType);

src/JsonApiDotNetCore/Middleware/RequestMiddleware.cs renamed to src/JsonApiDotNetCore/Middleware/CurrentRequestMiddleware.cs

-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
namespace JsonApiDotNetCore.Middleware
1313
{
1414
/// <summary>
15-
/// Can be overwritten to help you out during testing
16-
///
1715
/// This sets all necessary parameters relating to the HttpContext for JADNC
1816
/// </summary>
1917
public class CurrentRequestMiddleware

src/JsonApiDotNetCore/Models/ResourceDefinition.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,15 @@ public interface IResourceDefinition
2626
/// <typeparam name="TResource">The resource type</typeparam>
2727
public class ResourceDefinition<TResource> : IResourceDefinition, IResourceHookContainer<TResource> where TResource : class, IIdentifiable
2828
{
29-
private readonly ResourceContext _contextEntity;
29+
private readonly ResourceContext _resourceContext;
3030
private readonly IResourceGraph _resourceGraph;
3131
private List<AttrAttribute> _allowedAttributes;
3232
private List<RelationshipAttribute> _allowedRelationships;
3333
public ResourceDefinition(IResourceGraph resourceGraph)
3434
{
35-
_contextEntity = resourceGraph.GetResourceContext(typeof(TResource));
36-
_allowedAttributes = _contextEntity.Attributes;
37-
_allowedRelationships = _contextEntity.Relationships;
35+
_resourceContext = resourceGraph.GetResourceContext(typeof(TResource));
36+
_allowedAttributes = _resourceContext.Attributes;
37+
_allowedRelationships = _resourceContext.Relationships;
3838
_resourceGraph = resourceGraph;
3939
}
4040

src/JsonApiDotNetCore/RequestServices/Contracts/ICurrentRequest.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ public interface ICurrentRequest
3333
/// <summary>
3434
/// Sets the current context entity for this entire request
3535
/// </summary>
36-
/// <param name="contextEntityCurrent"></param>
37-
void SetRequestResource(ResourceContext contextEntityCurrent);
36+
/// <param name="currentResourceContext"></param>
37+
void SetRequestResource(ResourceContext currentResourceContext);
3838

3939
ResourceContext GetRequestResource();
4040
}

src/JsonApiDotNetCore/RequestServices/CurrentRequest.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace JsonApiDotNetCore.Managers
66
{
77
class CurrentRequest : ICurrentRequest
88
{
9-
private ResourceContext _contextEntity;
9+
private ResourceContext _resourceContext;
1010
public string BasePath { get; set; }
1111
public bool IsRelationshipPath { get; set; }
1212
public RelationshipAttribute RequestRelationship { get; set; }
@@ -17,12 +17,12 @@ class CurrentRequest : ICurrentRequest
1717
/// <returns></returns>
1818
public ResourceContext GetRequestResource()
1919
{
20-
return _contextEntity;
20+
return _resourceContext;
2121
}
2222

2323
public void SetRequestResource(ResourceContext primaryResource)
2424
{
25-
_contextEntity = primaryResource;
25+
_resourceContext = primaryResource;
2626
}
2727
}
2828
}

src/JsonApiDotNetCore/Serialization/Client/ResponseDeserializer.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,12 @@ private IIdentifiable ParseIncludedRelationship(RelationshipAttribute relationsh
9191
if (includedResource == null)
9292
return relatedInstance;
9393

94-
var contextEntity = _provider.GetResourceContext(relatedResourceIdentifier.Type);
95-
if (contextEntity == null)
94+
var resourceContext = _provider.GetResourceContext(relatedResourceIdentifier.Type);
95+
if (resourceContext == null)
9696
throw new InvalidOperationException($"Included type '{relationshipAttr.RightType}' is not a registered json:api resource.");
9797

98-
SetAttributes(relatedInstance, includedResource.Attributes, contextEntity.Attributes);
99-
SetRelationships(relatedInstance, includedResource.Relationships, contextEntity.Relationships);
98+
SetAttributes(relatedInstance, includedResource.Attributes, resourceContext.Attributes);
99+
SetRelationships(relatedInstance, includedResource.Relationships, resourceContext.Relationships);
100100
return relatedInstance;
101101
}
102102

src/JsonApiDotNetCore/Serialization/Common/BaseDocumentParser.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -128,8 +128,8 @@ private JToken LoadJToken(string body)
128128
/// <returns>The parsed entity</returns>
129129
private IIdentifiable ParseResourceObject(ResourceObject data)
130130
{
131-
var contextEntity = _provider.GetResourceContext(data.Type);
132-
if (contextEntity == null)
131+
var resourceContext = _provider.GetResourceContext(data.Type);
132+
if (resourceContext == null)
133133
{
134134
throw new JsonApiException(400,
135135
message: $"This API does not contain a json:api resource named '{data.Type}'.",
@@ -138,10 +138,10 @@ private IIdentifiable ParseResourceObject(ResourceObject data)
138138
+ "If you have manually registered the resource, check that the call to AddResource correctly sets the public name.");
139139
}
140140

141-
var entity = (IIdentifiable)Activator.CreateInstance(contextEntity.ResourceType);
141+
var entity = (IIdentifiable)Activator.CreateInstance(resourceContext.ResourceType);
142142

143-
entity = SetAttributes(entity, data.Attributes, contextEntity.Attributes);
144-
entity = SetRelationships(entity, data.Relationships, contextEntity.Relationships);
143+
entity = SetAttributes(entity, data.Attributes, resourceContext.Attributes);
144+
entity = SetRelationships(entity, data.Relationships, resourceContext.Relationships);
145145

146146
if (data.Id != null)
147147
entity.StringId = data.Id?.ToString();

test/DiscoveryTests/ServiceDiscoveryFacadeTests.cs

-2
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
using JsonApiDotNetCore.Data;
66
using JsonApiDotNetCore.Graph;
77
using JsonApiDotNetCore.Hooks;
8-
using JsonApiDotNetCore.Internal;
98
using JsonApiDotNetCore.Internal.Contracts;
10-
using JsonApiDotNetCore.Managers.Contracts;
119
using JsonApiDotNetCore.Models;
1210
using JsonApiDotNetCore.Services;
1311
using Microsoft.EntityFrameworkCore;

test/JsonApiDotNetCoreExampleTests/Acceptance/TestFixture.cs

-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ public class TestFixture<TStartup> : IDisposable where TStartup : class
1818
{
1919
private readonly TestServer _server;
2020
private IServiceProvider _services;
21-
2221
public TestFixture()
2322
{
2423
var builder = new WebHostBuilder()

test/NoEntityFrameworkTests/Acceptance/Extensibility/NoEntityFrameworkTests.cs

+3-6
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,7 @@ public async Task Can_Get_TodoItems()
3535
// act
3636
var response = await client.SendAsync(request);
3737
var responseBody = await response.Content.ReadAsStringAsync();
38-
var deserializedBody = _fixture.Server.GetDeserializer()
39-
.DeserializeList<TodoItem>(responseBody);
38+
var deserializedBody = _fixture.GetDeserializer().DeserializeList<TodoItem>(responseBody).Data;
4039

4140
// assert
4241
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
@@ -62,8 +61,7 @@ public async Task Can_Get_TodoItems_By_Id()
6261
// act
6362
var response = await client.SendAsync(request);
6463
var responseBody = await response.Content.ReadAsStringAsync();
65-
var deserializedBody = (TodoItem)_fixture.Server.GetDeserializer()
66-
.Deserialize(responseBody);
64+
var deserializedBody = _fixture.GetDeserializer().DeserializeSingle<TodoItem>(responseBody).Data;
6765

6866
// assert
6967
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
@@ -99,8 +97,7 @@ public async Task Can_Create_TodoItems()
9997
// act
10098
var response = await client.SendAsync(request);
10199
var responseBody = await response.Content.ReadAsStringAsync();
102-
var deserializedBody = (TodoItem)_fixture.Server.GetDeserializer()
103-
.Deserialize(responseBody);
100+
var deserializedBody = _fixture.GetDeserializer().DeserializeSingle<TodoItem>(responseBody).Data;
104101

105102
// assert
106103
Assert.Equal(HttpStatusCode.Created, response.StatusCode);

test/NoEntityFrameworkTests/TestFixture.cs

+36-1
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,58 @@
1+
using JsonApiDotNetCore.Builders;
2+
using JsonApiDotNetCore.Models;
3+
using JsonApiDotNetCore.Serialization.Client;
14
using JsonApiDotNetCoreExample.Data;
5+
using JsonApiDotNetCoreExample.Models;
26
using JsonApiDotNetCoreExampleTests.Helpers.Extensions;
7+
using JsonApiDotNetCoreExampleTests.Helpers.Models;
38
using Microsoft.AspNetCore.Hosting;
49
using Microsoft.AspNetCore.TestHost;
510
using System;
11+
using System.Linq.Expressions;
612

713
namespace NoEntityFrameworkTests
814
{
915
public class TestFixture : IDisposable
1016
{
1117
public AppDbContext Context { get; private set; }
1218
public TestServer Server { get; private set; }
13-
19+
private IServiceProvider _services;
1420
public TestFixture()
1521
{
1622
var builder = new WebHostBuilder().UseStartup<TestStartup>();
1723
Server = new TestServer(builder);
1824
Context = Server.GetService<AppDbContext>();
1925
Context.Database.EnsureCreated();
26+
_services = Server.Host.Services;
27+
}
28+
29+
public IRequestSerializer GetSerializer<TResource>(Expression<Func<TResource, dynamic>> attributes = null, Expression<Func<TResource, dynamic>> relationships = null) where TResource : class, IIdentifiable
30+
{
31+
var serializer = GetService<IRequestSerializer>();
32+
if (attributes != null)
33+
serializer.SetAttributesToSerialize(attributes);
34+
if (relationships != null)
35+
serializer.SetRelationshipsToSerialize(relationships);
36+
return serializer;
2037
}
38+
public IResponseDeserializer GetDeserializer()
39+
{
40+
var resourceGraph = new ResourceGraphBuilder()
41+
.AddResource<PersonRole>()
42+
.AddResource<Article>()
43+
.AddResource<Tag>()
44+
.AddResource<CamelCasedModel>()
45+
.AddResource<User>()
46+
.AddResource<Person>()
47+
.AddResource<Author>()
48+
.AddResource<Passport>()
49+
.AddResource<TodoItemClient>("todo-items")
50+
.AddResource<TodoItemCollectionClient, Guid>().Build();
51+
return new ResponseDeserializer(resourceGraph);
52+
}
53+
54+
public T GetService<T>() => (T)_services.GetService(typeof(T));
55+
2156

2257
public void Dispose()
2358
{

0 commit comments

Comments
 (0)