FountainMC / FountainAPI

A 'simple but beautiful' Minecraft Server Plugin API
https://fountainmc.org/
MIT License
5 stars 5 forks source link

Possibility of Dependency Injection #5

Open FormallyMyles opened 8 years ago

FormallyMyles commented 8 years ago

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 commented 8 years ago

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 commented 8 years ago

https://github.com/AuthMe-Team/AuthMeReloaded/blob/master/pom.xml#L422

Techcable commented 8 years ago

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 commented 8 years ago

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

phase commented 8 years ago

@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 commented 8 years ago

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

Techcable commented 8 years ago

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

PizzaCrust commented 8 years ago

Use injection to retrieve server instance?

Techcable commented 8 years ago

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

minecrafter commented 8 years ago

👍 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 commented 8 years ago

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

ljacqu commented 8 years ago

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.