.net - Nhibernate filter not consistently applying to child collections -
i have entity child objects soft deleted. when call simple on parent, want retrieve on child objects not soft deleted. entities have base class id, audit, soft delete fields kept.
in order achieve created 2 event listeners , 1 filter, 1 event listener cascade soft delete if necessary, , apply filter on preload.
public class nondeletedfilter : filterdefinition { public static string filtername = "nondeletedfilter"; public nondeletedfilter() { withname(filtername).withcondition("isdeleted = 0"); } } public class parentmap : iautomappingoverride<parent> { public void override(fluentnhibernate.automapping.automapping<parent> mapping) { mapping.hasmany(x => x.children).fetch.join() .inverse() .cascade.alldeleteorphan() .applyfilter(nondeletedfilter.filtername); } } public class preloadeventlistener : defaultpreloadeventlistener { public override void onpreload(nhibernate.event.preloadevent preloadevent) { preloadevent.session.enablefilter(nondeletedfilter.filtername); base.onpreload(preloadevent); } }
here issue, , it's worst kind: works. in test cases, creates sql perfectly. selects parent, has left outer join child , makes children isdeleted = false. in application not, join without checking. works on seperate parent/child relationship same mapping override applied.
the configuration built same mappings, has same filters , event listeners. difference can see test uses inmemory sqlite db database created based on mappings , initialization sql executed prepopulate database. it's populated actual data , can't find differences.
at point suppose question should look?
here thoughts. tables not right? fine. mapping missing something? same. filter not being applied? another. filter working? another.
perhaps i've looked @ code can't see issue. can shed light on concentrate efforts?
after few mind boggling hours issue revealed itself. have base repository call apply fetch statements properties. class broken, repository fetching same property, thereby overriding filter setup in map class. , yes, had been looking @ long notice difference. set eyes working through it, , noticed working 1 taking different path through repository. so, +1 pair programming.
and wondering why preload event listener not working, make when create custom criteria, don't override fetching strategy.
i should have known better since used elsewhere bypass filter , explicitly return objects regardless of soft delete.
here example of done in repository.
public override parent get(id) { session.createcriteria<parent>() .fetch<parent>(x => x.children) } public static icriteria fetch<t>(this icriteria criteria, params expression<func<t, object>>[] fetch) { foreach (expression<func<t, object>> expression in fetch) criteria.setfetchmode(expression, fetchmode.join); return criteria; }
included extension method clean code when creating custom criteria.
Comments
Post a Comment