你能构建一个 RESTful 业务逻辑层吗? [英] Can you build a RESTful Business Logic Layer?

查看:27
本文介绍了你能构建一个 RESTful 业务逻辑层吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为我的架构的数据访问层 (DAL) 构建了一个 RESTful 服务:

I've built a RESTful service for the Data Access Layer (DAL) of my architecture:

POST http://example.com/data/User
GET|PUT|DELETE http://example.com/data/User/{UserId}

但是,对于业务逻辑层 (BLL),使用了第二个非 RESTful 服务:

However, for the Business Logic Layer (BLL), a second non-RESTful service is used:

POST http://example.com/accountapi/register
POST http://example.com/accountapi/login

此 BLL 服务不调用 DAL 服务,而是直接与数据库对话.

This BLL service, instead of making calls to the DAL service, it talks directly to the database.

您将如何改进此架构?

  1. BLL 服务是否应该调用 DAL 服务?
  2. 我应该放弃 DAL 服务而只公开 BLL 服务吗?
  3. 我是否应该以某种方式在我的 RESTful DAL 服务中注入业务逻辑?如果是,如何?
  1. Should the BLL service call the DAL service ?
  2. Should I drop the DAL service and only expose the BLL service ?
  3. Should I, somehow, inject business logic on my RESTful DAL service ? If yes, how ?

推荐答案

(1) 避免让您的(非 REST)Web 服务业务逻辑层向数据访问层发出进一步的(RESTful)HTTP 请求.这样做当然比直接调用方法效率低.但更重要的是,它需要您将 BLL Web 服务和 DAL Web 服务部署到单独的 Web 服务器实例(或单独的集群)上.否则,您可能会遇到这样的情况:所有您的 HTTP 工作线程正忙于尝试提供 BLL 响应,并且每个线程都被阻塞,等待一个空闲的 HTTP 工作线程为它提供 DAL 响应而徒劳无功.这可能会导致停顿(如果您执行超时/重试处理)或彻底死锁(如果您不这样做).

(1) Avoid having your (non-REST) web service business logic layer make further (RESTful) HTTP requests onto the data access layer. Doing so of course would be less efficient than making direct method calls. But much more importantly, it would require you to deploy the BLL web services and the DAL web services onto separate web server instances (or separate clusters). Otherwise you can have a case where all your HTTP worker threads are busy trying to serve up BLL responses, and each is blocked waiting fruitlessly for a free HTTP worker thread to serve it a DAL response. This can lead to stalls (if you do timeout/retry processing) or outright deadlocks (if you don't).

(2 和 3)如果您的 Web 服务客户端需要业务逻辑和数据访问,请将它们作为统一的服务集提供.在内部,它们都依赖于相同的数据访问层方法调用:只是面向数据的 Web 服务实现只进行一次数据访问层调用,而面向业务逻辑的 Web 服务实现可能会进行多次 DAL 调用.不过,您确实希望在 Web 服务层下方分别构建 BLL 和 DAL 层.

(2 and 3) If your web service clients need both business logic and data access, provide those as a unified set of services. Internally they both depend on the same data access layer method calls: it's just that the data oriented web service implementations make just one data access layer call while the business logic oriented web service implementations may make many DAL calls. You do most definitely want to structure the BLL and DAL layers separately beneath the web service layer though.

我喜欢将 Web 服务视为面向恰好是其他程序的用户"的表示层的一部分.

I like to think of the web services as just the part of the presentation layer oriented towards "users" who happen to be other programs.

这篇关于你能构建一个 RESTful 业务逻辑层吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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