I want to use Repository Pattern (I know that I can use Unit of Work Pattern but I would like to use Repository Pattern here). So in each repository class I have queries for a specific table, for example:
public class NotesRepository : INotesRepository, IDisposable
{
private DatabaseContext context;
public NotesRepository(DatabaseContext context)
{
this.context = context;
}
public IQueryable<Notes> GetAllNotes()
{
return (from x in context.Notes
select x);
}
}
And another repository example:
public class CommentsRepository : ICommentsRepository, IDisposable
{
private DatabaseContext context;
public CommentsRepository(DatabaseContext context)
{
this.context = context;
}
public IQueryable<Comments> GetAllComments()
{
return (from x in context.Comments
select x);
}
}
Now I would like to use these two repositories in one controller. So with the Repository Pattern I initialize the database connection in the controller constructor and pass it to each repository, am I right? For example:
public class HomeController : Controller
{
private INotesRepository NotesRepository;
private ICommentsRepository CommentsRepository;
public HomeController()
{
DatabaseContext db = new DatabaseContext();
this.NotesRepository = new NotesRepository(db);
this.CommentsRepository = new CommentsRepository(db);
}
// ........
}
I think you should take this at least one step further, by having the Controller take the repositories as dependency in the constructor. This means you would be creating the Controller and Repository instances in a custom ControllerFactory (used in ASP.NET MVC and MVC2) or a DependencyResolver (introduced in ASP.NET MVC3 IIRC; which is a good fit if moving to a IoC Container). The DatabaseContext is normally created per HttpRequest in a web application, in order to maintain the same UnitOfWork during the request. If you're using a IoC container, this is easily done, since support for this is built into all major containers. If you're doing it manually, you have to wire up some mechanisms for creating and disposing the DatabaseContext in the Application.BeginRequest and EndRequest events.
For simplicity I'll give you a ASP.NET MVC example without the use of a container and having one DatabaseContext instance per Controller. Also the DatabaseContext is not disposed when the Controller is "released".
public class HomeController : Controller
{
private INotesRepository notesRepository;
private ICommentsRepository commentsRepository;
public HomeController(INotesRepository notesRepostory,
ICommentsRepository commentsRepository)
{
this.notesRepository = notesRepository;
this.commentsRepository = commentsRepository;
}
}
public class MyControllerFactory : DefaultControllerFactory
{
protected override IController GetControllerInstance(
RequestContext requestContext, Type controllerType)
{
if (controllerType == null)
return base.GetControllerInstance(requestContext, controllerType);
DatabaseContext db = new DatabaseContext();
//Here you should have logic to determine which
//type of controller to create
return new HomeController(new NotesRepository(db),
new CommentsRepository(db));
}
}
In the Global.asax Application.Start event handler:
ControllerBuilder.Current.SetControllerFactory(new MyControllerFactory());
There are lots of examples of IoC with MVC and your favorite brand of Container if you go on the container route.
The advantage of using Inversion of Control is that the application becomes loosely coupled and easier to change and maintain.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With