-
-
Notifications
You must be signed in to change notification settings - Fork 158
De-Couple DAL from Entity Framework #89
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
36 commits
Select commit
Hold shift + click to select a range
4ed46c8
feat(services): define IResourceService
jaredcnance fa9364f
refactor(controller): move bulk of logic into the resource service
jaredcnance b62227d
feat(service-collection-ext): add new service
jaredcnance 6d6c3e4
chore(example-controllers): update for new JsonApiController ctor
jaredcnance e17cad2
docs(readme): stub out documentation updates
jaredcnance 550e00a
feat(context-graph): first pass at refactoring the graph API
jaredcnance ba4719d
fix(IContextGraphBuilder): expose the AddDbContext method
jaredcnance b89b18f
refactor(service-collection-ext): prevent breaking changes
jaredcnance 4b859a3
hack(service-collection-ext): inject bogus dbContext types for DI
jaredcnance 8457b22
example: add "no entity framework" example
jaredcnance 99e8b4d
test(extensibility): add initial test around fetching non-ef data
jaredcnance 194d31d
Merge remote-tracking branch 'origin/develop' into feat/decouple-dal
jaredcnance 09cc32a
example(no-ef): rename models, controllers seem to cause conflict
jaredcnance 7c20d77
refactor(context-graph): the stored entity name should be dasherized …
jaredcnance 2fd9eb3
test(no-ef): validate the response body
jaredcnance 94e5dee
docs(readme): document custom data implementation
jaredcnance e6ba54f
docs(readme): document custom context mapping
jaredcnance 0264ea8
docs(readme): document new controller constructor
jaredcnance 84cf741
feat(example): use Dapper and Npgsql in NoEF example
jaredcnance 93edba5
test(acceptance/no-ef): ensure the database is created
jaredcnance 2c164fa
test(acceptance/no-ef): ensure database is migrated
jaredcnance edc4626
fix(example/startups): try using EnsureCreated to fix travis-ci build
jaredcnance 7c14d49
refactor(tests): split test projects
jaredcnance 36b55d8
fix(ci): restore new test project
jaredcnance 62183f3
chore(example): remove build files from source control
jaredcnance 629169f
chore(csproj): bump package major version
jaredcnance fd5e04a
feat(context-graph-builder): intro resource attr
jaredcnance b0146c4
refactor(*): further reduction of Dasherize usage
jaredcnance 75ae1b5
docs(readme): show ResourceAttribute usage
jaredcnance 3d95c01
fix(de-serializer): do not dasherize a public name
jaredcnance 7cff6d0
test(no-ef): additional tests / examples
jaredcnance 517367b
fix(example): missing attribute
jaredcnance aec7e1c
test(extensibility): add tests for dasherized routes for inherited an…
JanMattner 4480e6b
fix(link-builder): do not dasherize routes by default
jaredcnance 1de23d8
test(extensibility): fix/remove failing tests
jaredcnance 8d134f3
chore(csproj): add package information
jaredcnance 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
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,123 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Reflection; | ||
using Microsoft.EntityFrameworkCore; | ||
using JsonApiDotNetCore.Internal; | ||
using JsonApiDotNetCore.Models; | ||
using JsonApiDotNetCore.Extensions; | ||
|
||
namespace JsonApiDotNetCore.Builders | ||
{ | ||
public class ContextGraphBuilder : IContextGraphBuilder | ||
{ | ||
private List<ContextEntity> Entities; | ||
private bool _usesDbContext; | ||
public ContextGraphBuilder() | ||
{ | ||
Entities = new List<ContextEntity>(); | ||
} | ||
|
||
public IContextGraph Build() | ||
{ | ||
var graph = new ContextGraph() | ||
{ | ||
Entities = Entities, | ||
UsesDbContext = _usesDbContext | ||
}; | ||
return graph; | ||
} | ||
|
||
public void AddResource<TResource>(string pluralizedTypeName) where TResource : class | ||
{ | ||
var entityType = typeof(TResource); | ||
Entities.Add(new ContextEntity | ||
{ | ||
EntityName = pluralizedTypeName, | ||
EntityType = entityType, | ||
Attributes = GetAttributes(entityType), | ||
Relationships = GetRelationships(entityType) | ||
}); | ||
} | ||
|
||
protected virtual List<AttrAttribute> GetAttributes(Type entityType) | ||
{ | ||
var attributes = new List<AttrAttribute>(); | ||
|
||
var properties = entityType.GetProperties(); | ||
|
||
foreach (var prop in properties) | ||
{ | ||
var attribute = (AttrAttribute)prop.GetCustomAttribute(typeof(AttrAttribute)); | ||
if (attribute == null) continue; | ||
attribute.InternalAttributeName = prop.Name; | ||
attributes.Add(attribute); | ||
} | ||
return attributes; | ||
} | ||
|
||
protected virtual List<RelationshipAttribute> GetRelationships(Type entityType) | ||
{ | ||
var attributes = new List<RelationshipAttribute>(); | ||
|
||
var properties = entityType.GetProperties(); | ||
|
||
foreach (var prop in properties) | ||
{ | ||
var attribute = (RelationshipAttribute)prop.GetCustomAttribute(typeof(RelationshipAttribute)); | ||
if (attribute == null) continue; | ||
attribute.InternalRelationshipName = prop.Name; | ||
attribute.Type = GetRelationshipType(attribute, prop); | ||
attributes.Add(attribute); | ||
} | ||
return attributes; | ||
} | ||
|
||
protected virtual Type GetRelationshipType(RelationshipAttribute relation, PropertyInfo prop) | ||
{ | ||
if (relation.IsHasMany) | ||
return prop.PropertyType.GetGenericArguments()[0]; | ||
else | ||
return prop.PropertyType; | ||
} | ||
|
||
public void AddDbContext<T>() where T : DbContext | ||
{ | ||
_usesDbContext = true; | ||
|
||
var contextType = typeof(T); | ||
|
||
var entities = new List<ContextEntity>(); | ||
|
||
var contextProperties = contextType.GetProperties(); | ||
|
||
foreach (var property in contextProperties) | ||
{ | ||
var dbSetType = property.PropertyType; | ||
|
||
if (dbSetType.GetTypeInfo().IsGenericType | ||
&& dbSetType.GetGenericTypeDefinition() == typeof(DbSet<>)) | ||
{ | ||
var entityType = dbSetType.GetGenericArguments()[0]; | ||
entities.Add(new ContextEntity | ||
{ | ||
EntityName = GetResourceName(property), | ||
EntityType = entityType, | ||
Attributes = GetAttributes(entityType), | ||
Relationships = GetRelationships(entityType) | ||
}); | ||
} | ||
} | ||
|
||
Entities = entities; | ||
} | ||
|
||
private string GetResourceName(PropertyInfo property) | ||
{ | ||
var resourceAttribute = property.GetCustomAttribute(typeof(ResourceAttribute)); | ||
if(resourceAttribute == null) | ||
return property.Name.Dasherize(); | ||
|
||
return ((ResourceAttribute)resourceAttribute).ResourceName; | ||
} | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This name is not dasherized. Why?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See comment below. I'd like to avoid mutating any manual input so we can avoid inconsistencies as described in #93