Data Sharing Between Apps
Updating a Context
See the .NET Shared Contexts example on GitHub.
First, make sure you initialize the Glue
object with Contexts
enabled:
Glue.Initialize(
applicationName: "ApplicationThatUsesContexts",
useContexts: true // true by default
);
At this point, you can proceed to creating and retrieving Contexts
, update them and respond to changes:
// if "TestContext" doesn't exist, it will be automatically created
IContext context = await Glue.Contexts.GetContext("TestContext");
You can update some of the properties of the object, while the others remain the same:
context["Contact"] = new { AccountName = "ACC001", Name = "John Smith" };
context["Test"] = new Dictionary<string, List<object>> { { "Key", new List<object> { 1, 2, 3 } } };
To remove a property, set it to null
:
context["Alpha"] = null;
A transactional update:
await context.Update(ctx => { ctx["Foo"] = "Bar"; ctx["now"] = DateTime.Now; });
Or, in a more strongly typed manner:
// define user-specific context with its own properties
public class Status
{
public string State {get;set;}
public DateTime Date {get;set;}
// etc.
}
public interface IMyContextType : IContext
{
Status MyProperty {get;set;}
int SomeOtherProperty {get;set;}
// you can add more POCO's here to reflect your application needs
}
IMyContextType myContext = await Glue.Contexts.GetContext<IMyContextType>("TestContext");
// myContext is proxy-code generated by Glue42 so when reading/writing from/to the IMyContextType
// you effectively read/write from/to the Glue42 context
myContext.MyProperty = GetCurrentStatus();
Contexts are keyed by name, so every .NET, JavaScript or other Glue42 enabled application that requests TestContext
will have access to the same data.
Replacing a Context
In addition to updating the context, you can also replace the entire context object by using the Set()
method of the context object:
// setting value of the context replaces the entire context object
await context.Set(new { Alpha = "A", Beta = "B" });
// you can use Dictionaries as well
await context.Set(new Dictionary<string, object> { { "Alpha", "A" }, { "Beta", "B" } });
Subscribing for Context Updates
An example of responding to context data update events:
context.ContextUpdated += Context__ContextUpdated;
void Context__ContextUpdated(object sender, ContextUpdatedEventArgs e)
{
Console.WriteLine("Context updated: " + new
{
(sender as IContext).ContextId,
(sender as IContext).ContextName,
e.IsReset,
Added = "[" + string.Join(", ", e.Added.Select(x => x + "")) + "]",
Updated = "[" + string.Join(", ", e.Updated.Select(x => x + "")) + "]",
Removed = "[" + string.Join(", ", e.Removed.Select(x => x + "")) + "]",
});
}
An example of responding to context creation/deletion events:
Glue.Contexts.Created += Contexts_Created;
void Contexts_Created(object sender, ContextStateEventArgs e)
{
Console.WriteLine("Context created: " + new { e.ContextId, e.ContextName });
}
Glue.Contexts.Removed += Contexts_Removed;
void Contexts_Removed(object sender, ContextStateEventArgs e)
{
Console.WriteLine("Context removed: " + new { e.ContextId, e.ContextName });
}
Unsubscribing
To unsubscribe from context updates:
context.ContextUpdated -= Context__ContextUpdated;
Glue.Contexts.Created -= Contexts_Created;
Glue.Contexts.Removed -= Contexts_Removed;
Listing All Available Contexts
To get the names of all currently available shared contexts, use:
string[] contextNames = Glue.Contexts.GetContexts();