Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There is one thing that I try to do when exposing IQueryable... make sure you do not expose your EF style objects, always make sure you have a view model of some sort sitting over the top that you can control.</p> <p>Just as an example, say your DB has User and UserSecrets</p> <pre><code>public class User { public long UserId { get; set; } public string Name { get; set; } public virtual ICollection&lt;UserSecret&gt; UserSecrets { get; set; } } public class UserSecret { public long UserSecretId { get; set; } public long UserId { get; set; } public string Secret { get; set; } } </code></pre> <p>If you expose <code>IQueryable&lt;User&gt;</code> you can easily extract the UserSecrets as well</p> <pre><code>www.blah.com/users?$expand=UserSecrets </code></pre> <p>Instead expose a <code>UserViewModel</code> or something similar</p> <pre><code>public class UserViewModel { public string Name { get; set; } } </code></pre> <p>You can expose <code>IQueryable&lt;UserViewModel&gt;</code> in the following way:</p> <pre><code>return dbContext.Users.Select(u =&gt; new UserViewModel { Name = u.Name }) </code></pre> <p>The great thing is that this is still <code>IQueryable</code> - you can still filter etc and it will still execute at the db level, but you have control over exactly what data can be pulled (in this case <code>UserSecret</code> is no longer accessible).</p> <p>Of course you can also apply your own filters so you can avoid users not getting access to data they are not allowed too:</p> <pre><code>return dbContext.Users.Where(u =&gt; ...).Select(u =&gt; new UserViewModel { Name = u.Name }) </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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