Base solution for your next web application
Ends in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

working with trees #4869


User avatar
0
BobIngham created

aspnet core, angular 5.1.0 I implement generic tree builder methods using the code here: [https://stackoverflow.com/questions/19648166/nice-universal-way-to-convert-list-of-items-to-tree]) Which implements the following:

public class TreeItem<T>
{
    public T Item { get; set; }
    public IEnumerable<TreeItem<T>> Children { get; set; }
}

using the following generic helper:

public static IEnumerable<TreeItem<T>> GenerateTree<T, K>(
        this IEnumerable<T> collection,
        Func<T, K> id_selector,
        Func<T, K> parent_id_selector,
        K root_id = default(K))
    {
        foreach (var c in collection.Where(c => parent_id_selector(c).Equals(root_id)))
        {
            yield return new TreeItem<T>
            {
                Item = c,
                Children = collection.GenerateTree(id_selector, parent_id_selector, id_selector(c))
            };
        }
    }

I then implement this in my service:

public async Task<List<TreeItem<NcActionDto>>> GetAllAsTree()
{
    var query = _ncActionRepository.GetAll();
    var ncActions = await query
        .ToListAsync();

    var actionTree = ncActions.GenerateTree(m => m.Id, m => m.ParentId);

    return ObjectMapper.Map<List<TreeItem<NcActionDto>>> (actionTree);
}

I can run this successfully using the swagger interface but when I come to refresh my service proxies it is not present.

Is there any reason why the nswag generation should not pick up this method?


2 Answer(s)
  • User Avatar
    0
    BobIngham created

    I'm not sure why this didn't work, maybe it has something do with nswag and generics. I changed the code to work with a recursive function and a nested list so it's not a show-stopper.

  • User Avatar
    0
    alper created
    Support Team

    From the perspective of WebAPI, using generics may not be a good idea. but swagger recommends some kinda configuration for that. maybe it'd be useful for you. See <a class="postlink" href="https://github.com/domaindrivendev/Swashbuckle/issues/600">https://github.com/domaindrivendev/Swas ... issues/600</a>