Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In my MVC apps I'm using architecture that Rob Conery showed on his MVC Storefront video series and it works like charm for me.</p> <p>Repository => Service + Filters => Controller => View</p> <p>I've tried to simulate what you want to achieve and managed todo like this</p> <p><strong>Edit1</strong>: Changed IList to IQueryable in repository and filters</p> <p>Repository</p> <pre><code>public interface IRepository { IQueryable&lt;Vote&gt; GetVotes(); IQueryable&lt;Story&gt; GetStories(); } </code></pre> <p>Service for getting what you want </p> <pre><code>public class Service : IService { private IRepository _repository; public Service(IRepository repository) { _repository = repository; if (_repository == null) throw new InvalidOperationException("Repository cannot be null"); } public IList&lt;Vote&gt; GetUserVotes(int userID) { return _repository.GetVotes().WithUserID(userID).ToList(); } public IList&lt;Story&gt; GetNotVotedStories(IList&lt;Vote&gt; votes) { return _repository.GetStories().WithoutVotes(votes).ToList(); } } </code></pre> <p>Filters to filter your stories and user votes (These are basically extension methods). Not the nicest implementation out there, but you can rewrite later</p> <pre><code>public static class Filters { public static IQueryable&lt;Vote&gt; WithUserID(this IQueryable &lt;Vote&gt; qry, int userID) { return from c in qry where c.UserID == userID select c; } public static IQueryable&lt;Story&gt; WithoutVotes(this IQueryable &lt;Story&gt; qry, IList &lt;Vote&gt; votes) { return from c in qry where votes.Where(x =&gt; x.StoryID == c.StoryID).ToList().Count &gt; 0 select c; } } </code></pre> <p>And then you can pass current UserID in controller, not in DAL or View like you had to do before</p> <pre><code>public class HomeController : Controller { private readonly IRepository _repository; private readonly IService _service; public HomeController() { _repository = new Repository(); _service = new Service.Service(_repository); } public ActionResult Index() { var userVotes = _service.GetUserVotes(CurrentUserID); var unvotedStories = _service.GetNotVotedStories(userVotes); return View(unvotedStories); } } </code></pre> <p>This allows you to stay away from adding user related UserVotedOn property to your Story model</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.
    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