Skip to content

Commit

Permalink
Refactored to make less horrible
Browse files Browse the repository at this point in the history
  • Loading branch information
hughesjs committed May 6, 2024
1 parent 1b448a1 commit 0526bae
Showing 1 changed file with 11 additions and 19 deletions.
30 changes: 11 additions & 19 deletions src/UniversalDiveDataFormat/Services/LinkResolutionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ public class LinkResolutionService
private readonly List<Link> _links;

private readonly bool _failOnMissingLink;

private static bool IsInModelNamespace(Type t) => t.Namespace!.StartsWith("UniversalDiveDataFormat.Models");
private static bool IsList(Type type) => type.IsGenericType && type.GetGenericTypeDefinition().IsAssignableTo(typeof(IEnumerable));
private static bool IsListOfModelObjects(Type type) => IsList(type) && IsInModelNamespace(type.GetGenericArguments().Single());


public LinkResolutionService(bool failOnMissingLink = true)
{
Expand All @@ -24,6 +29,8 @@ public LinkResolutionService(bool failOnMissingLink = true)

public void ResolveAllLinksInObjectGraph(object root)
{
_linkableLookup.Clear();
_links.Clear();
// Maybe make everything inherit from an empty base and use that rather than object
if (!IsInModelNamespace(root.GetType()))
{
Expand All @@ -46,41 +53,26 @@ private void BuildLinks()
link.SetLinkedObject(linkable);
}
}

private static bool IsInModelNamespace(Type t) => t.Namespace!.StartsWith("UniversalDiveDataFormat.Models");
private static bool IsList(Type type) => type.IsGenericType && type.GetGenericTypeDefinition().IsAssignableTo(typeof(IEnumerable));
private static bool IsListOfModelObjects(Type type) => IsList(type) && IsInModelNamespace(type.GetGenericArguments().Single());

private void FindAllLinkablesAndLinksInSubgraph(object root)
private void FindAllLinkablesAndLinksInSubgraph(object? root)
{
if (root is null) return;
PropertyInfo[] properties = root.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.GetProperty);

ProcessModelObject(root);
foreach (PropertyInfo property in properties)
{
object? propertyValue = property.GetValue(root);
if (propertyValue is null) continue;
if (IsListOfModelObjects(property.PropertyType))
{
ProcessListOfModelObjects((IEnumerable)propertyValue);
((IEnumerable)propertyValue).Cast<object?>().ToList().ForEach(FindAllLinkablesAndLinksInSubgraph);
continue;
}

if (!IsInModelNamespace(property.PropertyType)) continue;

ProcessModelObject(propertyValue);
FindAllLinkablesAndLinksInSubgraph(propertyValue);
}
}

private void ProcessListOfModelObjects(IEnumerable list)
{
foreach (object? item in list)
{
if (item is null) continue;
ProcessModelObject(item);
FindAllLinkablesAndLinksInSubgraph(item);
}
}

private void ProcessModelObject(object model)
{
Expand Down

0 comments on commit 0526bae

Please sign in to comment.