简体   繁体   English

具有可选参数和不同业务逻辑的Web服务

[英]Webservice with optional parameter and different business logic

Question: What is the best way to design a webservice that has optional parameters and different business logic for certain parameters? 问题:设计具有可选参数并且某些参数具有不同业务逻辑的Web服务的最佳方法是什么?


Specific example: 具体的例子:

The web service is intended to make it possible to search for houses. Web服务旨在使搜索房屋成为可能。

A house resource has a structure like this: 房屋资源具有以下结构:

{
    "houseId": 3123,
    "street": "Drake Street",
    "houseNo": "789",
    "city": "London",
    "zipCode: "EC2R 6AB",
    "country": "uk",
    "coordinates": "51.506885, -0.128107",
    "inventory": ["Veranda", "Baloncy", "Stove"]
}

The web service should perform a proximity search for a given address or coordinate and return houses that meet the given criteria. Web服务应该对给定的地址执行邻近搜索,或者协调并返回符合给定条件的房屋。

Valid example: 有效的例子:

/rest/v1/houses?city=London&street=Drake Street&inventory=Balcony&inventory=Veranda

Now it could also be possible to send requests as follows: 现在也可以如下发送请求:

/rest/v1/houses?city=London&street=Drake Street&coordinates=51.506885, -0.128107

In this case, I would have to decide which geographical information has priority. 在这种情况下,我将不得不确定哪个地理信息具有优先权。 Coordinates or Street+City+ZipCode. 坐标或街道+城市+邮政编码。

In order to make things even more complicated, it should also be possible to pass the radius and the number of search results to the webservice. 为了使事情变得更加复杂,还应该可以将半径和搜索结果的数量传递给Web服务。 For performance reasons it should not be possible to get an unlimited number of search results if the country is missing. 出于性能原因,如果缺少该国家/地区,则不可能获得无限数量的搜索结果。

Furthermore, it should not be possible to get an unlimited number of search results for an infinite radius. 此外,对于无限半径,应该不可能获得无限数量的搜索结果。 Therefore, one of the two values must be set to a finite number to trigger a search. 因此,必须将两个值之一设置为有限数字才能触发搜索。

And so on... further logic... 依此类推...进一步的逻辑...


Concluding: 结论:

It's not important to identify a particular house (resource) by id or anything like this. 通过id或类似的东西识别特定的房屋(资源)并不重要。 It is important to filter the existing houses by criteria. 重要的是要按标准过滤现有房屋。

When filtering, however, it must be ensured that there is different business logic for certain parameters. 但是,在过滤时,必须确保某些参数具有不同的业务逻辑。

How can I map such a problem elegantly in the backend? 如何在后端优雅地映射这样的问题? Are there any patterns that support such an approach? 是否有任何模式支持这种方法?

Filtering can be a difficult thing to get right, so there is no right or wrong answer here. 过滤可能很难正确,因此这里没有正确或错误的答案。 Here's how I would approach this though. 不过,这就是我的处理方法。

Firstly, let's say you have a RESTful URL for; 首先,假设您有一个RESTful URL;

/rest/v1/search-houses/

Which can take in multiple parameters, one or more. 可以接受多个参数,一个或多个。

Based on this, you can then see what parameters you need to filter on. 基于此,然后可以查看需要过滤哪些参数。 Here is where your business logic lies and this very much depends on the structure of your code base, database and more. 这是您的业务逻辑所在,这在很大程度上取决于代码库,数据库等的结构。

On the design pattern front, MVC in my opinion solves a lot of problems. 在设计模式方面,我认为MVC解决了许多问题。 In this instance, if you are always filtering data from a single table with basic lookups, ie where town = London or country = UK , then this is easy. 在这种情况下,如果您始终使用基本查找来过滤单个表中的数据,即where town = London or country = UK ,那么这很容易。 Simply use the parameters that have been entered to build a String that will be there where clause part. 只需使用输入的参数来构建将在where子句部分存在的String Make sure to do this safely though using prepared statements . 确保使用prepared statements安全地执行此操作。

If you are looking up in multiple tables or different places depending on what information has been entered, this gets more tricky. 如果根据输入的信息在多个表或不同位置中查找,这将变得更加棘手。 As such, in this instance I'd probably look to create multiple Model files that follow the same approach as outlined above but are tailored towards the specific database query. 因此,在这种情况下,我可能希望创建多个模型文件,这些文件遵循与上述相同的方法,但针对特定的数据库查询量身定制。

Hope that helps you get going in the right direction. 希望能帮助您朝正确的方向前进。 There is no single answer to this question unfortunately. 不幸的是,这个问题没有一个答案。 As with many things, it depends... 与许多事情一样,这取决于...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM