0

I would like to write the following code more abstract.

private void LoadRelatedData(TabAccount tabAccount)
{
    if (ConxCore.Instance.EntityModel.Entry(tabAccount).Collection(x => x.TabAccountLangs).IsLoaded == false)
    {
        var list = (from x in ConxCore.Instance.EntityModel.TabAccounts
            from y in x.TabAccountLangs
            select y).ToList();
    }
}

The parts I would like to abstract are the following:

  1. .Entry(tabAccount) -> should take every EntitySet

  2. x => x.TabAccountLangs -> should take the property I specify on calling the Method (maybe threw MemberExpression)

  3. from x ...TabAccounts -> should load the DbSet from the EntitySet I am calling the method with

  4. from y in x.TabAccountLangs -> should be the property from above

About the abstraction, I just want to avoid copy/pasting this code over and over and just changing the 4 mentioned points, would be happy to be able to call this method with paramaters given and the method does the rest.

So instead of:

private void LoadRelatedData(TabAccount tabAccount)
{
    if (ConxCore.Instance.EntityModel.Entry(tabAccount).Collection(x => x.TabAccountLangs).IsLoaded == false)
    {
        var list = (from x in ConxCore.Instance.EntityModel.TabAccounts
            from y in x.TabAccountLangs
            select y).ToList();
    }
}

private void LoadRelatedData(TabElement tabElement)
{
    if (ConxCore.Instance.EntityModel.Entry(tabElement).Collection(x => x.TabElementLangs).IsLoaded == false)
    {
        var list = (from x in ConxCore.Instance.EntityModel.TabElements
            from y in x.TabElementLangs
            select y).ToList();
    }
}

Something like this (only pseudo code):

private void LoadRelatedData(object obj, object collection, object dbSetOfObj)
{
    if (ConxCore.Instance.EntityModel.Entry(obj).Collection(x => x.collection).IsLoaded == false)
    {
        var list = (from x in ConxCore.Instance.EntityModel.dbSetOfObj
            from y in x.collection
            select y).ToList();
    }
}

And call this method:

LoadRelatedData(tabAccount, TabAccountLangs, TabAccounts);
LoadRelatedData(tabElement, TabElementLangs, TabElements);

Hope you can help me out. Thanks in advance.

4

1 回答 1

2
using System.Data.Entity; // For the Include extension method.

private void LoadRelatedData<TEntity, TRelated>(TEntity entity, Expression<Func<TEntity, ICollection<TRelated>> navigationProperty)
    where TEntity : class
    where TRelated : class
{
    if (!ConxCore.Instance.EntityModel.Entry(entity).Collection(navigationProperty).IsLoaded) 
    {
        var list = ConxCore.Instance.EntityModel.Set<TEntity>().Include(navigationProperty).ToList();
    }
}

然后,您可以通过以下方式调用它:

LoadRelatedData(tabAccount, ta => ta.TabAccountLangs);
LoadRelatedData(tabElement, te => te.TabElementLangs);

但是,您知道这样的调用将加载所有相关数据,对于任何 x? 也就是说,您可能会以非特别优化的方式加载大量数据。如果只想加载单个实体的相关数据,可以从Collection调用返回的对象中加载集合:

private void LoadRelatedDataOfSingleObject<TEntity, TRelated>(TEntity entity, Expression<Func<TEntity, ICollection<TRelated>> navigationProperty)
    where TEntity : class
    where TRelated : class
{
    var collectionEntry = ConxCore.Instance.EntityModel.Entry(entity).Collection(navigationProperty);
    if (!collectionEntry.IsLoaded) collectionEntry.Load();
}

但是,如果要加载许多对象的数据,则应考虑使用其中一种Include扩展方法

于 2013-08-06T12:31:27.983 回答