Skip to content

Commit 9239d61

Browse files
author
Bart Koelman
committed
Cleanup dead and duplicate code; moved essential registration to top-level namespace
1 parent e82ab39 commit 9239d61

File tree

18 files changed

+108
-203
lines changed

18 files changed

+108
-203
lines changed

src/Examples/GettingStarted/Startup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using Microsoft.AspNetCore.Builder;
22
using Microsoft.Extensions.DependencyInjection;
33
using Microsoft.EntityFrameworkCore;
4-
using JsonApiDotNetCore.Extensions;
4+
using JsonApiDotNetCore;
55

66
namespace GettingStarted
77
{

src/Examples/JsonApiDotNetCoreExample/Startups/Startup.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using Microsoft.EntityFrameworkCore;
77
using JsonApiDotNetCore.Extensions;
88
using System;
9+
using JsonApiDotNetCore;
910
using JsonApiDotNetCore.Configuration;
1011
using JsonApiDotNetCore.Query;
1112
using JsonApiDotNetCoreExample.Services;
@@ -42,7 +43,7 @@ public virtual void ConfigureServices(IServiceCollection services)
4243
.AddJsonApi(ConfigureJsonApiOptions, discovery => discovery.AddCurrentAssembly());
4344

4445
// once all tests have been moved to WebApplicationFactory format we can get rid of this line below
45-
services.AddClientSerialization();
46+
services.AddClientSerialization();
4647
}
4748

4849
protected virtual void ConfigureJsonApiOptions(JsonApiOptions options)

src/Examples/NoEntityFrameworkExample/Startup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using System;
2-
using JsonApiDotNetCore.Extensions;
2+
using JsonApiDotNetCore;
33
using JsonApiDotNetCore.Services;
44
using Microsoft.AspNetCore.Builder;
55
using Microsoft.AspNetCore.Hosting;

src/Examples/ReportsExample/Startup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using JsonApiDotNetCore.Extensions;
1+
using JsonApiDotNetCore;
22
using Microsoft.AspNetCore.Hosting;
33
using Microsoft.Extensions.Configuration;
44
using Microsoft.Extensions.DependencyInjection;

src/JsonApiDotNetCore/Builders/JsonApiApplicationBuilder.cs

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using JsonApiDotNetCore.Configuration;
33
using JsonApiDotNetCore.Data;
4+
using JsonApiDotNetCore.Extensions.EntityFrameworkCore;
45
using JsonApiDotNetCore.Formatters;
56
using JsonApiDotNetCore.Graph;
67
using JsonApiDotNetCore.Internal;
@@ -29,12 +30,12 @@ namespace JsonApiDotNetCore.Builders
2930
/// A utility class that builds a JsonApi application. It registers all required services
3031
/// and allows the user to override parts of the startup configuration.
3132
/// </summary>
32-
public class JsonApiApplicationBuilder
33+
internal sealed class JsonApiApplicationBuilder
3334
{
3435
private readonly JsonApiOptions _options = new JsonApiOptions();
35-
internal IResourceGraphBuilder _resourceGraphBuilder;
36-
internal bool _usesDbContext;
37-
internal readonly IServiceCollection _services;
36+
private IResourceGraphBuilder _resourceGraphBuilder;
37+
private bool _usesDbContext;
38+
private readonly IServiceCollection _services;
3839
private IServiceDiscoveryFacade _serviceDiscoveryFacade;
3940
private readonly IMvcCoreBuilder _mvcBuilder;
4041

@@ -47,7 +48,10 @@ public JsonApiApplicationBuilder(IServiceCollection services, IMvcCoreBuilder mv
4748
/// <summary>
4849
/// Executes the action provided by the user to configure <see cref="JsonApiOptions"/>
4950
/// </summary>
50-
public void ConfigureJsonApiOptions(Action<JsonApiOptions> configureOptions) => configureOptions(_options);
51+
public void ConfigureJsonApiOptions(Action<JsonApiOptions> options)
52+
{
53+
options?.Invoke(_options);
54+
}
5155

5256
/// <summary>
5357
/// Configures built-in .NET Core MVC (things like middleware, routing). Most of this configuration can be adjusted for the developers' need.
@@ -90,16 +94,25 @@ public void ConfigureMvc()
9094
/// </summary>
9195
public void AutoDiscover(Action<IServiceDiscoveryFacade> autoDiscover)
9296
{
93-
autoDiscover(_serviceDiscoveryFacade);
97+
autoDiscover?.Invoke(_serviceDiscoveryFacade);
98+
}
99+
100+
public void ConfigureResourcesFromDbContext<TDbContext>(Action<IResourceGraphBuilder> resources)
101+
where TDbContext : DbContext
102+
{
103+
_resourceGraphBuilder.AddDbContext<TDbContext>();
104+
_usesDbContext = true;
105+
_services.AddScoped<IDbContextResolver, DbContextResolver<TDbContext>>();
106+
107+
ConfigureResources(resources);
94108
}
95109

96110
/// <summary>
97111
/// Executes the action provided by the user to configure the resources using <see cref="IResourceGraphBuilder"/>
98112
/// </summary>
99-
/// <param name="resourceGraphBuilder"></param>
100-
public void ConfigureResources(Action<IResourceGraphBuilder> resourceGraphBuilder)
113+
public void ConfigureResources(Action<IResourceGraphBuilder> resources)
101114
{
102-
resourceGraphBuilder(_resourceGraphBuilder);
115+
resources?.Invoke(_resourceGraphBuilder);
103116
}
104117

105118
/// <summary>

src/JsonApiDotNetCore/Data/DbContextResolver.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
namespace JsonApiDotNetCore.Data
44
{
5-
public sealed class DbContextResolver<TContext> : IDbContextResolver
6-
where TContext : DbContext
5+
public sealed class DbContextResolver<TDbContext> : IDbContextResolver
6+
where TDbContext : DbContext
77
{
8-
private readonly TContext _context;
8+
private readonly TDbContext _context;
99

10-
public DbContextResolver(TContext context)
10+
public DbContextResolver(TDbContext context)
1111
{
1212
_context = context;
1313
}

src/JsonApiDotNetCore/Extensions/IApplicationBuilderExtensions.cs renamed to src/JsonApiDotNetCore/Extensions/ApplicationBuilderExtensions.cs

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,26 @@
66
using Microsoft.Extensions.DependencyInjection;
77
using Microsoft.Extensions.Logging;
88

9-
namespace JsonApiDotNetCore.Extensions
9+
namespace JsonApiDotNetCore
1010
{
11-
// ReSharper disable once InconsistentNaming
12-
public static class IApplicationBuilderExtensions
11+
public static class ApplicationBuilderExtensions
1312
{
1413
/// <summary>
15-
/// Runs several internal JsonApiDotNetCore services to ensure proper configuration and registers required middlewares.
16-
/// The <paramref name="skipRegisterMiddleware"/> can be used to skip any middleware registration, in which case the developer
17-
/// is responsible for registering middleware that are required for JsonApiDotNetCore.
14+
/// Validates the resource graph and optionally registers the JsonApiDotNetCore middleware.
1815
/// </summary>
19-
/// <param name="app"></param>
20-
/// <param name="skipRegisterMiddleware">Indicates if JsonApiDotNetCore should skip middleware registration. This enables a user to take full control of middleware registration.</param>
21-
/// <param name="useAuthentication">Indicates if .NET Core authentication middleware should be registered. Ignored when <paramref name="skipRegisterMiddleware"/> is set to true.</param>
22-
/// <param name="useAuthorization">Indicates if .NET Core authentication middleware should be registered. Ignored when <paramref name="skipRegisterMiddleware"/> is set to true.</param>
16+
/// <remarks>
17+
/// The <paramref name="skipRegisterMiddleware"/> can be used to skip any middleware registration, in which case the developer
18+
/// is responsible for registering required middleware.
19+
/// </remarks>
20+
/// <param name="skipRegisterMiddleware">Indicates to not register any middleware. This enables callers to take full control of middleware registration order.</param>
21+
/// <param name="useAuthentication">Indicates if 'app.UseAuthentication()' should be called. Ignored when <paramref name="skipRegisterMiddleware"/> is set to true.</param>
22+
/// <param name="useAuthorization">Indicates if 'app.UseAuthorization()' should be called. Ignored when <paramref name="skipRegisterMiddleware"/> is set to true.</param>
2323
/// <example>
24-
/// This example illustrate which required middlewares should be registered when using the <paramref name="skipRegisterMiddleware"/> option.
24+
/// The next example illustrates how to manually register middleware.
2525
/// <code>
2626
/// app.UseJsonApi(skipRegisterMiddleware: true);
27-
/// // JADNC requires routing
2827
/// app.UseRouting();
29-
/// // JADNC requires CurrentRequestMiddleware
3028
/// app.UseMiddleware{CurrentRequestMiddleware}();
31-
/// // JANDC requires the endpoint feature enabled as follows
3229
/// app.UseEndpoints(endpoints => endpoints.MapControllers());
3330
/// </code>
3431
/// </example>
@@ -41,7 +38,8 @@ public static void UseJsonApi(this IApplicationBuilder app, bool skipRegisterMid
4138
inverseRelationshipResolver?.Resolve();
4239
}
4340

44-
if (!skipRegisterMiddleware) {
41+
if (!skipRegisterMiddleware)
42+
{
4543
// An endpoint is selected and set on the HttpContext if a match is found
4644
app.UseRouting();
4745

@@ -65,12 +63,12 @@ public static void UseJsonApi(this IApplicationBuilder app, bool skipRegisterMid
6563

6664
private static void LogResourceGraphValidations(IApplicationBuilder app)
6765
{
68-
var logger = app.ApplicationServices.GetService(typeof(ILogger<ResourceGraphBuilder>)) as ILogger;
69-
var resourceGraph = app.ApplicationServices.GetService(typeof(IResourceGraph)) as ResourceGraph;
66+
var logger = (ILogger)app.ApplicationServices.GetService(typeof(ILogger<ResourceGraphBuilder>));
67+
var resourceGraph = (ResourceGraph)app.ApplicationServices.GetService(typeof(IResourceGraph));
7068

7169
if (logger != null)
7270
{
73-
resourceGraph?.ValidationResults.ForEach((v) => logger.Log(v.LogLevel, null, v.Message));
71+
resourceGraph?.ValidationResults.ForEach(v => logger.Log(v.LogLevel, null, v.Message));
7472
}
7573
}
7674
}

src/JsonApiDotNetCore/Extensions/DbContextExtensions.cs

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,30 +11,11 @@ namespace JsonApiDotNetCore.Extensions
1111
{
1212
public static class DbContextExtensions
1313
{
14-
/// <summary>
15-
/// Get the DbSet when the model type is unknown until runtime
16-
/// </summary>
17-
public static IQueryable<object> Set(this DbContext context, Type t)
18-
=> (IQueryable<object>)context
19-
.GetType()
20-
.GetMethod("Set")
21-
.MakeGenericMethod(t) // TODO: will caching help runtime performance?
22-
.Invoke(context, null);
23-
24-
/// <summary>
25-
/// Determines whether or not EF is already tracking an entity of the same Type and Id
26-
/// </summary>
27-
public static bool EntityIsTracked(this DbContext context, IIdentifiable entity)
28-
{
29-
return GetTrackedEntity(context, entity) != null;
30-
}
31-
32-
3314
/// <summary>
3415
/// Determines whether or not EF is already tracking an entity of the same Type and Id
3516
/// and returns that entity.
3617
/// </summary>
37-
public static IIdentifiable GetTrackedEntity(this DbContext context, IIdentifiable entity)
18+
internal static IIdentifiable GetTrackedEntity(this DbContext context, IIdentifiable entity)
3819
{
3920
if (entity == null)
4021
throw new ArgumentNullException(nameof(entity));
@@ -49,7 +30,6 @@ public static IIdentifiable GetTrackedEntity(this DbContext context, IIdentifiab
4930
return (IIdentifiable)trackedEntries?.Entity;
5031
}
5132

52-
5333
/// <summary>
5434
/// Gets the current transaction or creates a new one.
5535
/// If a transaction already exists, commit, rollback and dispose

src/JsonApiDotNetCore/Extensions/IEnumerableExtensions.cs renamed to src/JsonApiDotNetCore/Extensions/EnumerableExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
using System.Collections.Generic;
1+
using System.Collections.Generic;
22
using System.Linq;
33
using JsonApiDotNetCore.Query;
44

55
namespace JsonApiDotNetCore.Extensions
66
{
7-
public static class IEnumerableExtensions
7+
public static class EnumerableExtensions
88
{
99
/// <summary>
1010
/// gets the first element of type <typeparamref name="TImplementedService"/> if it exists and casts the result to that.

src/JsonApiDotNetCore/Extensions/IQueryableExtensions.cs renamed to src/JsonApiDotNetCore/Extensions/QueryableExtensions.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111

1212
namespace JsonApiDotNetCore.Extensions
1313
{
14-
// ReSharper disable once InconsistentNaming
15-
public static class IQueryableExtensions
14+
public static class QueryableExtensions
1615
{
1716
private static MethodInfo _containsMethod;
1817
private static MethodInfo ContainsMethod

0 commit comments

Comments
 (0)