Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
nathan-parkinson committed Jan 3, 2019
2 parents 2506b18 + 6c30c2f commit 657572c
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 20 deletions.
1 change: 1 addition & 0 deletions src/LinqToDB.Include/Accessors/IRootAccessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace LinqToDB.Include
interface IRootAccessor
{
PropertyAccessor<TEntity, TProperty> GetByPath<TEntity, TProperty>(List<string> pathParts) where TEntity : class where TProperty : class;
IPropertyAccessor GetByPath(List<string> pathParts);
MappingSchema MappingSchema { get; }
}
}
17 changes: 17 additions & 0 deletions src/LinqToDB.Include/Accessors/RootAccessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,23 @@ PropertyAccessor<TEntity, TProperty> IRootAccessor.GetByPath<TEntity, TProperty>
return result as PropertyAccessor<TEntity, TProperty>;
}

IPropertyAccessor IRootAccessor.GetByPath(List<string> pathParts)
{
var thisPath = pathParts.First();

//TODO get by Type and PropertyName to account for multiple inherited classes with same PropertyName
var accessor = Properties.SingleOrDefault(x => x.PropertyName == thisPath);
if (accessor == null)
{
return null;
}

var result = accessor.FindAccessor(pathParts.Skip(1).ToList());

return result;
}


public void LoadMap(List<TClass> entities, IQueryable<TClass> query)
{
foreach (var propertyAccessor in Properties.OrderBy(x => x.PropertyName))
Expand Down
43 changes: 23 additions & 20 deletions src/LinqToDB.Include/Visitors/PropertyVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,17 @@ internal PropertyVisitor(IRootAccessor<TClass> rootAccessor)
{
_rootAccessor = rootAccessor;
}


private static void AddFilterForInheritedType<T, TProperty>(IPropertyAccessor<T> accessor,
Expression<Func<TProperty, bool>> includeFilter)
where T : class
where TProperty : class

private static void AddFilterForDynamicType<T, TProperty>(IPropertyAccessor<T> accessor,
Expression<Func<TProperty, bool>> includeFilter)
where T : class
where TProperty : class
{
var accessorImpl = (PropertyAccessor<T, TProperty>)accessor;
var accessorImpl = accessor as PropertyAccessor<T, TProperty>;
if (accessorImpl == null)
{
throw new PropertyAccessorNotFoundException($"PropertyAccessor<{typeof(T).Name}, {typeof(TProperty).Name}> not found");
}
accessorImpl.AddFilter(includeFilter);
}

Expand All @@ -31,28 +34,27 @@ public IRootAccessor<TClass> MapProperties<TProperty>(Expression<Func<TClass, TP
where TProperty : class
{
Visit(expr);

var accessor = latestAccessor as IPropertyAccessor<TClass>;
//dupes at this point
if (!_rootAccessor.Properties.Contains(accessor))
{
_rootAccessor.Properties.Add(accessor);
}

if (includeFilter != null)
{
if (latestAccessor is PropertyAccessor<TClass, TProperty> accessorImpl)
{
accessorImpl.AddFilter(includeFilter);
}
else if (typeof(TClass).IsAssignableFrom(latestAccessor.DeclaringType))
{
dynamic dynamicAccessor = latestAccessor;
AddFilterForInheritedType(dynamicAccessor, includeFilter);
}
else
{
throw new PropertyAccessorNotFoundException($"PropertyAccessor<{typeof(TClass).Name}, {typeof(TProperty).Name}> not found");
}
}

//dupes at this point
if (!_rootAccessor.Properties.Contains(accessor))
{
_rootAccessor.Properties.Add(accessor);
//can type checking be added here?
var propertyAccessor = _rootAccessor.GetByPath(PathWalker.GetPath(expr));
dynamic dynamicAccessor = propertyAccessor;
AddFilterForDynamicType(dynamicAccessor, includeFilter);
}
}

return _rootAccessor;
Expand Down Expand Up @@ -86,6 +88,7 @@ protected override Expression VisitMember(MemberExpression node)
{
//check PropertyAccessor for the property does not already exist
var localAccessor = CreateAccessor(node);

latestAccessor = localAccessor;
return base.VisitMember(node);
}
Expand Down

0 comments on commit 657572c

Please sign in to comment.