Filter soft deleted entities

102 views
Skip to first unread message

Cassio Tavares

unread,
May 20, 2019, 10:50:35 PM5/20/19
to RavenDB - 2nd generation document database
Hi,

My entities have a IsDeleted property. I'm trying to use OnBeforeQuery to filter all entities in a query where the IsDeleted == false. My plan is to inherit from a specific interface to identify these entities.

I searched the docs but couldn't find anything useful. 

Is it possible using OnBeforeQuery or any other way?

Thanks

Oren Eini (Ayende Rahien)

unread,
May 21, 2019, 4:42:30 AM5/21/19
to ravendb
Yes, here is how you can handle this:

private void Store_OnBeforeQuery(object sender, BeforeQueryEventArgs e)
{
    switch (e.QueryCustomization)
    {
        case IDocumentQuery<User> userQuery:
            userQuery.AndAlso().WhereEquals("IsDeleted", false);
            break;
        case IAsyncDocumentQuery<User> userAsyncQuery:
            userAsyncQuery.AndAlso().WhereEquals("IsDeleted", false);
            break;
        default:
            break;
    }
}

--
You received this message because you are subscribed to the Google Groups "RavenDB - 2nd generation document database" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ravendb+u...@googlegroups.com.
To post to this group, send email to rav...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ravendb/490d0bc6-37b8-4797-a5dd-8aff7b9d4c90%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


--
Oren Eini
CEO   /   Hibernating Rhinos LTD
Skype:  ayenderahien
Support:  sup...@ravendb.net

Cassio Tavares

unread,
May 22, 2019, 3:28:33 AM5/22/19
to RavenDB - 2nd generation document database
Hi Oren, thank you for your reply but this doesn't work.

e.QueryCustomization is not of type IDocumentQuery<> and the switch will never work.

I searched for any property inside e.QueryCustomization that could do the trick but I couldn't find anything.

Can you (or anyone else) check it?

Thanks again


On Tuesday, May 21, 2019 at 5:42:30 AM UTC-3, Oren Eini wrote:
Yes, here is how you can handle this:

private void Store_OnBeforeQuery(object sender, BeforeQueryEventArgs e)
{
    switch (e.QueryCustomization)
    {
        case IDocumentQuery<User> userQuery:
            userQuery.AndAlso().WhereEquals("IsDeleted", false);
            break;
        case IAsyncDocumentQuery<User> userAsyncQuery:
            userAsyncQuery.AndAlso().WhereEquals("IsDeleted", false);
            break;
        default:
            break;
    }
}

On Tue, May 21, 2019 at 5:50 AM Cassio Tavares <cas...@gmail.com> wrote:
Hi,

My entities have a IsDeleted property. I'm trying to use OnBeforeQuery to filter all entities in a query where the IsDeleted == false. My plan is to inherit from a specific interface to identify these entities.

I searched the docs but couldn't find anything useful. 

Is it possible using OnBeforeQuery or any other way?

Thanks

--
You received this message because you are subscribed to the Google Groups "RavenDB - 2nd generation document database" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rav...@googlegroups.com.

To post to this group, send email to rav...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ravendb/490d0bc6-37b8-4797-a5dd-8aff7b9d4c90%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Oren Eini (Ayende Rahien)

unread,
May 22, 2019, 4:47:45 AM5/22/19
to ravendb
Did you can it? 
IDocumentQueryCustomization is implemented by either AsyncDocumentQuery<T> or DocumentQuery<T>, which implement the relevant interfaces, so this has to work.
If it doesn't, please send us a code sample

To unsubscribe from this group and stop receiving emails from it, send an email to ravendb+u...@googlegroups.com.

To post to this group, send email to rav...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Cassio Tavares

unread,
May 22, 2019, 1:06:28 PM5/22/19
to RavenDB - 2nd generation document database
My mistake.

I was trying to do it with my interface, like 

case IDocumentQuery<ISoftDelete> query

Then I wrongly looked if IDocumentQuery was inherited by IDocumentQueryCustomization, but it is the opposite. Now I see it.

Thanks

Cassio Tavares

unread,
May 22, 2019, 9:59:50 PM5/22/19
to RavenDB - 2nd generation document database
It is working.

I don't know what you think, but IMO we should have a way to handle it only testing an interface. Like in my case. I just want to know if it is an entity that implements ISoftDelete.

The way it is now, I have to create the switch and a case for each of my entities and use the same code in each one.

Or maybe there is a better way to do it that I didn't realize.

Oren Eini (Ayende Rahien)

unread,
May 23, 2019, 1:20:10 AM5/23/19
to ravendb
The problem is that we have generic and multiple levels of inheritance at the same time.
Technically, you could do:

dynamic qc = e.QueryCustomization;
if( typeof(ISoftDelete).IsAssignableFrom( e.QueryCustomization .GetType().GetGenericArguments()[0] ))
       qc.AndAlso().WhereEquals("IsDeleted", false);

 


To unsubscribe from this group and stop receiving emails from it, send an email to ravendb+u...@googlegroups.com.

To post to this group, send email to rav...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Cassio Tavares

unread,
May 23, 2019, 10:48:09 AM5/23/19
to RavenDB - 2nd generation document database
Yes, I understand.

I have tried the dynamic cast before. I thought it could work but it throws an exception. I really don't know why.

Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'Cannot implicitly convert type 'void' to 'object''

Good subject for a blog post? :)

Oren Eini (Ayende Rahien)

unread,
May 24, 2019, 3:06:05 PM5/24/19
to ravendb
Can you send me a test code of your scenario?

To unsubscribe from this group and stop receiving emails from it, send an email to ravendb+u...@googlegroups.com.

To post to this group, send email to rav...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Arão Benjamin Garcea

unread,
Mar 30, 2021, 11:04:41 AM3/30/21
to RavenDB - an awesome database
Hi,

This is my first post in the group, and I see that my question is an already opened subject, so I'll just reply to the conversation.

Any updates on this issue, not using the switch statement?

I used the dynamic example of Oren and I got the same esception: 'Cannot implicitly convert type 'void' to 'object'.

Perhaps we have a different approach, but I didn't found in the documentation.

Thanks!

Oren Eini (Ayende Rahien)

unread,
Mar 30, 2021, 5:12:57 PM3/30/21
to ravendb
Can you send me a test code of your scenario?

You received this message because you are subscribed to the Google Groups "RavenDB - an awesome database" group.

To unsubscribe from this group and stop receiving emails from it, send an email to ravendb+u...@googlegroups.com.

Arão Benjamin Garcea

unread,
Mar 31, 2021, 1:07:23 PM3/31/21
to RavenDB - an awesome database
Hi Oren, thanks for your reply!

The Query:
Query.png

The Entity:
ToDoTask.png

The DocumentStoreHolder (with the Exception):
DocumentStoreHolder.png

Nik

unread,
Apr 1, 2021, 5:20:58 AM4/1/21
to RavenDB - an awesome database
The DocumentQuery is mutable and AndAlso() probably does not return the query object itself so you need to do it this way:

qc.AndAlso();
qc.WhereEquals(...)


Btw, I just realized this is a recent discussion. I posted a related observation/question:

Arão Benjamin Garcea

unread,
Apr 1, 2021, 11:49:11 AM4/1/21
to RavenDB - an awesome database
Hi Nik,

Yes, it worked here!
Which is strange 'cause I thought that AndAlso() would return this, as in the Multi-tenant example on your other post.

But it's already a great progress, thank you very much.

Nik

unread,
Apr 1, 2021, 11:53:56 AM4/1/21
to RavenDB - an awesome database
The soft-delete example in my post was actually copied from here. The multi-tenant one works.

Anyway, keep an eye on the other post as well as the current solution you are using might produce unexpected results in case you have an "OR" comparison operator in your query.

Arão Benjamin Garcea

unread,
Apr 1, 2021, 12:11:07 PM4/1/21
to RavenDB - an awesome database
I saw the soft-deleted example using switch, but I wanted a less verbose way of doing it.

Your other post is indeed crucial, it's a really big unexpected result, and I'm now researching a little on it.
Reply all
Reply to author
Forward
0 new messages