Reputation: 2282
I've been using MVC frameworks for a short while now and I really like how the concerns are separated out. I've got into a bad habit of letting the controllers do quite a bit of work. So I'm really looking for some advice.
When I first started using MVC I quite often had the controller doing manipulation on the models after database work had been done. I knew this was bad so moved that work into the models. However I'm not happy with that as I want my models to be very learn.
I've done a bit of reading and I see that people are keeping their controllers and models lean by having a service layer, which I like the look of.
I'm just trying to understand how a service layer and repository should all work together. Here are my assumptions, can you please let me know if this is a good way of working?
I'm trying to turn all my code around to the single responsibility principle, hence trying to sort out my coding practices.
Thanks
Upvotes: 48
Views: 8561
Reputation: 16195
This video gives great insight into how to organize your asp.net MVC solution and addressing separation of concerns, and better testability. Hopefully it will help someone else also. I learned some good stuff from it.
Upvotes: 7
Reputation: 16419
Ian Cooper has just written a blog post called The Fat Controller on just this subject.
Upvotes: 6
Reputation: 12656
First, there is no set of rules that's going to work in every situation. How you model you're application depends a lot on the type and complexity of the project. Having said that, here are some ideas:
Most important, do what feels right (that's usually the right answer).
Upvotes: 26