Skip to content
This repository has been archived by the owner on Dec 31, 2021. It is now read-only.

Possibility of Dependency Injection #5

Open
FormallyMyles opened this issue Jun 12, 2016 · 12 comments
Open

Possibility of Dependency Injection #5

FormallyMyles opened this issue Jun 12, 2016 · 12 comments
Assignees
Labels

Comments

@FormallyMyles
Copy link

FormallyMyles commented Jun 12, 2016

Hi!

A lot of modern APIs use dependency injection, and it's not a bad idea when it's used right and does not cause latency and what (Resolving those dependencies).

Would this project consider that?

eg.

@Plugin(name = "An addition to the Amazing Test Plugin", id = "testPlugin2", author = "myles")
public class TestPlugin {
    @Inject
    Logger logger;

    @EventHandler
    public void onEnable(ServerStartEvent event) {
        logger.info("The magic of a logger!");
    }

}

An example of a library that implements this is Guice (https://github.com/google/guice/wiki/GettingStarted), Sponge currently uses this also.

@sgdc3
Copy link

sgdc3 commented Jun 12, 2016

Guice is a huge library, if you want to use a more small implementation take a look at https://github.com/AuthMe-Team/AuthMeReloaded

@sgdc3
Copy link

sgdc3 commented Jun 12, 2016

@phase phase added the plugin label Jun 12, 2016
@Techcable
Copy link
Member

Techcable commented Jun 12, 2016

So guava is a 'maybe', dependency injection is definite (where possible).
However, this can cause issues. For example how can you have material constants if you don't have a server instance to construct it?

@Techcable Techcable added this to the Bukkit feature parity milestone Jun 13, 2016
@Techcable Techcable self-assigned this Jun 13, 2016
@Techcable
Copy link
Member

@MylesIsCool
Are you referring to dependency injection everywhere?
Or is a server singleton fine?

@phase
Copy link
Member

phase commented Jun 13, 2016

@sgdc3 I wasn't able to find where you implemented javax.inject.

@MylesIsCool @Techcable I don't know how much help this can be. The Logger example is nice, but that could easily be done with a Factory method or something similar, and I don't think using Guice or creating our own dependency injector would benefit plugin developers in enough ways to warrant the code needed for it.

@PizzaCrust
Copy link
Contributor

It looks nicer and more modern than just using a method to retrieve the logger.

@Techcable
Copy link
Member

We could depreciate Fountain.getServer() and tell plugins they have to have 'a good reason' to use it 😄

@PizzaCrust
Copy link
Contributor

Use injection to retrieve server instance?

@Techcable
Copy link
Member

@PizzaCrust Yes, normally plugins would do that, but to manufacture Material constants we need a static getter for the server.

@minecrafter
Copy link
Contributor

👍 to Guice.

If we decide to add injection in, the functionality to inject our own classes would be very nice - Guice already lets you do this.

@sgdc3
Copy link

sgdc3 commented Jun 23, 2016

@ljacqu is working on a simple and very lightweight inject implementation, based on the injector we are using in authme.

@ljacqu
Copy link

ljacqu commented Jun 23, 2016

I can confirm what @sgdc3 says, it's in use in AuthMe (look at the initialization package) and is in the process of being extracted as its own project.

If you need to do tons of fancy stuff with dependency injection – use Guice. We needed a simple, lightweight one where we basically have one instance of each class (singletons) we want to wire together elegantly.

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

No branches or pull requests

7 participants