Hi,
I am creating a Web API using ASP.NET Core Web Application (using the .NET Framework) with Database First. I am using LLBLGen Pro version 5.1 and the LLBLGen Runtime.
I have two questions:
1: I have two tables with a many-to-many relationship, Item and Collection which are related through the ItemCollection table and I am trying to create a list of all my items including the names of all associated Collections (which is a property in my Collection table). I have read about the prefetch paths and using the below code:
var path = new PrefetchPath2(EntityType.ItemEntity);
path.Add(ItemEntity.PrefetchPathItemCollections);
using (DataAccessAdapter adapter = new DataAccessAdapter(“ConnectionString”))
{
var data = new LinqMetaData(adapter);
return data.Item.Select(all => all).WithPath(path).ToList();
}
This gets me the ids of an Item's associated Collections, but not the Collection object itself. This means that I have to match the Collection id with the Collection table in a separate query in order to get the Collection name, like so:
//Get all items include relationship tables (items)
//Get all items from the collection table in (collections)
foreach (ItemEntity i in items)
{
foreach (ItemCollectionEntity ice in i.ItemCollections)
{
var name = collections.Where(x => x.Id == ice.CollectionId).Select(y => y.Name).First());
}
}
Compared to the same in Entity Framework:
//Get all items include collections
foreach (Item i in items)
{
foreach (Collection c in i.collections)
{
var name = c.Name;
}
}
It is not a big deal, I am just sure I am missing something?
2: In order to reduce the amount of overhead and make the code easier to unit-test, I am trying to make a generic version of the most common methods, which is GetAll, Find, Post, Put and Delete (inspired by https://goo.gl/4DnImB). However, I have run into a problem with a generic get method (I am new to generics) where I can specify the prefetch paths as an argument (using the knowledge from the first question).
What I am trying to do is something like this (pseudo-code):
public class GenericRepository<TEntity> : IGenericRepository<TEntity> where TEntity : class
{
public IEnumerable<TEntity> GetAllWithPrefetch(List<string> prefetches)
{
var path = new PrefetchPath2(EntityType.TEntity);
path.Add(prefetches);
using (DataAccessAdapter adapter = new DataAccessAdapter("ConnectionString"))
{
var data = new LinqMetaData(adapter);
return data.GetQueryableForEntity<TEntity>().Select(all => all).WithPath(path).ToList();
}
}
}
However the PrefetchPath2 takes an EntityType enum which is specific to the object type and there seem to be no way to call WithPath on the GetQueryableForEntity() so I am quite stuck. Do you have some pointers on how to achieve the above?
Best regards
Andreas
Edited for clarity