Reputation: 20050
I would like to implement a certain context for ongoing operations in my application (.NET, C#) such that code that is started an executed by the application could access it.
The issue is that the context is not being passed around as an object, like certain APIs are doing.
I would like to implement something similar to OperationContext in WCF, where this static class can provide each operation its context.
How can one implement such a system?
An example usage:
Our application loads and executes 3rd party "plugins" that are created by anyone with access to our SDK.
Such user code may look like this:
public void SomeMethod(string a, string b)
{
// NEEDED: something like this
Context.Results = new Results();
// Do some stuff here
// Call another method (without passing Results to it)
AnotherMethod();
}
public void AnotherMethod()
{
// NEEDED: Access the context (Results) somehow to add more info to it.
Context.Results.Add(something);
}
According to this example, all i need is some static Context class, but how can i make sure it would be unique for every executed operation in my application?
Upvotes: 3
Views: 372
Reputation: 4736
As an option, if you were to introduce an IoC container like Autofac into your solution, it could provide access to a shared context when it creates objects.
See this SO question for an example, which points to this article about object relationship types. It should give you some ideas about what's possible with this approach.
Upvotes: 1