Friday, 15 June 2012

c# - Apply Expression Tree Logic Directly on Properties? -



c# - Apply Expression Tree Logic Directly on Properties? -

it's possible (perhaps likely) i'm mixing terminology here. allow me explain idea...

currently have entity framework 6 code-first kind of approach, persistence-ignorant models , interfaces iunitofwork , ientityrepository abstract dbcontext , dbset<entity> respectively. , naturally it's "wired up" database via fluent mapping syntax. kid object on entity model:

public ilist<field> fields { get; set; }

i have mapping such as:

hasmany(e => e.fields) .withrequired(f => f.entity) .hasforeignkey(f => f.entityid) .willcascadeondelete(false);

this same parent/child relationship continues through sizable graph of objects under entity aggregate root. , times in application find need iterate on kid objects various reasons. when have ignore elements (ones soft-deleted, example), means i'm materializing records database don't need.

so i'm wondering (and far searching/tinkering hasn't found solution) if there's way apply sort of logic straight field mapping result in db-side filtering when lazy-loading property. @ simplest, image .where() clause on fluent mapping above.

for example, might have look tree static property on field model:

public static expression<func<field, bool>> isactive = f => f.versions.orderbydescending(v => v.versiondate) .firstordefault.isactive == true;

if utilize in .where() clause straight in dbset<field> might like:

var activefields = db.fields.where(field.isactive);

then when info materialized, look tree logic applied db-side making whole thing little less chatty database. perhaps separate property on model:

public ilist<field> allfields { get; set; } public ilist<field> activefields { get; set; }

each own mapping. additional logic mapped additional fields. ideally point here offload much of querying logic possible database. maybe there's technique creating calculated fields in entity framework offload logic database, provided logic can expressed in linq entities expression?

c# entity-framework ef-code-first expression-trees

No comments:

Post a Comment