Note that there are some explanatory texts on larger screens.

plurals
  1. PONavigation Property Filter
    primarykey
    data
    text
    <p>My question is this: How can you implement a default server-side "filter" for a <a href="http://www.breezejs.com/documentation/navigation-properties" rel="nofollow noreferrer">navigation property</a>?</p> <p>In our application we seldom actually delete anything from the database. Instead, we implement "soft deletes" where each table has a <code>Deleted</code> bit column. If this column is true the record has been "deleted". If it is false, it has not. </p> <p>This allows us to easily "undelete" records accidentally deleted by the client.</p> <p>Our current ASP.NET Web API returns only "undeleted" records by default, unless a <code>deleted</code> argument is sent as <code>true</code> from the client. The idea is that the consumer of the service doesn't have to worry about specifying that they only want undeleted items. </p> <p>Implementing this same functionality in Breeze is quite simple, at least for base entities. For example, here would be the implementation of the classic Todo's example, adding a "Deleted" bit field:</p> <pre><code> // Note: Will show only undeleted items by default unless you explicitly pass deleted = true. [HttpGet] public IQueryable&lt;BreezeSampleTodoItem&gt; Todos(bool deleted = false) { return _contextProvider.Context.Todos.Where(td =&gt; td.Deleted == deleted); } </code></pre> <p>On the client, all we need to do is...</p> <pre><code>var query = breeze.EntityQuery.from("Todos"); </code></pre> <p>...to get all undeleted Todos, or... </p> <pre><code>var query = breeze.EntityQuery.from("Todos").withParameters({deleted: true}) </code></pre> <p>...to get all deleted Todos.</p> <p>But let's say that a BreezeSampleTodoItem has a child collection for the tools that are needed to complete that Todo. We'll call this "Tools". Tools also implements soft deletes. When we perform a query that uses <code>expand</code> to get a Todo with its Tools, it will return all Tools - "deleted" or not.</p> <p>But how can I filter out these records by default when <code>Todo.Tools</code> is expanded?</p> <p>It has occurred to me to have separate Web API methods for each item that may need expanded, for example:</p> <pre><code>[HttpGet] public IQueryable&lt;Todo&gt; TodoAndTools(bool deletedTodos = false, bool deletedTools = false) { return // ...Code to get filtered Todos with filtered Tools } </code></pre> <p>I found some example code of how to do this in <a href="https://stackoverflow.com/questions/11228463/load-navigation-properties-with-filter-for-entity-framework-4-3">another SO post</a>, but it requires hand-coding each property of Todo. The code from the above-mentioned post also returns a <code>List</code>, not an <code>IQueryable</code>. Furthermore this requires methods to be added for every possible expansion which isn't cool.</p> <p>Essentially what I'm looking for is some way to define a piece of code that gets called whenever <code>Todos</code> is queried, and another for whenever <code>Tools</code> is queried - preferably being able to pass an argument that defines if it should return Deleted items. This could be anywhere on the server-side stack - be it in the Web API method, itself, or maybe part of Entity Framework (note that filtering Include extensions is <a href="http://data.uservoice.com/forums/72025-entity-framework-feature-suggestions/suggestions/1015345-allow-filtering-for-include-extension-method" rel="nofollow noreferrer">not supported in EF</a>.)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload