在 MVC 架构中实现服务层 [英] Implementing a service layer in an MVC architecture

查看:22
本文介绍了在 MVC 架构中实现服务层的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

人们通常如何在 MVC 架构中实现服务层?它是一个为底层业务对象提供所有请求的对象吗?或者更像是一个为不同服务对象提供服务的对象,这些服务对象又与业务对象交互?

How would one typically implement a service layer in an MVC architecture? Is it one object that serves all requests to underlying business objects? Or is more like an object that serves different service objects that in their turn interact with business objects?

所以:

  1. 控制器 -> 服务 -> getUserById(),或:

  1. Controller -> Service -> getUserById(), or:

控制器 -> ServiceManager -> getUserService() -> getUserById()

Controller -> ServiceManager -> getUserService() -> getUserById()

另外,如果后者更合适,你会在引导程序中配置这个 ServiceManager 对象吗?换句话说,在引导程序中将应用所需的不同服务注册到服务管理器?

Also, if the latter is more appropriate, would you configure this ServiceManager object in a bootstrap? In other words, register the different services that you will be needing for your app to the service manager in a bootstrap?

如果以上都不合适,有什么可以帮助我更好地理解应该如何实现服务层?

If none of the above is appropriate, what would help me get a better understanding of how a service layer should be implemented?

提前致谢.

推荐答案

我读这个问题的方式,真的有两件事应该回答:

The way I read this question, there's really two things that should be answered:

A) 我更愿意将Service"拆分为CustomerService"和OrderService",换句话说,按领域概念分组.

A) I would prefer splitting "Service" into "CustomerService" and "OrderService", in other words grouped by domain concepts.

B) 其次,我会使用依赖注入直接在我需要的地方获得正确的服务,所以我基本上使用 alt 1.替代方案 2 中添加的抽象对我没有提供额外的价值,因为 IoC 容器确实如此重要的部分.

B) Secondly I'd use dependency injection to get the proper service directly where I need it, so I'm basically using alt 1. The added abstraction in alternative 2 provides no additional value for me, since the IoC container does the important part.

这篇关于在 MVC 架构中实现服务层的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆