Reputation:
I am trying to learn how to use Dependency Injection on an auto-scaffolded controller in an MVC 5 Web application. While I have found tutorials on "Dependency Injection" and "Unity", the examples I have seen are more of the hello world variety and never actually get around to dealing with database contexts in controllers.
To give you a better example of what I mean, here is a code snippet of what I have and what I am trying to do.
When I use Visual Studio to auto-scaffold a controller "with views, using Entity Framework", this is typically what is created.
public class TideUploadsController : Controller
{
// This is the EF Database context placed by the auto-scaffolder
private AzMeritContext db = new AzMeritContext();
[HttpGet]
public ActionResult Index()
{
return View(db.TideUploads.ToList());
}
/* further CRUD ActionResults not shown */
}
}
From what I read so far, to use Dependency Injection, I should use a constructor that calls an interface instead of a class. I have read mixed reviews about wrapping a repository around Entity Framework. I would like to see if I could do this without use of a repository.
Is there a pre-existing interface I should use in place of the DbContext that is placed in the code by the auto-scaffolder?
// This is the EF Database context placed by the auto-scaffolder
private AzMeritContext db = new AzMeritContext();
public TideUploadsController(/* what Interface goes here? */)
{
//db = ?
}
Or is this a situation where I really need to wrap a repository around the Entity Framework dbContext?
I have spent the last few weeks reading books and searching through tutorials and videos, and I have not yet seen a step-by-step example that shows something like:
Upvotes: 0
Views: 1075
Reputation: 16066
Well, for me one of the benefits behind dependency injection is that you basically are decoupling your "dependencies" into mockable objects or other implementations, IMHO repository pattern is the common way to go, I don't see any other alternative of having to use a wrapper and configure you interface and your implementation in your container.
I guess you are familiar with the concept:
public class UserRepository:IUserRepository
{
public UserRepository(){
// you usually instanciate your context here
// private AzMeritContext db = new AzMeritContext();
}
User GetUserById(int Id){
// do your query to get a single user
}
}
this implementation looks ok to me.
then your controller should look like this:
public class TideUploadsController : Controller
{
public TideUploadsController(IUserRepository userRepository){
// constructor injection
// assign your user repository to a local variable outside of the constructor scope something like _userRepository
}
[HttpGet]
public ActionResult Index()
{
return View(_userRepository.GetUserById(1));
// let's assume your variable's name is _userRepository
}
/* further CRUD ActionResults not shown */
}
}
just remember that your interface should implement the methods you would like to use and expose in your controllers, services, etc.
my two cents, I hope that helps.
Upvotes: 1