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

Ease the creation of singleton components in factory #211

Open
a-peyrard opened this issue Jul 29, 2015 · 3 comments
Open

Ease the creation of singleton components in factory #211

a-peyrard opened this issue Jul 29, 2015 · 3 comments

Comments

@a-peyrard
Copy link
Contributor

To create simple factories, especially in unit tests, I often create the factory with a bunch of singleton factory machines like this:

Factory factory = Factory.builder()
            .addMachine(new SingletonFactoryMachine<>(0, NamedComponent.of(String.class, ComponentModule.ENV_APP_NAME, "test")))
            .addMachine(new SingletonFactoryMachine<>(0, NamedComponent.of(EventBus.class, "tempEventBus", new EventBus())))
            .build();

I think it might be nice to add a method to the factory builder, permitting to do something like this:

Factory factory = Factory.builder()
            .addSingletonMachine(String.class, ComponentModule.ENV_APP_NAME, "test")
            .addSingletonMachine(EventBus.class, "tempEventBus", new EventBus())
            .build();

WDYT ?

@xhanin
Copy link
Contributor

xhanin commented Jul 30, 2015

You can already do something with LocalMachines, something like:

LocalMachines.threadLocal()
            .set(ComponentModule.ENV_APP_NAME, "test")
            .set("tempEventBus", new EventBus());

Factory factory = Factory.builder()
            .addLocalMachines(LocalMachines.threadLocal())
            .build();

// do stuff


LocalMachines.threadLocal().clear();

I think the LocalMachines API is pretty fine for that, the problem is that LocalMachines are always put in a context (a named context or a thread local) and therefore must be cleared. Maybe by adding a way to get a fresh and disposable instance of LocalMachines to use in a builder that would be enough? Something like:

Factory factory = Factory.builder()
            .addLocalMachines(LocalMachines.local()
                            .set(ComponentModule.ENV_APP_NAME, "test")
                            .set("tempEventBus", new EventBus()))
            .build();

Maybe we could even allow the easy creation of a Factory from LocalMachines, sg like:

Factory factory = LocalMachines.local()
                            .set(ComponentModule.ENV_APP_NAME, "test")
                            .set("tempEventBus", new EventBus())
                            .buildFactory();

WDYT?

@a-peyrard
Copy link
Contributor Author

I didn't thought of using the local machine API. I'm using it quite often, but more in a way of ensuring the existence of components or overriding components, in unit tests, when the factory is created somewhere else in the system.

I like the two solutions, indeed having to clear the thread local when you are the creator of the factory is not convenient, so the instance of a LocalMachines not linked to any thread local could be useful. So could be the last proposition, in order to create a factory in a very compact way.

@fcamblor
Copy link
Contributor

+1 with Augustin :-)

Le jeu. 30 juil. 2015 09:29, a-peyrard notifications@github.com a écrit :

I didn't thought of using the local machine API. I'm using it quite often,
but more in a way of ensuring the existence of components or overriding
components, in unit tests, when the factory is created somewhere else in
the system.

I like the two solutions, indeed having to clear the thread local when you
are the creator of the factory is not convenient, so the instance of a
LocalMachines not linked to any thread local could be useful. So could be
the last proposition, in order to create a factory in a very compact way.


Reply to this email directly or view it on GitHub
#211 (comment).

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

No branches or pull requests

3 participants