Skip to content

v2.0.7 #116

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 9 commits into from
May 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions docs/EntityRepositories.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,14 @@ A sample implementation that performs data authorization might look like:
public class MyAuthorizedEntityRepository : DefaultEntityRepository<MyEntity>
{
private readonly ILogger _logger;
private readonly AppDbContext _context;
private readonly IAuthenticationService _authenticationService;

public MyAuthorizedEntityRepository(AppDbContext context,
public MyAuthorizedEntityRepository(
ILoggerFactory loggerFactory,
IJsonApiContext jsonApiContext,
IAuthenticationService authenticationService)
: base(context, loggerFactory, jsonApiContext)
{
_context = context;
: base(loggerFactory, jsonApiContext)
{
_logger = loggerFactory.CreateLogger<MyEntityRepository>();
_authenticationService = authenticationService;
}
Expand Down
21 changes: 21 additions & 0 deletions src/JsonApiDotNetCore/Data/DbContextResolver.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;
using JsonApiDotNetCore.Extensions;
using Microsoft.EntityFrameworkCore;

namespace JsonApiDotNetCore.Data
{
public class DbContextResolver : IDbContextResolver
{
private readonly DbContext _context;

public DbContextResolver(DbContext context)
{
_context = context;
}

public DbContext GetContext() => _context;

public DbSet<TEntity> GetDbSet<TEntity>() where TEntity : class
=> _context.GetDbSet<TEntity>();
}
}
27 changes: 23 additions & 4 deletions src/JsonApiDotNetCore/Data/DefaultEntityRepository.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
Expand All @@ -17,12 +18,19 @@ public class DefaultEntityRepository<TEntity>
IEntityRepository<TEntity>
where TEntity : class, IIdentifiable<int>
{
[Obsolete("DbContext is no longer directly injected into the ctor. Use JsonApiContext.GetDbContextResolver() instead")]
public DefaultEntityRepository(
DbContext context,
ILoggerFactory loggerFactory,
IJsonApiContext jsonApiContext)
: base(context, loggerFactory, jsonApiContext)
{ }

public DefaultEntityRepository(
ILoggerFactory loggerFactory,
IJsonApiContext jsonApiContext)
: base(loggerFactory, jsonApiContext)
{ }
}

public class DefaultEntityRepository<TEntity, TId>
Expand All @@ -35,6 +43,7 @@ public class DefaultEntityRepository<TEntity, TId>
private readonly IJsonApiContext _jsonApiContext;
private readonly IGenericProcessorFactory _genericProcessorFactory;

[Obsolete("DbContext is no longer directly injected into the ctor. Use JsonApiContext.GetDbContextResolver() instead")]
public DefaultEntityRepository(
DbContext context,
ILoggerFactory loggerFactory,
Expand All @@ -47,6 +56,18 @@ public DefaultEntityRepository(
_genericProcessorFactory = _jsonApiContext.GenericProcessorFactory;
}

public DefaultEntityRepository(
ILoggerFactory loggerFactory,
IJsonApiContext jsonApiContext)
{
var contextResolver = jsonApiContext.GetDbContextResolver();
_context = contextResolver.GetContext();
_dbSet = contextResolver.GetDbSet<TEntity>();
_jsonApiContext = jsonApiContext;
_logger = loggerFactory.CreateLogger<DefaultEntityRepository<TEntity, TId>>();
_genericProcessorFactory = _jsonApiContext.GenericProcessorFactory;
}

public virtual IQueryable<TEntity> Get()
{
if(_jsonApiContext.QuerySet?.Fields != null && _jsonApiContext.QuerySet.Fields.Any())
Expand Down Expand Up @@ -111,10 +132,8 @@ public virtual async Task<TEntity> UpdateAsync(TId id, TEntity entity)
if (oldEntity == null)
return null;

_jsonApiContext.RequestEntity.Attributes.ForEach(attr =>
{
attr.SetValue(oldEntity, attr.GetValue(entity));
});
foreach(var attr in _jsonApiContext.AttributesToUpdate)
attr.Key.SetValue(oldEntity, attr.Value);

foreach(var relationship in _jsonApiContext.RelationshipsToUpdate)
relationship.Key.SetValue(oldEntity, relationship.Value);
Expand Down
11 changes: 11 additions & 0 deletions src/JsonApiDotNetCore/Data/IDbContextResolver.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Microsoft.EntityFrameworkCore;

namespace JsonApiDotNetCore.Data
{
public interface IDbContextResolver
{
DbContext GetContext();
DbSet<TEntity> GetDbSet<TEntity>()
where TEntity : class;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ public static void AddJsonApiInternals(
services.AddSingleton<DbContextOptions>(new DbContextOptionsBuilder().Options);
}

services.AddScoped<IDbContextResolver, DbContextResolver>();
services.AddScoped(typeof(IEntityRepository<>), typeof(DefaultEntityRepository<>));
services.AddScoped(typeof(IEntityRepository<,>), typeof(DefaultEntityRepository<,>));
services.AddScoped(typeof(IResourceService<>), typeof(EntityResourceService<>));
Expand Down
2 changes: 1 addition & 1 deletion src/JsonApiDotNetCore/JsonApiDotNetCore.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<VersionPrefix>2.0.6</VersionPrefix>
<VersionPrefix>2.0.7</VersionPrefix>
<TargetFrameworks>netstandard1.6</TargetFrameworks>
<AssemblyName>JsonApiDotNetCore</AssemblyName>
<PackageId>JsonApiDotNetCore</PackageId>
Expand Down
6 changes: 6 additions & 0 deletions src/JsonApiDotNetCore/Models/AttrAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ public AttrAttribute(string publicName)
PublicAttributeName = publicName;
}

public AttrAttribute(string publicName, string internalName)
{
PublicAttributeName = publicName;
InternalAttributeName = internalName;
}

public string PublicAttributeName { get; set; }
public string InternalAttributeName { get; set; }

Expand Down
24 changes: 12 additions & 12 deletions src/JsonApiDotNetCore/Serialization/JsonApiDeSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public JsonApiDeSerializer(
public object Deserialize(string requestBody)
{
var document = JsonConvert.DeserializeObject<Document>(requestBody);
var entity = DataToObject(document.Data);
var entity = DocumentToObject(document.Data);
return entity;
}

Expand All @@ -46,30 +46,29 @@ public object DeserializeRelationship(string requestBody)
return new List<DocumentData> { data.ToObject<DocumentData>() };
}


public List<TEntity> DeserializeList<TEntity>(string requestBody)
{
var documents = JsonConvert.DeserializeObject<Documents>(requestBody);

var deserializedList = new List<TEntity>();
foreach (var data in documents.Data)
{
var entity = DataToObject(data);
var entity = DocumentToObject(data);
deserializedList.Add((TEntity)entity);
}

return deserializedList;
}

private object DataToObject(DocumentData data)
private object DocumentToObject(DocumentData data)
{
var contextEntity = _jsonApiContext.ContextGraph.GetContextEntity(data.Type);
_jsonApiContext.RequestEntity = contextEntity;

var entity = Activator.CreateInstance(contextEntity.EntityType);

entity = _setEntityAttributes(entity, contextEntity, data.Attributes);
entity = _setRelationships(entity, contextEntity, data.Relationships);
entity = SetEntityAttributes(entity, contextEntity, data.Attributes);
entity = SetRelationships(entity, contextEntity, data.Relationships);

var identifiableEntity = (IIdentifiable)entity;

Expand All @@ -79,7 +78,7 @@ private object DataToObject(DocumentData data)
return identifiableEntity;
}

private object _setEntityAttributes(
private object SetEntityAttributes(
object entity, ContextEntity contextEntity, Dictionary<string, object> attributeValues)
{
if (attributeValues == null || attributeValues.Count == 0)
Expand All @@ -99,13 +98,14 @@ private object _setEntityAttributes(
{
var convertedValue = TypeHelper.ConvertType(newValue, entityProperty.PropertyType);
entityProperty.SetValue(entity, convertedValue);
_jsonApiContext.AttributesToUpdate[attr] = convertedValue;
}
}

return entity;
}

private object _setRelationships(
private object SetRelationships(
object entity,
ContextEntity contextEntity,
Dictionary<string, RelationshipData> relationships)
Expand All @@ -118,14 +118,14 @@ private object _setRelationships(
foreach (var attr in contextEntity.Relationships)
{
entity = attr.IsHasOne
? _setHasOneRelationship(entity, entityProperties, attr, contextEntity, relationships)
: _setHasManyRelationship(entity, entityProperties, attr, contextEntity, relationships);
? SetHasOneRelationship(entity, entityProperties, attr, contextEntity, relationships)
: SetHasManyRelationship(entity, entityProperties, attr, contextEntity, relationships);
}

return entity;
}

private object _setHasOneRelationship(object entity,
private object SetHasOneRelationship(object entity,
PropertyInfo[] entityProperties,
RelationshipAttribute attr,
ContextEntity contextEntity,
Expand Down Expand Up @@ -158,7 +158,7 @@ private object _setHasOneRelationship(object entity,
return entity;
}

private object _setHasManyRelationship(object entity,
private object SetHasManyRelationship(object entity,
PropertyInfo[] entityProperties,
RelationshipAttribute attr,
ContextEntity contextEntity,
Expand Down
3 changes: 3 additions & 0 deletions src/JsonApiDotNetCore/Services/IJsonApiContext.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections.Generic;
using JsonApiDotNetCore.Builders;
using JsonApiDotNetCore.Configuration;
using JsonApiDotNetCore.Data;
using JsonApiDotNetCore.Internal;
using JsonApiDotNetCore.Internal.Generics;
using JsonApiDotNetCore.Internal.Query;
Expand All @@ -22,6 +23,8 @@ public interface IJsonApiContext
PageManager PageManager { get; set; }
IMetaBuilder MetaBuilder { get; set; }
IGenericProcessorFactory GenericProcessorFactory { get; set; }
Dictionary<AttrAttribute, object> AttributesToUpdate { get; set; }
Dictionary<RelationshipAttribute, object> RelationshipsToUpdate { get; set; }
IDbContextResolver GetDbContextResolver();
}
}
25 changes: 17 additions & 8 deletions src/JsonApiDotNetCore/Services/JsonApiContext.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System;
using System.Collections.Generic;
using System.Linq;
using JsonApiDotNetCore.Builders;
using JsonApiDotNetCore.Configuration;
using JsonApiDotNetCore.Data;
using JsonApiDotNetCore.Internal;
using JsonApiDotNetCore.Internal.Generics;
using JsonApiDotNetCore.Internal.Query;
Expand All @@ -13,19 +15,22 @@ namespace JsonApiDotNetCore.Services
public class JsonApiContext : IJsonApiContext
{
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly IDbContextResolver _contextResolver;

public JsonApiContext(
IDbContextResolver contextResolver,
IContextGraph contextGraph,
IHttpContextAccessor httpContextAccessor,
JsonApiOptions options,
IMetaBuilder metaBuilder,
IGenericProcessorFactory genericProcessorFactory)
{
_contextResolver = contextResolver;
ContextGraph = contextGraph;
_httpContextAccessor = httpContextAccessor;
Options = options;
MetaBuilder = metaBuilder;
GenericProcessorFactory = genericProcessorFactory;
RelationshipsToUpdate = new Dictionary<RelationshipAttribute, object>();
}

public JsonApiOptions Options { get; set; }
Expand All @@ -39,16 +44,17 @@ public JsonApiContext(
public PageManager PageManager { get; set; }
public IMetaBuilder MetaBuilder { get; set; }
public IGenericProcessorFactory GenericProcessorFactory { get; set; }
public Dictionary<RelationshipAttribute, object> RelationshipsToUpdate { get; set; }
public Dictionary<AttrAttribute, object> AttributesToUpdate { get; set; } = new Dictionary<AttrAttribute, object>();
public Dictionary<RelationshipAttribute, object> RelationshipsToUpdate { get; set; } = new Dictionary<RelationshipAttribute, object>();

public IJsonApiContext ApplyContext<T>()
{
var context = _httpContextAccessor.HttpContext;
var path = context.Request.Path.Value.Split('/');

RequestEntity = ContextGraph.GetContextEntity(typeof(T));
if(context.Request.Query.Any())

if (context.Request.Query.Any())
{
QuerySet = new QuerySet(this, context.Request.Query);
IncludedRelationships = QuerySet.IncludedRelationships;
Expand All @@ -61,14 +67,17 @@ public IJsonApiContext ApplyContext<T>()
return this;
}

public IDbContextResolver GetDbContextResolver() => _contextResolver;

private PageManager GetPageManager()
{
if(Options.DefaultPageSize == 0 && (QuerySet == null || QuerySet.PageQuery.PageSize == 0))
if (Options.DefaultPageSize == 0 && (QuerySet == null || QuerySet.PageQuery.PageSize == 0))
return new PageManager();

var query = QuerySet?.PageQuery ?? new PageQuery();

return new PageManager {
var query = QuerySet?.PageQuery ?? new PageQuery();

return new PageManager
{
DefaultPageSize = Options.DefaultPageSize,
CurrentPage = query.PageOffset > 0 ? query.PageOffset : 1,
PageSize = query.PageSize > 0 ? query.PageSize : Options.DefaultPageSize
Expand Down
Loading