question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

how to apply Decorator design pattern with lagom?

See original GitHub issue

Hi, How to apply the Decorator design pattern with lagom, on single instance registrations and/or collection registrations?

Example how I used to do it with a DI container from C# .Net called SimpleInjector (if same syntax were to be in lagom):

class HttpClient(ABC):
    async def send(self, request: Request) -> Response:
        pass

class ActualHttpClient(HttpClient):
    async def send(self, request: Request) -> Response:
        # Implement actual request send, e.g. with aiohttp

class LoggingHttpClientDecorator(HttpClient):
    def __init__(self, decoratee: HttpClient):
        self.__decoratee = decoratee
    async def send(self, request: Request) -> Response:
        print("Sending request")
        response = await self.__decoratee.send(request);
        print("Received response")
        return response;

container = Container()
container[HttpClient] = Singleton(ActualHttpClient)
container.RegisterSingletonDecorator[HttpClient, LoggingHttpClientDecorator](); # mimics the SimpleInjector syntax

With this code, resolving HttpClient would return LoggingHttpClientDecorator decorating ActualHttpClient. SimpleInjector also supports collection injections, e.g. injecting something like Iterable[HttpClient], and apply a decorator to all of the HttpClient instances registered to the collection.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
Eldar1205commented, Aug 30, 2021

Thanks for your attention and collaboration with this issue 😃 I think your suggestion for decorator with lambda would requires users of this feature to do more work in the following case: Imagine the decorator has another dependency other than the decorated class, e.g. Logger, then as a user I would need to write the lambda to include that other dependency which should also be resolved from the container somehow. Now imagine I wrote that down, and my decorator was changed to have an additional dependency in the constructor - now I need to fix my lambda registration to match that as well.

An API that supports both container.register_decorator(HttpClient, LoggingHttpClientDecorator) or container.register_decorator(HttpClient, Singleton(LoggingHttpClientDecorator)) would resolve both this issues if the container would inject into LoggingHttpClientDecorator the decorated HttpClient and any other dependencies it has just with any normal registered class. Note that users should be able to apply multiple decorator registrations such that the other matter to determine the decorator chain, e.g. LoggingHttpClientDecorator decorator which is then decorated itself with CachingHttpClientDecorator

0reactions
meadstevecommented, Aug 31, 2021

hmmm. I really like your suggestion. It’s quite easy to read. My only concern about it is that it could end up making it easy to create some construction infinite loops depending on how the decoration is implemented.

Maybe it’s worth trying out. If it doesn’t work then something like the following is also an option:

container.register_decorator(HttpClient, lambda decorated, c: LoggingHttpClientDecorator(decorated, logger=c[Logger]))

having the container passed as an argument to the lambda is a pattern I’ve used in other parts of the library so it shouldn’t be surprising.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Decorator Design Pattern in Java Example - DigitalOcean
Decorator design pattern is one of the structural design pattern (such as Adapter Pattern, Bridge Pattern, Composite Pattern) and uses abstract ...
Read more >
Lagom Decor Ideas for a Balanced Home - Invaluable.com
Start practicing lagom with interior design expert Niki Brantmark's “one in, one out” rule. For every item you buy, get rid of something...
Read more >
Decorator Design Pattern in Java with Example - GeeksforGeeks
The decorator design pattern is a structural pattern, which provides a wrapper to the existing class. The decorator design pattern uses abstract ...
Read more >
Issues · meadsteve/lagom - GitHub
how to apply Decorator design pattern with lagom ? enhancement New feature or request. #165 opened on Aug 23, 2021 by Eldar1205.
Read more >
Lagom Home Decoration – Boosting of Happiness
Use of ceramic and glass accessories. You can enhance the simple style of your interior by adding minimalistic design home decor items made...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found