Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy don't use repository in the view
    text
    copied!<p>I have a partial view that loops through its Model (a list of things) to show the thing.Name and three integer values that are counts of related entities.</p> <p>First of all, I tried putting: (pseudo-razor)</p> <pre><code>foreach(thing in Model){ @thing.Name : @thing.related.entities.where(condition1).Count() @thing.related.entities.where(condition2).Count() @thing.related.entities.where(condition3).Count() } </code></pre> <p>But its really slow... so I created a function in the ThingRepository that does same queries faster, something like this (pseudo-code)</p> <pre><code>function GetCountofRelatedEntities(relatedID,condition){ return db.entities.where(relatedID==relatedID &amp;&amp; condition).count() } </code></pre> <p>and its much faster, so I want to call it. I think I should call it from the controller, but then I need a ViewModel to keep a (thing, int, int, int) collection as the model, or I can use the ViewBag extremely to pass the results to the view, but, and here is the question: Why not simply use the repository from the view? whats wrong with this code in the view? (pseudo-razor)</p> <pre><code>@repo=new ThingRepository() foreach(thing in Model){ @thing.Name : @repo.GetCountofRelatedEntities(thing.relatedID,condition1) @repo.GetCountofRelatedEntities(thing.relatedID,condition1) @repo.GetCountofRelatedEntities(thing.relatedID,condition1) } </code></pre> <p>Could you tell me why I shouldn't instantiate the repository inside a View? Or I can do it?</p>
 

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