Question

I'm kinda new to Dependency Injection and I have a doubt. In my app I have a HashMap to store built classes (like a cache) for RetroFit, but now I'm moving to DI with Dagger and I'd like to know how can I achieve the same behaviour.

My code:

private Map<String, Object> restInstances;
public <T> T getRestClient(Class<T> clazz) {
        T client = null;

        if ((client = (T) restInstances.get(clazz.getCanonicalName())) != null) {
            return client;
        }

        client = restAdapter.create(clazz);
        restInstances.put(clazz.getCanonicalName(), client);
        return client;
    }

After starting with DI, my "module" class:

 @Provides @Singleton
    public JobManager providesJobManager(){
        Configuration config = new Configuration.Builder(app)
                .minConsumerCount(1).maxConsumerCount(3).loadFactor(3).customLogger(new CustomLogger() {
                    private static final String TAG = "JOBS";

                    @Override
                    public boolean isDebugEnabled() {
                        return true;
                    }

                    @Override
                    public void d(String text, Object... args) {
                        Log.d(TAG, String.format(text, args));
                    }

                    @Override
                    public void e(Throwable t, String text, Object... args) {
                        Log.e(TAG, String.format(text, args));
                    }

                    @Override
                    public void e(String text, Object... args) {
                        Log.e(TAG, String.format(text, args));
                    }
                })
                .consumerKeepAlive(120).build();

        return new JobManager(app, config);
    }

    @Provides @Singleton
    public RestAdapter providesRestAdapter()
    {
        restInstances = new HashMap<String, Object>();
        return new RestAdapter.Builder()
                .setEndpoint("http://192.168.0.23:9000/api")
                .setLogLevel(RestAdapter.LogLevel.FULL).build();
    }

So, how can I inject this "cache" hash to my app ?

Thanks

Was it helpful?

Solution

You could create a class which it's sole purpose would be to provide rest interface classes. Here's the interface / implementation

public interface RestApiProvider {
    public <T> T getRestClient(Class<T> clazz);
}

public class RestApiProviderImpl implements RestApiProvider {
    private Map<String, Object> restInstances = new HashMap<String, Object>();
    private RestAdapter restAdapter;

    @Inject
    RestApiProvider(RestAdapter restAdapter) {
        this.restAdapter = restAdapter;
    }

    public <T> T getRestClient(Class<T> clazz) {
        T client = null;

        if ((client = (T) restInstances.get(clazz.getCanonicalName())) != null) {
            return client;
        }

        client = restAdapter.create(clazz);
        restInstances.put(clazz.getCanonicalName(), client);
        return client;
    }

}

In your module you would have

@Provides @Singleton
public RestAdapter providesRestAdapter()
{
    return new RestAdapter.Builder()
            .setEndpoint("http://192.168.0.23:9000/api")
            .setLogLevel(RestAdapter.LogLevel.FULL).build();
}

@Provides @Singleton
public RestApiProvider providesRestApiProvider(RestApiProviderImpl impl) {
    return impl;
}

The way this works is the the RestAdapter Provider from your module would be used as dependency in your RestApiProviderImpl instance. Now anywhere you'd need to get a RestApi class instance you'd simply need to inject your RestApiProvider.

@Inject
RestApiProvider restApiProvider;

// Somewhere in your code
RestApiClassOfSomeSort instance = restApiProvider.getRestClient(RestApiClassOfSomeSort.class);
instance.// do what you need!
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top