stephen776
stephen776

Reputation: 9234

ASP.NET MVC Controller with multiple repositories?

I have a View where I am searching my db for an object(i.e. Books)..

My controller for this view depends on a BooksRepository that implements a search method.

Everything works fine. I also have the option to do an advanced search which presents a larger form in a Modal popup. This form has many fields including a Dropdown box to select an 'Author' to search by.

I would like to pass a list of authors in my viewmodel so in my conroller I instantiate an instance of my view model, I need to call a repository method to bring back the list of authors's...

My thinking is that this GetAuthors() method should be in an AuthorRepository...

Is it bad practice to inject multiple repo's into a controller? or should I have an Author controller that gets injected with the author repo...and call a method in the Author controller from my BookSearch controller?

Upvotes: 2

Views: 4326

Answers (5)

BZink
BZink

Reputation: 7947

I have a few controllers that reference more than one repository. Just be careful if each of your repositories instantiate their own data context (or EF ObjectContext). Speaking in terms of Entity Framework, if you start navigating entity references with two open contexts you'll have problems.

Other than that, it works fine for me.

Upvotes: 1

Reactor
Reactor

Reputation: 99

From an architect point of view.

If you feel your mvc controllers are getting out of hand with dependencies, then its time to think about 2 things.

  1. have a look at the design and determine if you need facade classes to represent complex subsystems, besides its better for unit testing anyway (there are such thing as 4 tier apps)

  2. look at some of the other design patterns that can help solve this issue before it becomes it becomes a problem (strategy with DI, visitor possibly)

Also, I bet in this situation the unit tests are more of a pain, if you can't unit test it in a simple way, it should be flagged for improvement

Good luck,

Upvotes: 0

Mikael Östberg
Mikael Östberg

Reputation: 17146

I don't think it's a bad idea to inject the two repositories you need into the controller. Actually it sounds like a good practice.

But if you feel things are going out of hands, you might want to create an Application Service that would orchestrate a function in which you could inject several repositories. That would also be a way to move logic away from the controller.

But in this case, I think you are doing it right.

Read this book: http://www.infoq.com/minibooks/domain-driven-design-quickly

Upvotes: 4

cwharris
cwharris

Reputation: 18125

Personally, I would think that books and authors are pretty specific entities.... unless you're planning on having an author write a song as well, and you want to have a music repository and a book repository, I would probably keep the authors and books in the same repository, as you're more than likely going to need them both at the same time.

Even then, you could have a music repository and a book repository that both pull from the same author table. There's nothing wrong with that. And no, having more than one repository in a controller is not a "no-no", but unless you're using dependency injection, it can start to get hairy as you add more repositories.

Upvotes: 2

tobias86
tobias86

Reputation: 5029

I think it's perfectly fine to refer to multiple repositories in a controller. A controller's job is to wrap data in a model and pass it to a view, regardless of how it gets to the data. Doing cross-controller calls can get messy.

Upvotes: 11

Related Questions