How best to isolate in-memory resources by Workbook?
I've written an ExcelDNA plugin that generates data and re-uses it in subsequent calculations. The data is currently stored in static lists, dictionaries etc., but this makes it available to all open workbooks when it should be specific to the workbook that created it. This will inevitably lead to clashes and dependencies between workbooks. I'd hoped each workbook would operate in its own memory space but that doesn't seem to be the case.
I could include a workbook identifier in all dictionary keys (and turn lists into dictionaries of lists etc.) but that seems fiddly and prone to error.
Another approach would be for the static methods to retrieve a class instance from a dictionary keyed on the workbook identifier and have all the resources stored within those class instances (which would also do all the work and just return results to the static methods). That seems a safer approach.
However before I go ahead, is there a better way? My googling hasn't found anything about this.
The workbook name is unique in the Excel instance, while using a COM reference to the Workbook object is probably not a good idea. So you'll need to keep in mind that the name can change in a 'Save As' and either check for this now and then, or make a plan by monitoring the COM events.
Thanks for the quick response, and for the tip on the workbook name. In the absence of a persistent identifier I'll probably add a GUID into the workbook (perhaps in an XMLPart or a "Defined Name") and use that as the key.
I'll proceed with my second approach (workbook-specific instance classes called from static methods) unless there's a better way?
You'll have to consider whether the Guid will be easier to manage than the name as files are opened, copied and saved.
Maybe binary names would be useful for this.
Depending on the size and frequency of need to refresh, a workbook is essentially a self-contained database via its worksheets, too.
Thanks, I'll look into the binary names. I'm also considering CustomDocumentProperties (as well as Defined Names and XMLParts which I'm already using in my project).
Whichever route I take the simplest way I can think of to cope with govert's point would be on workbook load if the workbooks ID is already in my dictionary then I give the workbook a new ID. That might be simpler than trying to cope with changes of filename.
In terms of the size of the data it can be millions of rows. I only display a sample of them (to keep Excel from going bang) but the functions act on the full dataset. The upshot being that the data isn't in the worksheets directly (although I do persist any non-deterministic results to XMLParts, everything else gets recalculated after the workbook is opened).
@SeanMJFowler You might find mem_view handy. It uses memory mapped files as backing store for data. Of course you will eventually have to store that somewhere more permanent.
Thanks Keith; I'll bear that in mind if I get performance issues. I store the data as arrays and, as they're just arrays of numbers, it's pretty efficient. So far applying multiple simple operations to millions of rows using LINQ takes around half a second on a 3 core VM, but I still have more to do so I might yet hit problems.
Thanks for the help guys. I've gone for storing a GUID in a CustomDocumentProperty; if I get issues I can easily switch to some other store. On load I always set this property to a new GUID. I haven't yet implemented clearing-down the cached data when a document is closed.
In case it helps anyone I've used this pattern for classes that previously contained static resources (e.g. _results was previously static):
public class ResultCache
{
private static readonly Dictionary<string, ResultCache> _resultCaches = new Dictionary<string, ResultCache>();
private readonly Dictionary<string, object[,]> _results = new Dictionary<string, object[,]>();
public static ResultCache GetCache()
{
var workbookId = ExcelHelper.GetWorkbookId();
if (!_resultCaches.TryGetValue(workbookId, out var output))
{
output = new ResultCache();
_resultCaches.Add(workbookId, output);
}
return output;
}
Any non-static methods are then called like this:
ResultCache.GetCache().StoreResult(...
The static GetCache method gets the right instance based on the active workbook (if that proves unsafe I'll derive the workbook from the caller cell).