Is it possible to save a class in another namespace?

Caché

Hi 

Quick question... Is it possible to save a class in another namespace. If you're saving a global you can just use an extended reference, but is it possible to achieve the same when saving a class object?  Obviously the class will be defined in the other namespaces too.

I want to do it from an Ensemble SOAP Service. I guess you could switch namespaces, but I was a bit worried about doing that from Ensemble process.

thanks

 

  • + 1
  • 0
  • 157
  • 4
  • 3

Answers

Any of the following is possible, but this will only work if you want to keep the data in the two namespaces for that class exactly the same : 

  • Map the globals that are used by the persistent class to the other namespace.

OR

  • Change the global names in the Class Storage definition (DataLocation, IDLocation, IndexLocation, StreamLocation) and add a namespace reference to it, like this :
Storage Default
{
<Data name="customerDefaultData">
...
<DataLocation>^["USER"]test.customerD</DataLocation>
<DefaultData>customerDefaultData</DefaultData>
<IdLocation>^["USER"]test.customerD</IdLocation>
<IndexLocation>^["USER"]test.customerI</IndexLocation>
<StreamLocation>^["USER"]test.customerS</StreamLocation>
<Type>%Library.CacheStorage</Type>

 

Be careful if properties are pointing to other persistent classes as well !

Hi Danny

Thanks, but neither of those would work. There are multiple possible namespaces I might want to save the class to.  Each with their own version of the global (i.e. containing different records)

Yes it does contain a child class too, so that needs to be considered.

This is possible:

Class MyClass As %Persistent {

Method SaveToNs(Namespace = {$namespace}) As %Status
{
  new $namespace = ""
  set $namespace = Namespace
  quit:'##class(%Dictionary.CompiledClass).%ExistsId(..%ClassName(1)) $$$ERROR($$$GeneralError, "Class is undefined in: " _ Namespace)
  set sc = ..%Save()
  quit sc

}

}

Hello,

If you need to save your class in more than 1 namespace, and you are using studio, you may do it automatically with studio hooks.

This is done in that way:
1. You create your own source control class which inherit from %Studio.SourceControlBase
2. you put your code in that class. For example you may use OnAfterSave method to run on all namespaces that your class need to be saved, loop on this list (except the current one of course) and save + compile your class (programmatically) in each namespace. 

https://irisdocs.intersystems.com/iris20191/csp/docbook/DocBook.UI.Page.cls?KEY=GSTD_Intro#GSTD_intro_schooks

https://irisdocs.intersystems.com/iris20191/csp/documatic/%25CSP.Documatic.cls?APP=1&LIBRARY=%25SYS&CLASSNAME=%25Studio.SourceControl.Base

 

 

 

 

Comments

Does your suggestion just switch namespace? 

We will be receiving events to a central SOAP service for a number of 'sites' on the same machine in different namespaces. They share a common database, but some globals are not mapped to the central database namespace so as to separate out some settings and they also need to take responsibility for resolving data issues for the patients belonging to their area.  

The SOAP service will upload events automatically for individual patients, but each 'site' only wishes to see events related to their patients and manually resolve 'rejections' for their area.  I think in explaining the case, I have thought of another way to do it. I think I will save the object centrally and just save a namespace specific global which points to ones for their area, then I can also create an index by 'site'.  I think that should do it.  

thanks for your suggestions!

Thanks for sharing this.  I would be interested in seeing some of the data structures you decided on with dummy data.

I would typically use a percent (%) global if I wanted a global to be accessible across multiple namespaces but I don't usually mix percent globals and class storage definitions.