Skip to content

Suite Context

On modular applications there is usually a need to store and access contextual information. An example of this is the HttpContext provided by Asp.Net. A similar approach is use by the Suite framework, which provides a ISuiteContext service through DI.

Important

Below we show how to retrieve and store information from and into the ISuiteContext directly, but we strongly recommend implementing extension methods to encapsulate this logic and avoid using the methods shown below directly all over a module. See this.

Accessing the SuiteContext

The ISuiteContext can be accessed by injecting it as a dependency. For example:

C#
1
2
3
4
5
6
7
8
9
    internal class SampleMiddleware: IMiddleware
    {
        private readonly ISuiteContext suiteContext;

        public SampleMiddleware(ISuiteContext suiteContext)
        {
            this.suiteContext = suiteContext;
        }
    }

Retrieving information from ISuiteContext

The ISuiteContext interface provides two methods to retrieve information, Get and GetRequired. Both methods accept a string key, and if there is a value for the key, both methods have the same behavior, returning the value. These methods differ when the value for the key is not present, in that case Get will return null while GetRequired will throw an exception.

C#
Foo? someFoo = suiteContext.Get("foo-key") as Foo;

try
{
    Bar someBar = (Bar)suiteContext.GetRequired("bar-key");
}
catch(InvalidOperationException ex)
{
    // Handle required value not found.
}

Storing information on the ISuiteContext

The Set method is used to store information on the ISuiteContext. To do so we need to pass the string key and the object to be stored.

C#
suiteContext.Set("foo-key", someFooObject);