Message 1 of 10
Is it safe to store Schema and Field references in an add-in static variable of a static class?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I have tried using the following code while switching documents and it seems to work.
All the methods in SchemaManager call GetSchema() to make sure the 4 static variables have a reference to the Schema and its fields, then use the static fields inside the code.
I tested it working while switching between two documents, and everything seems to be working just fine.
I was expecting some kind of crash, because (I think) I am caching the static fields on Doc1 and using them in Doc2.
Are the static fields always reset when I start a new command?
Is the scope of the static fields somehow reset when switching documents?
Are schema and field unique across documents?
Am I doing something really wrong and my quick tests don't show the problem?
public static class SchemaManager
{
private static Schema _schema;
private static Field _numbers, _strings, _points;
private static void GetSchema()
{
if (_schema != null)
return;
var schemaGuid = new Guid("603564a1-cc29-4545-a24b-5e72e0818aae");
_schema = Schema.Lookup(schemaGuid);
if (_schema == null)
{
var schemaBuilder = new SchemaBuilder(schemaGuid);
schemaBuilder.SetSchemaName("IntelliClad");
schemaBuilder.SetReadAccessLevel(AccessLevel.Public);
schemaBuilder.SetWriteAccessLevel(AccessLevel.Vendor);
schemaBuilder.SetVendorId("UniverseCorp");
schemaBuilder.AddMapField("Numbers", typeof(string), typeof(double)).SetSpec(SpecTypeId.Length);
schemaBuilder.AddMapField("Strings", typeof(string), typeof(string));
schemaBuilder.AddMapField("Points", typeof(string), typeof(XYZ)).SetSpec(SpecTypeId.Length);
_schema = schemaBuilder.Finish();
}
_numbers = _schema.GetField("Numbers");
_strings = _schema.GetField("Strings");
_points = _schema.GetField("Points");
}