Creating Generic Entity/Collection Fetching

Posts   
 
    
MWNoel
User
Posts: 3
Joined: 03-Jan-2017
# Posted on: 03-Jan-2017 19:16:33   

Project Details

  • LLBLGen Pro 3.5 Final (Nov 6th 2012)
  • Adapter
  • .NET 4.5
  • SQLServer 2008 R2
  • Using Telerik for ASP.NET My Problem I have a form, which needs to have drop-downs (Text and Value) populated from a database I'm accessing with LLBLGen DataAccessAdapter.
    The drop-down is made with Telerik, but the data source I'm setting is functionally the same as an asp drop-down's data source.

I'm looking for a way to generically call my database through my BLL so that I can simply call something like:

DropDown.DataSource = GetEntityCollection<OrderEntity>();

or

DropDown.DataSource = GetEntityCollection(OrderEntity);

Given that I often need whole tables which are small, I don't want to rewrite or copy-paste code that is functionally identical but with different targets.

I was initially attempting to do this like seen below. Since I need the type to be passed in as a generic or parameter I can't use as the type for EntityCollection<>, according to what I understand.

public static object GetEntityCollection<T>()  //Or GetEntityCollection(EntityBase2 entity)
{
    using (DataAccessAdapter adapter = new DataAccessAdapter(CONNECTION))
    {
        EntityCollection<typeof(T)> collection = new EntityCollection<typeof(T)>();
        //EntityCollection<EntityBase2> collection = new EntityCollection<entity.type> ??
        try
        {
            adapter.FetchEntityCollection(collection, null);
        }
        catch
        {
            //Log Exception & return empty object
        }

        return collection;
    }
}

Finally, If this is not possible directly, is there a better way to split this up to avoid rewriting as a much for every single database entity/collection I need to fetch?

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 03-Jan-2017 19:48:20   
public static IEntityCollection2 GetEntityCollection<T>() where T : EntityBase2
{
    using (DataAccessAdapter adapter = new DataAccessAdapter())
    {
        IEntityCollection2 collection = new EntityCollection<T> ();
        try
        {
            adapter.FetchEntityCollection(collection, null);
        }
        catch
        {
            //Log Exception & return empty object
        }

        return collection;
    }
}

MWNoel
User
Posts: 3
Joined: 03-Jan-2017
# Posted on: 03-Jan-2017 20:38:19   

That's fantastic thank you! I can't believe how close I was and just couldn't get it.

I was able to get in bucket predicates, so out of curiosity is there a similar way to get in prefetch paths?

Wherever I need to define my filter

Dictionary<EntityField2, string> filterItems = new Dictionary<EntityField2, string>();
filterItems.Add(ContactFields.TypeCode, DropDownList.SelectedValue);

Then in the method for getting the collection

public static IEntityCollection2 GetEntityCollection<T>(Dictionary<EntityField2, string> filterItems = null) where T : EntityBase2
{
    using (DataAccessAdapter adapter = new DataAccessAdapter(CONNECTION_PMNDataCenter))
    {
        IEntityCollection2 collection = new EntityCollection<T>();
        RelationPredicateBucket bucket = new RelationPredicateBucket();
        try
        {
            if (filterItems != null && filterItems.Count > 0)
            {
                foreach (KeyValuePair<EntityField2, string> entry in filterItems)
                {
                    bucket.PredicateExpression.Add(entry.Key == entry.Value);
                }
            }
            else
            {
                bucket = null;
            }

            //Something for Prefetch paths here??
            
            adapter.FetchEntityCollection(collection, bucket);
        }
        catch
        {
            //Log Exception
        }

        return collection;
    }
}

I was looking for a way to iterate possible prefetch paths that may be on an Entity, but I was unsuccessful. And there's the real problem of having the prefetch grab everything related to an entity which could end up being millions of unnecessary items. So what do you think?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 04-Jan-2017 15:01:18   

There's no iteratable set of prefetch paths for a given entity 'e'. You have to specify these separately, as that's the intended use case.

In a generic way it doesn't really make sense to have this, as where to stop? You could fetch the whole database (as x is related to y, is related to z etc.), and do you want all related entities 1 level deep? Or just one specific related entity?

Frans Bouma | Lead developer LLBLGen Pro
MWNoel
User
Posts: 3
Joined: 03-Jan-2017
# Posted on: 04-Jan-2017 18:14:57   

I was concerned that if it were possible, that exact scenario could happen.

I suppose given the circumstance where I'll need prefetching I'll just have to come up with an alternative.

Thank you Otis.