像 Fractal 这样的图书馆在哪里? [英] where does a library like Fractal belong?

查看:19
本文介绍了像 Fractal 这样的图书馆在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我今天正在研究 Fractal(tldr:数据对象/集合到 json 格式库)并看到了一些好处使用它.然而,它的功能似乎跨越了我正在开发的应用程序的多个层.因此出现了一个问题——使用 Fractal 的代码属于哪里?模型、服务、控制器,还是其他地方?项目文档的文档中给出的示例似乎倾向于将其放在控制器中或直接放在路由回调中(更复杂的示例似乎来自 Laravel 应用程序,作者在他的 API 书中提到了这一点).

I was looking into Fractal (tldr : data object/collection to json formatting library) today and saw some benefits to using it. However it's functionality seems to span across multiple layers of the app I'm working on. Hence a question appeared -- where does a code utilising Fractal belong to? Model, service, controller, some other place? The examples given in the documentation at the project docs seem to favour putting it in the controller or right in a route callback (more complex examples seem to be coming from Laravel app and the author mentioned it in his book on API).

我关心的是耦合——如果我把它放在控制器中,正如大多数使用示例所显示的那样,那么我将来就会受到它的约束.我的第一直觉是将它抽象一点,将抽象绑定到一个合同,然后将其投入使用.可能听起来过分设计,但我正在开发的 API有抱负"与 JSON-API 兼容,所以交换这样的其他东西的json格式化程序"听起来不那么疯狂.此外,我仍然需要格式化错误消息,而 Fractal 似乎根本没有触及这一点.

My concern is coupling -- if I put it in the controller, as most of the usage examples show, then I'm pretty much bound to it in the future. My first instinct is to abstract it a little, make that abstraction bound to a contract and then put it to use. Might sound over engineered, but the API I'm working on "is aspiring" to be JSON-API compliant, so exchanging such a "json formatter" for something else sounds less crazy. Besides I still need to format error messages and Fractal seems no to touch that at all.

我想利用对 Eloquent 分页器和嵌入式资源的支持,因为这总是很痛苦.仅这样做会使表示/控制层变得尴尬(至少可以说).即使在 Fractal 文档中,他们也会向控制器类添加一些额外的方法来准备 Fractal 对象.这对我来说似乎有点奇怪,但也许只是我.这就是为什么把它带到这里.

I'd like to take advantage of support for Eloquent's paginator and embedded resources, because that's always a pain. Only doing that makes it awkward (to say the least) at presentation/control layer. Even in the Fractal docs they resort to adding some extra methods to the controller class to prep Fractal objects. It seems a bit weird to me, but maybe it's just me. That's why take it here.

我知道这可能是一个偏好问题,但我指望有人有一个合理的声音:).或者也许是一个更好的解决方案,请记住自动化和 json-api 合规性是关键原因.

I'm aware that it might be a matter of preference, but I'm counting on somebody to have a reasonably sounding one :). Or perhaps a better solution altogether, keeping in mind that automation and json-api compliance are key reasons.

推荐答案

我曾用一个 API 类为我的应用程序需要与之交互的专有系统执行此操作.API 返回的对象看起来很像模型,所以我为我需要的对象实现了许多类,并实现了一个库来进行 API 调用并返回对象.幸运的是,我只需要对 API 进行读取访问,因此我的库仅实现了一小部分可用操作.

I did this once with an API class for a proprietary system with which my application needed to interface. The API returned objects that looked a lot like models, so I implemented a number of classes for the objects I needed and implemented a library to make the API calls and return the objects. Luckily, I only needed read access to the API, so my library implements only a small subset of the actions available.

也许您可以将您需要的所有功能(包括 Fractal 和任何 Eloquent 功能)抽象到您已经为其定义了接口的库类中.这样所有的 Fractal 代码都在一个地方,如果你需要替换它,你只需重写你的自定义库类(这可能需要很多工作,但可能比寻找散布在整个代码中的 Fractal 引用更好).

Maybe you could abstract all the functionality you need (both Fractal and any Eloquent features) into a library class for which you have defined an interface. That way all the Fractal code is in one place and if you ever need to replace it, you just rewrite your custom library class (which might be a lot of work, but probably better than hunting down references to Fractal sprinkled throughout your code).

这篇关于像 Fractal 这样的图书馆在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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