Nhibernate filter is not applied sequentially to children's collections

I have an object with child objects that are gently removed. When I call simple access to the parent object, I want it to retrieve to child objects that have not been softly deleted. All my objects have a base class in which the fields id, audit, soft delete are stored.

To do this, I created 2 event listeners and 1 filter, one event listener will cascade soft deletion, if necessary, and the other will apply the filter at 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);
    }
}

, : . sql. , isdeleted = false. , . / .

, . , , - , inmemory sqlite db, , sql . , .

, - ?

. ? . ? . ? , . ? .

, , . - , ?

+3
1

. . , , , , . , , . , , . , +1 .

, , preload , , .

, , .

, .

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;
}

.

0

Source: https://habr.com/ru/post/1794184/


All Articles