Skip to content

1. Context and initialization

Sergey Ivonchik edited this page Oct 29, 2021 · 8 revisions

What is Context

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)

How to configure Context

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

Versioning and caching

Clone this wiki locally