Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for clean extension of factories #311

Open
lstrojny opened this issue Oct 15, 2020 · 4 comments
Open

Allow for clean extension of factories #311

lstrojny opened this issue Oct 15, 2020 · 4 comments

Comments

@lstrojny
Copy link

We needed to re-add runtime embeddeds and links again. In order to do so, we override the EmbeddedsFactory and LinksFactory with class extending each. That’s a bit unclean and a cleaner way would be to decorate the original classes. To enable that, one would need to introduce two interface Hateoas\Factory\EmbeddedsFactoryInterface and Hateoas\Factory\LinksFactoryInterface:

interface EmbeddedsFactoryInterface
{
    /** @return Embedded[] */
    public function create(object $object, SerializationContext $context): array;
}
interface LinksFactoryInterface
{
    /** @return Link[] */
    public function create(object $object, SerializationContext $context): array;
}

Then change the AddRelationsListener to use these new interfaces instead of the concrete classes. Would you be interested in this change?

@goetas
Copy link
Collaborator

goetas commented Oct 18, 2020

I personally would like to avoid having relations added at runtime. I prefer to declare relations when possible and then allow to exclude them at runtime. The main advantages of this approach is that is possible to generate OpenAPI documentation of those models. (example is NelmioApiDoc Bundle). When relations are declared at runtime, that is not possible anymore.

I understand that for some projects that might be not easy, but in this library I would like that ppl start thinking in that direction.

@lstrojny
Copy link
Author

Just to note: I get that and I am not proposing adding the ability for runtime relations but instead being able to add capabilities like that in a clean way.

@goetas
Copy link
Collaborator

goetas commented Oct 29, 2020

@lstrojny sorry for the misunderstanding, but yes that sounds a good idea. this change needs also some adaptations in the builder class to allow you to pass the custom implementation of the factories, right?

@lstrojny
Copy link
Author

Yup, that seems necessary. Since the EmbeddsFactory and the LinksFactory have a few dependency, one could either inject factory factories or the class name of the factories. The latter would limit adding new dependencies in an extension while the former is quite a bit complicated. Any opinions?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants