跨多个平台使用业务逻辑的最佳方法(云功能?) [英] Best Way to Use Business Logic Across Multiple Platforms (Cloud Functions?)

查看:47
本文介绍了跨多个平台使用业务逻辑的最佳方法(云功能?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在分别使用ReactJS和iOS创建应用程序的移动版和网络版.这两个平台都将从Firestore数据库中提取数据以供使用,但是我想知道什么是只写一次业务逻辑以便对数据库进行操作的最佳方法是什么?

I am in the process of creating a mobile and web version of an app using ReactJS and iOS respectively. Both of these platforms will pull data down from a Firestore database to use but I am wondering what is the best way to only write business logic once in order to do operations on the database?

例如,在两个应用程序上,您都将单击一个按钮来更新Firestore数据库中的字段,而不是先编写逻辑来用Javascript编写代码,然后再编写Swift,那么最好的做法是只编写一次逻辑然后在两个平台上调用相同的逻辑?

For instance, on both apps you will click a button that updates a field in the Firestore database, instead of writing the logic to do this in Javascript and then Swift, is there a best practice to only have to write the logic once and then call the same logic from both platforms?

云功能是实现这一目标的最佳方法吗?我可以用Go语言编写一个云函数,然后从switft中的iOS应用程序和javascript中的ReactJS应用程序中调用此云函数吗?这是最佳做法吗?

Would cloud functions be the best way to achieve this? Could I write one cloud function in say Go, and then call this cloud function from both the iOS app in switft and ReactJS app in javascript? Is this best practice?

推荐答案

使用云函数来增加共享代码的数量确实是一个常见的用例.一种相当常规的模式是使用Cloud Functions进行更复杂的写入,其中客户端仅调用单个函数,然后该函数包含更复杂的代码.我仍然会在应用程序代码本身中保留更简单的写操作,但这是优先选择的问题.

Using Cloud Functions to increase the amount of shared code is indeed a common use-case. A fairly regular pattern is to use Cloud Functions for more complex writes, where the client merely calls a single function, which then contains the more complex code. I'd still keep simpler write operations in the application code itself, but that is a matter of preference.

请注意,如果重复的代码是扇出操作,则还可以让客户端将主文档写入数据库本身,然后触发云功能执行扇出操作.这样,客户端仍然可以获得通过SDK编写的好处(例如,它可以脱机工作),但是某些代码在服务器上,因此可以在客户端之间重用.

Note that if the duplicated code is a fan-out operation, you can also have the client write the primary document to the database itself, and then have that trigger a Cloud Function to perform the fan-out. That way the client still gets the benefits of writing through the SDK (e.g. it works offline), but some of the code is on the server and thus reused between clients.

我还看到许多开发人员创建了Cloud Functions来合并他们需要的所有数据.这样,客户端可以只执行一个调用就可以从多个集合中获取数据.但是我本人并不喜欢这样做,因为那样会失去Cloud Firestore SDK的离线和实时功能.

I also see many developers creating Cloud Functions to join all the data they need. So that way the client can do just one call to get data from multiple collections. I'm not a big fan of that myself however, because you lose the offline and realtime capabilities of the Cloud Firestore SDKs that way.

这篇关于跨多个平台使用业务逻辑的最佳方法(云功能?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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