Note that there are some explanatory texts on larger screens.

plurals
  1. POArea of responsibilities between Controller and Repository in ASP.Net MVC2
    primarykey
    data
    text
    <p>I'm trying to get an understanding of how to structure an ASP.Net MVC2 Web App using repositories.</p> <p>A lot of examples, tutorials and books I have read structure the App like so:</p> <pre><code>public interface IProductsRepository { IQueryable&lt;Product&gt; Products { get; } } public class SqlProductsRepository : IProductsRepository { public Table&lt;Product&gt; productsTable; public IQueryable&lt;Product&gt; Products { get { return productsTable } } } public class ProductsController : Controller { IProductsRepository productsRepository; public ProductsController(IProductsRepository productsRepository) { // set the Repository } public ActionResult GetLatestPublishedProducts(int pageSize, int page, ref int totalItemCount) { // Do LINQ query return View(from p in productsRepository where p.PublishedOn != null orderby p.ProductId descending select to(p)) .Skip((page - 1) * pageSize) .Take(pageSize) .ToList()); } } </code></pre> <p>One thing I don't understand is why the Linq query lives in the controller.</p> <p>I'd like to know why doing something like this is not correct:</p> <pre><code>public class SqlProductsRepository : IProductsRepository { // set data model stuff here public List&lt;Product&gt; GetLatestPublishedProducts(int pageSize, int page, ref int totalItemCount) { // DO LINQ Query in here } } public class ProductsController : Controller { // set repository stuff here public ActionResult GetLatestPublishedProducts(int pageSize, int page, ref int totalItemCount) { return View(productsRepository.GetLatestPublishedProducts(pageSize, page, totalItemCount)); } } </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. 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