分层架构中的公共服务
我在很多书中读到,在分层架构中,一层应该只使用其下面的层提供的服务。企业应用程序中常用的层有:
- 表示层
- 业务
- 持久层
这意味着业务层的服务(包含业务逻辑)应该只访问持久层提供的服务。
我有一个向用户发送消息的 MessageService。每当对象的状态发生重大变化时,必须向所有关联的用户通知该变化。这意味着识别变化的业务层Service必须使用MessageService来发送消息。但 messageService 本身位于业务层,因此同一层的其他服务不应访问它。
那么我们如何在不违反代码架构的情况下使用MessageService呢?
I read in many books that in a layered architecture a layer should only use the services provided by the layers below it. The commonly used layers are in an enterprise application are:
- Presentation
- Business
- Persistence
This means services at business layer (that contain business logic) should access only access the services provided by the persistence layer.
I have a MessageService that sends messages to the users. Whenever there is a significant change in the state of an object, all the associated users must be notified about the change. This means that the Service at business layer that identified the change must send use MessageService to send messages. But messageService is itself at the business layer, hence no other service from the same layer should access it.
So how can we use MessageService without violating the architecture of the code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
表示层(或顶层)不仅仅意味着 UI,它可以是任何消耗系统中服务的东西。即,计划作业可以位于顶层(可能您不想将其命名为表示层)。在您的情况下,我认为 MessageService 应该位于顶层,因为它消耗系统中的其他服务。例如,如果您编写一个 Web 服务,它应该位于服务层之上,但您可能希望以不同的方式命名。
Presentation Layer(or the Top Layer) does not mean only UI, it can be anything that consumes the services in the system. ie, a Scheduled Job can be at the top layer(may be you don't want to name it as presentation layer).In your case I feel MessageService should be at top level as it consumes other services in the system. For example if you write a web service it should be above the service layer but you may be want it to be named differently.