-
Notifications
You must be signed in to change notification settings - Fork 0
1. Context and initialization
Context serves as main repository of all dependencies that required for library to function. It is immutable object which you must initialize before making any requests. Basically it configures logger, caches and mappers for requests and responses.
Simple default context initialization looks like this:
Context.InitializeDefault(1, () => { /* Ready to go callback */ });
You pass Version
number and onContextReady
to this function. When onContextReady
fires, you
are ready to go with your requests.
But sometimes you need more control over cache sizes, have custom loggers and type-mappers. So,
you can use other two arguments of InitializeDefault
to configure this stuff.
Complete function signature looks like this:
InitializeDefault(
int version,
Action onContextReady,
Action<Builder> onBeforeInstantiate = null,
int inflateMultiplier = 1)
You can configure and change all aspects of Context
by using onBeforeInstantiate
callback. It receives Builder
of the context and exposes all the aspects of configuration.
Context.InitializeDefault(Version, () => {
Initialized = true;
}, builder => {
builder.WithLogger(new MyLogger());
builder.WithMapper(typeof(Json<>), typeof(NewtonsoftJsonMapper<,>));
builder.WithMapper(typeof(Json), typeof(NewtonsoftJsonMapper<,>));
builder.WithMemoryCache(memoryCacheSize);
builder.WithDiskCache(diskCacheSize);
builder.WithNativeCache(nativeCacheSize);
});