Lack of horizontal programming facilities #12826
Replies: 3 comments 3 replies
-
While that's somewhat true (AFAIK), the usual approach is to just write higher-level controllers and methods. For example, I pretty much never use the standard methods for defining Actions. Instead, I define my own methods for, eg, AuthenticatedAction vs UnauthenticatedAction vs AdminAction -- stating clearly who is allowed to call what. Ditto for serialization on the outbound side. And I generally have specialized Controllers as well, bundling these specialized Actions together in ways that make sense. IMO, that's better than having universal one-size-fits-all plugins -- it means the call site is saying exactly what it means (which reduces the risk of security-compromising oversights). But in practice it takes no more call-site code than using such plugins does; if anything, thoughtful factoring usually results in more-concise and readable controller code. |
Beta Was this translation helpful? Give feedback.
-
Currently away from keyboard, so I can't provide a direct example, but the
high concept is that you define a method that does the user authentication
and takes a function parameter; your concrete endpoints call that with a
function that takes the user as a parameter, and which provides the guts of
the endpoint logic.
(Admittedly this is easier in Scala than Java, but lambda functions are no
longer unknown in Java. Basically, you're writing your own version of the
standard Action, on top of the standard one.)
…On Fri, Aug 2, 2024, 9:56 AM GithubUser8080 ***@***.***> wrote:
Could you please provide an example for the first part?
E.g i would like to receive a User in my controller method, and have this
be injected by the request body, depending on the request content-type
—
Reply to this email directly, view it on GitHub
<#12826 (reply in thread)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAQVNYNUC25B3UJWSSLTEULZPOFYNAVCNFSM6AAAAABL4HIEKOVHI2DSMVQWIX3LMV43URDJONRXK43TNFXW4Q3PNVWWK3TUHMYTAMRSGQYTMMA>
.
You are receiving this because you commented.Message ID:
<playframework/playframework/repo-discussions/12826/comments/10224160@
github.com>
|
Beta Was this translation helpful? Give feedback.
-
I have done something similar, an action for authentication, but since I need the current principal in controllers, you need to put the principal into a request attribute in the action, then in every controller method you need to do something like
Which means you also need to inject authentication (unless you use a static method) and the request object in every method. Then considering you also need serialization and deserialization, every controller method ends up having code for authentication, authorization, deserialization, serialization, and various validation errors. It's not difficult, but i would say probably 60% of my controllers are repetition of horizonal concerns. |
Beta Was this translation helpful? Give feedback.
-
Greetings, i would like to ask if there is a potential support for horizontal concerns in controllers. The lack of facilities to easily register application wide functionality like Spring is making us move to other frameworks, and I would like to see if we can avoid that because play has a lot of good things.
Authentication, authorization, serialization, all of that are usually applied to the entire app and it is discouraging that we have to write the same code in every controller method.
Some examples are
serializer.serialize(my dto, request)
which means i also have to inject serializers in every controller.This are just my own views on what i find hard to do in play, as feedback. Apologies if there are workarounds I do not know about.
Beta Was this translation helpful? Give feedback.
All reactions