Git Product home page Git Product logo

scopes's Introduction

Scopes

Easy-to-use custom Guice scopes - reentrant and not. Bits available in this Maven repository/continuous-build.

Usage

It is not uncommon, using Guice, to want to be able to provide some dynamically created objects for injection into other code. Particularly if you are writing an extensible system, this is useful.

The standard Guice approach is Assisted Inject - which, while (IMO) boilerplate-heavy, works. On the other hand, if you want to write something where clients building on what you write will be able to extend things with their own objects - that is, you don't know all the types that might be injected in the future - then assisted inject is less appealing. Yes, you could tell someone they have to write factories for 20 types, or write code that creates 20 factories in a loop and binds things, but who wants to do that.

This library offers a simple alternative:

class MyModule extends AbstractModule {
    public void configure() {
        ReentrantScope scope = new ReentrantScope();
        bind (ReentrantScope.class).toInstance(scope);

        // there is no getting around explitly binding classes, but
        // you can provide a way to pass in an array easily enough:

        scope.bindTypes(binder(), FooRequest.class, FooResponse.class, ...);
    }
}

public class TheApp {

    public void onRequest(FooRequest req) {
        FooResponse resp = new FooResponse();
        try (AutoCloseable ac = scope.enter(req, resp)) {
            // Anything with a Provider<FooRequest>, etc. will
           // get the passed request
        }
    }
}

You enter the scope with an array of objects; you exit the scope by calling scope.exit() on the same thread you entered it on (the easy way is to use the AutoCloseable as shown above).

Threading

Sometimes you need multi-threading; whereas typically Guice scopes are little more than a wrapper for some ThreadLocals. So, all subclasses of AbstractScope let you call

ExecutorService wrapThreadPool (ExecutorService svc)

If you are inside a ReentrantScope, and dispatch a Runnable to an ExecutorService wrappered via this method, the current scope contents will be frozen and reconstituted before the Runnable runs - so you get identical scope contents to what you had when you submitted teh Runnable.

This way it is possible to have all of the benefits of scoping, and have a complex threading model.

scopes's People

Contributors

timboudreau avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

scopes's Issues

Repository?

Is there a Repository that Scopes is hosted in?

I attempted to find one and failed, then tried to build it using jitpack


[ERROR] The build could not read 1 project -> [Help 1]
[ERROR]   
[ERROR]   The project com.mastfrog:scopes-parent:[unknown-version] (/home/jitpack/build/pom.xml) has 1 error
[ERROR]     Non-resolvable parent POM: Could not find artifact com.mastfrog:mastfrog-parent:pom:2.4.1 in central (https://repo.maven.apache.org/maven2) and 'parent.relativePath' points at no local POM @ line 5, column 13 -> [Help 2]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/ProjectBuildingException
[ERROR] [Help 2] http://cwiki.apache.org/confluence/display/MAVEN/UnresolvableModelException

https://jitpack.io/com/github/timboudreau/scopes/bc0c7d195b/build.log

Which results in the above error.

Thanks for making this available btw! I'm hoping to use it along with http://www.github.com/SpongePowered/SpongeAPI/ to have better injections with our contexts.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.