-
-
Notifications
You must be signed in to change notification settings - Fork 158
Move EF core related building to extension methods #616
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
1f8fb38
feat: remove inability to use request serializer with unknown type at…
maurei 3047584
fix: typo in test setup
maurei 5b4eee6
chore: spacing
maurei 8674f52
fix: add IResourceQueryService and IResourceCmdService to DI containe…
maurei 1b7f9b2
chore: update comments IRequestSerializer
maurei 3f46fa0
chore: moved ef core related building to extension methods
maurei 93e1cf6
Merge branch 'develop' into feat/efcore-extension-methods
maurei 6156166
chore: spacing, rename file
maurei 7264350
refactor: remove need for internal modifier
maurei 2515c1e
refactor: minor refactor id type retrieval
maurei File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
src/JsonApiDotNetCore/Extensions/EntityFrameworkCoreExtension.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
using System; | ||
using System.Reflection; | ||
using JsonApiDotNetCore.Configuration; | ||
using JsonApiDotNetCore.Graph; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using JsonApiDotNetCore.Builders; | ||
using JsonApiDotNetCore.Models; | ||
using JsonApiDotNetCore.Data; | ||
|
||
namespace JsonApiDotNetCore.Extensions.EntityFrameworkCore | ||
{ | ||
|
||
/// <summary> | ||
/// Extensions for configuring JsonApiDotNetCore with EF Core | ||
/// </summary> | ||
public static class IResourceGraphBuilderExtensions | ||
{ | ||
/// <summary> | ||
/// Add all the models that are part of the provided <see cref="DbContext" /> | ||
/// that also implement <see cref="IIdentifiable"/> | ||
/// </summary> | ||
/// <typeparam name="TDbContext">The <see cref="DbContext"/> implementation type.</typeparam> | ||
public static IResourceGraphBuilder AddDbContext<TDbContext>(this IResourceGraphBuilder resourceGraphBuilder) where TDbContext : DbContext | ||
{ | ||
var builder = (ResourceGraphBuilder)resourceGraphBuilder; | ||
var contextType = typeof(TDbContext); | ||
var contextProperties = contextType.GetProperties(); | ||
foreach (var property in contextProperties) | ||
{ | ||
var dbSetType = property.PropertyType; | ||
if (dbSetType.GetTypeInfo().IsGenericType | ||
&& dbSetType.GetGenericTypeDefinition() == typeof(DbSet<>)) | ||
{ | ||
var resourceType = dbSetType.GetGenericArguments()[0]; | ||
builder.AddResource(resourceType, pluralizedTypeName: GetResourceNameFromDbSetProperty(property, resourceType)); | ||
} | ||
} | ||
return resourceGraphBuilder; | ||
} | ||
|
||
private static string GetResourceNameFromDbSetProperty(PropertyInfo property, Type resourceType) | ||
{ | ||
// this check is actually duplicated in the DefaultResourceNameFormatter | ||
// however, we perform it here so that we allow class attributes to be prioritized over | ||
// the DbSet attribute. Eventually, the DbSet attribute should be deprecated. | ||
// | ||
// check the class definition first | ||
// [Resource("models"] public class Model : Identifiable { /* ... */ } | ||
if (resourceType.GetCustomAttribute(typeof(ResourceAttribute)) is ResourceAttribute classResourceAttribute) | ||
return classResourceAttribute.ResourceName; | ||
|
||
// check the DbContext member next | ||
// [Resource("models")] public DbSet<Model> Models { get; set; } | ||
if (property.GetCustomAttribute(typeof(ResourceAttribute)) is ResourceAttribute resourceAttribute) | ||
return resourceAttribute.ResourceName; | ||
|
||
return null; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Extensions for configuring JsonApiDotNetCore with EF Core | ||
/// </summary> | ||
public static class IServiceCollectionExtensions | ||
{ | ||
/// <summary> | ||
/// Enabling JsonApiDotNetCore using the EF Core DbContext to build the ResourceGraph. | ||
/// </summary> | ||
/// <typeparam name="TContext"></typeparam> | ||
/// <param name="services"></param> | ||
/// <param name="options"></param> | ||
/// <param name="resources"></param> | ||
/// <returns></returns> | ||
public static IServiceCollection AddJsonApi<TDbContext>(this IServiceCollection services, | ||
Action<JsonApiOptions> options = null, | ||
Action<IServiceDiscoveryFacade> discovery = null, | ||
Action<IResourceGraphBuilder> resources = null, | ||
IMvcCoreBuilder mvcBuilder = null) | ||
where TDbContext : DbContext | ||
{ | ||
var application = new JsonApiApplicationBuilder(services, mvcBuilder ?? services.AddMvcCore()); | ||
if (options != null) | ||
application.ConfigureJsonApiOptions(options); | ||
application.ConfigureMvc(); | ||
if (discovery != null) | ||
application.AutoDiscover(discovery); | ||
application.ConfigureResources<TDbContext>(resources); | ||
application.ConfigureServices(); | ||
return services; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Extensions for configuring JsonApiDotNetCore with EF Core | ||
/// </summary> | ||
public static class JsonApiApplicationBuildExtensions | ||
{ | ||
/// <summary> | ||
/// Executes the action provided by the user to configure the resources using <see cref="IResourceGraphBuilder"/>. | ||
/// Additionally, inspects the EF core database context for models that implement IIdentifiable. | ||
/// </summary> | ||
public static void ConfigureResources<TContext>(this JsonApiApplicationBuilder builder, Action<IResourceGraphBuilder> resourceGraphBuilder) where TContext : DbContext | ||
{ | ||
builder._resourceGraphBuilder.AddDbContext<TContext>(); | ||
builder._usesDbContext = true; | ||
builder._services.AddScoped<IDbContextResolver, DbContextResolver<TContext>>(); | ||
resourceGraphBuilder?.Invoke(builder._resourceGraphBuilder); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.