somakeit / smib

1 stars 0 forks source link

**EXPERIMENT** Re-write app with no dependency injection #20

Open sam57719 opened 5 months ago

sam57719 commented 5 months ago

Interesting to see how the app would differ without dependency injection.

My Initial Ideas

Would need to add factory functions for all injectables e.g.

from functools import cache

@cache
def get_slack_app():
    app = CustomApp(...)
    return app

@cache
def get_plugin_manager():
    app = get_slack_app()
    manager = PluginManager(app)
    return manager

And/or define a singleton decorator for use on our own class implementations

def singleton(cls):
    """A decorator function to make a class a Singleton"""
    instances = {}

    def wrapper(*args, **kwargs):
        if cls not in instances:
            instances[cls] = cls(*args, **kwargs)
        return instances[cls]

    return wrapper

@singleton
class PluginManager:
    ...
sam57719 commented 3 months ago

@sjefferson99 any input on this at all would be appreciated. Any good design patterns? The slack app by default uses decorator registration for the event hooks

Factories, interfaces, etc etc.