Title: MVC3 accessing Repository in Controllers
Tags: c#;asp.net-mvc;repository
Question: Thanks for the help!  My solution below has been updated to the correct answer.
I have a simple question on how to access the repository for my controller.  If you look at my addMovie method, I thought I could do movies.AddMovie(movie) inside the ModelState.IsValid after the controller inherits the IMovieRepository.
IMovieRepository:
public interface IMovieRepository : IDisposable
IEnumerable<Movies&gt; GetMovies();
Movies GetMovieByID(int id);
void AddMovie(Movies movie);
void UpdateMovie(Movies movie);
void DeleteMovie(int id);
void save(Movies movie);
MovieRepository:
public class MovieRepository : BaseRepository, IMovieRepository
private MovieContext db;
public MovieRepository(MovieContext repo)
db = repo;
public IEnumerable<Movies&gt; GetMovies()
return db.Movies.ToList();
public Movies GetMovieByID(int id)
return db.Movies.Find(id);
public void AddMovie(Movies movie)
db.Movies.Add(movie);
public void UpdateMovie(Movies movie)
db.Entry(movie).State = EntityState.Modified;
public void DeleteMovie(int id)
Movies movie = db.Movies.Find(id);
db.Movies.Remove(movie);
public void save(Movies movie)
if (movie.movieID == 0)
db.Movies.Add(movie);
else
db.Entry<Movies&gt;(movie).State = System.Data.EntityState.Modified;
db.SaveChanges();
NinjectDependencyResolve:
Bind<IMovieRepository&gt;().To<MovieRepository&gt;();
MoviesController:
public class MoviesController : Controller
private IMovieRepository movieRepository;
public MoviesController()
this.movieRepository = new MovieRepository(new MovieContext());
public MoviesController(IMovieRepository movieRepository)
this.movieRepository = movieRepository;
#region "GET"
//
// GET: /Admin/Movies/
public ActionResult Index()
return View();
#endregion
#region "POST"
[HttpPost]
public ActionResult addMovie(Movies movie)
if (ModelState.IsValid)
{
movieRepository.AddMovie(movie);
movieRepository.save(movie);
return RedirectToAction("index", "movies");
}
return View(movie);
#endregion
Comment: You should not change your question to your solution.  That does not allow others to benefit from the question/answer.  If you want to include the answer, then add it to the end of your question.

Here is the accepted answer: You'll need to add a constructor to the controller:
private IMovieRepository _Repository;
[Inject]
public MovieController(IMovieRepository repository)
_Repository = repository;
Comment for this answer: Since you're using Ninject, you need the attribute so Ninject knows where the dependency injection occurs. Otherwise, you'll get an error saying you need a parameterless constructor.
Comment for this answer: Thanks, that fixed it!  What does the [Inject] do?

Here is another answer: private IMovieRepository _repository;
public MoviesController(IMovieRepository repository)
_repository =repository;
use constructor injection

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 1, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2013:00:03/17/13', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<c#><asp.net-mvc><repository>', 'Title': 'MVC3 accessing Repository in Controllers', 'ViewCount': 668, 'id': 15464501, 'user_AboutMe': None, 'user_AccountId': 2407819.0, 'user_DisplayName': 'Andrew', 'user_Reputation': 1971.0, 'user_Views': 156.0, 'user_id': 2103759.0}}