Note that there are some explanatory texts on larger screens.

plurals
  1. PODatabase abstraction layer design - Using IRepository the right way?
    text
    copied!<p>I'm in the process of designing my ASP.NET MVC application and I ran across a couple of interesting thoughts.</p> <p>Many samples I have seen describe and use the Repository pattern (<code>IRepository</code>) so this is the way I did it while I was learning MVC.</p> <p>Now I know what it's all doing, I starting to look at my current design and wonder if it's the best way to go.</p> <p>Currently I have a basic <code>IUserRepository</code>, which defines methods such as <code>FindById()</code>, <code>SaveChanges()</code>, etc.</p> <p>Currently, whenever I want to load/query the user table in the DB, I do something along the lines of the following:</p> <pre><code> private IUserRepository Repository; public UserController() : this(new UserRepository()) { } [RequiresAuthentication] [AcceptVerbs(HttpVerbs.Get)] public ActionResult Edit(string ReturnUrl, string FirstRun) { var user = Repository.FindById(User.Identity.Name); var viewModel = Mapper.Map&lt;User, UserEditViewModel&gt;(user); viewModel.FirstRun = FirstRun == "1" ? true : false; return View("Edit", viewModel); } [AcceptVerbs(HttpVerbs.Post), ValidateAntiForgeryToken(Salt = "SaltAndPepper")] public ActionResult Edit(UserEditViewModel viewModel, string ReturnUrl) { //Map the ViewModel to the Model var user = Repository.FindById(User.Identity.Name); //Map changes to the user Mapper.Map&lt;UserEditViewModel, User&gt;(viewModel, user); //Save the DB changes Repository.SaveChanges(); if (!string.IsNullOrEmpty(ReturnUrl)) return Redirect(ReturnUrl); else return RedirectToAction("Index", "User"); } </code></pre> <p>Now I don't fully understand how MVC works in regards to creating a controller when a user creates a link (not sure if there is 1 controller per user or 1 controller per application), so I'm not positive of the best course of action.</p> <p>I found a great question regarding the use of a generic repository interface <code>IRepository&lt;T&gt;</code> <a href="https://stackoverflow.com/questions/1853414/generic-repository-irepositoryt-or-irepository">here</a> and have also seem the idea of a static <code>RepositoryFactory</code> on a number of blogs. Basically only 1 instance of the repository is kept ever and it is obtained via this factory</p> <p>So my question revolves around how people do it in there apps, and whats considered good practice.</p> <p>Do people have individual repositorys based on each table (<code>IUserRepository</code>)?<br> Do they use a generic <code>IRepository&lt;T&gt;</code>?<br> Do they use a static repository factory?<br> Or something else completely?</p> <p>EDIT: I just realised I should probably ask as well:</p> <p>Is having a private <code>IRepository</code> on each controller a good way to go? or should I instantiate a new <code>IRepository</code> every time I want to use it?</p> <p>BOUNTY EDIT: I'm starting a bounty to get some more perspectives (not that Tim's wasn't helpful).</p> <p>I'm more curious to know what people do in their MVC apps or what they think is a good idea.</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