哪里像一个分形库属于? [英] where does a library like Fractal belong?

查看:168
本文介绍了哪里像一个分形库属于?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找进入分形(tldr:数据对象/收集到JSON格式库)今天,看到一些好处使用它。然而,它的功能似乎穿过我工作的应用程序的多个层次跨越。因此,一个问题出现了 - 哪里一个code形利用属于?模式,服务,控制器,其他地方?在项目文档似乎文档中给出​​的例子赞成把它在控制器或右的路线回调(更复杂的例子似乎是从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).

我担心的是耦合 - 如果我把它放在控制器,因为大部分的使用实例表明,那我多势必在将来pretty。我的第一本能是抽象了一点,作出这样的抽象绑定到一个合同,然后把它用。初听了工程,但我在努力是有抱负的API是 JSON-API 兼容,所以这种交换一个JSON格式别的东西听起来很疯狂少。此外,我还需要格式化的错误信息和分形似乎没有触摸到的。

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.

我想充分利用了雄辩的分页程序和嵌入式资源的支持,因为这是永远的痛。只是做这使得它别扭presentation /控制层(至少可以说)。即使在分形文档他们诉诸于增加一些额外的方法给控制器类preP分形物体。这似乎有点怪我,但也许这只是我。这就是为什么把它在这里。

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.

我知道这可能是preference的事,但我指望有人有一个合理的冠冕堂皇的:)。或者也许是更好的解决方案完全,牢记自动化和 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.

也许你可以抽象所有你需要(包括分形和任何雄辩的功能)到您已定义的接口库类的功能。这样,所有的分形code是在一个地方,如果你需要更换它,你只需重写你的自定义库类(这可能是大量的工作,但可能比引用追捕到分形更好地在整个洒你的code)。

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).

这篇关于哪里像一个分形库属于?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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