Question

Je suis un peu nouveau dans l'injection de dépendance et j'ai un doute. Dans mon application, j'ai un haschmap pour stocker des cours construits (comme un cache) pour la modernisation, mais maintenant je déménage à Di avec Dague et j'aimerais savoir comment puis-je atteindre le même comportement.

Mon 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;
    }

Après avoir commencé avec di, ma classe "module":

 @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();
    }

Alors, comment puis-je injecter ce hasch "cache" à mon application?

merci

Était-ce utile?

La solution

Vous pouvez créer une classe auquel il est seul objectif serait de fournir des classes d'interface de repos. Voici l'interface / la mise en œuvre

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;
    }

}

dans votre module, vous auriez

@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;
}

La manière dont cela fonctionne est que le fournisseur de rétablissement de votre module serait utilisé comme dépendance dans votre instance RestaPriViderImpl. Maintenant, n'importe où vous auriez besoin d'une instance de classe RestaPI, vous devez simplement injecter votre RestaPripRovider.

@Inject
RestApiProvider restApiProvider;

// Somewhere in your code
RestApiClassOfSomeSort instance = restApiProvider.getRestClient(RestApiClassOfSomeSort.class);
instance.// do what you need!

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top