开发者

Is it Okay to consume repositories and services from a same controller?

I use repositories to perform basic CRUD operations and I use services to implement business logic with t开发者_如何学JAVAhese repositories.

I tend to use repositories and services at the same time within controllers. My question is -is this Okay in terms of a standard architecture? Should I move my basic repositories to basic services, so my controllers only consume services, not repositories? Hope this make sense, thank you.


I'd recommend you to put your Repositories behind the Services, so the Controllers would only consume Services. That way you:

  • Maintain nice layered architecture (Onion Architecture pattern);
  • Keep your Controllers Actions clean and clear.

Actually, nothing stops you from injecting Repos along with Services into your Controllers but it just feels wrong - what is the point of Services existence then?


It's OK as long as you don't put any biz logic in the controller. There's no point in creating a new service class that wraps the repository if it doesn't add any logic/behavior.

I tend to use repositories and services at the same time within controllers.

Think about the controller/action functionality and it's dependencies, if you are using repositories and services then you should probably create a new service class that wraps the functionality, and make the controller/action depend on that single service. Actions should ideally depend on one service only.

I've had controllers call repositories directly when all I need is CRUD (e.g. admin app).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜