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:
.Entry(tabAccount) -> should take every EntitySet
x => x.TabAccountLangs -> should take the property I specify on calling the Method (maybe threw MemberExpression)
from x ...TabAccounts -> should load the DbSet from the EntitySet I am calling the method with
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.