(The images for this post were taken from the ESRI site because I no longer use the ArcDesktop applications)
In our project we are using Domains to display text instead of the codes written in our tables.
Well first what are Domains?
I always found Domains and Subtypes very similar. They are both used to bring description to a field in a feature. The difference I guess is that Subtypes also separate the features to groups whereas Domains only give text value.
According to ESRI domains are used as validators for the data inserted using ArcMap/ArcCatalog. The trouble with that is that in all my systems so far we didn’t insert the feature data by those programs – we did it all by code! And at least in 9.3.1 there were no validations on the field…
(from ESRI site using domain coded values)
Whereas according to ESRI subtypes allow much more. They increase performance, they create all kinds of rules and they allow the applying of domains on the fields.
How do I create domains regularly (in ArcCatalog)?
In ArcCatalog right click on the Geodatabase –> Properties –> Domains tab:
(Taken from the ESRI site)
On the first empty line in the top table add your domain name and description.
On the next table enter the Domain Type: Coded Values, Range
If you choose Range you will have to enter a minimum and maximum values
If you choose Coded Values you will have to enter in the bottom table the codes of the domain and their description. We use Coded Values domains.
How do I associate a field with a domain(in ArcCatalog)?
In ArcCatalog right click on a table/layer –> Properties –> Subtypes:
(Taken from the ESRI site)
On the bottom table click on the field you want to add the domain to and select the domain from the combo box.
How do I create domains by C# code?
You can read more about it here.
- #region Create/Delete Domains
- public void CreateDomain(string name, esriFieldType type, List<KeyValuePair<object ,string >> domains)
- {
- ICodedValueDomain codedValueDomain = new CodedValueDomainClass();
- foreach (var pair in domains)
- {
- codedValueDomain.AddCode(pair.Key, pair.Value);
- }
- // The code to set the common properties for the new coded value domain.
- var domain = (IDomain)codedValueDomain;
- domain.Name = name;
- domain.FieldType = type;
- domain.SplitPolicy = esriSplitPolicyType.esriSPTDuplicate;
- domain.MergePolicy = esriMergePolicyType.esriMPTDefaultValue;
- ((IWorkspaceDomains) _workspace).AddDomain(domain);
- }
- public void DeleteDomain(string name)
- {
- ((IWorkspaceDomains)_workspace).DeleteDomain(name);
- }
- //For tests
- internal IDomain GetDomain(string name)
- {
- return ((IWorkspaceDomains)_workspace).DomainByName[name];
- }
- #endregion
The code uses an inner variable _workspace which is the current IWorkspace.
Weird things (found while unit testing):
1. You can create a domain with a name of “” or null but they will have the same name (later in my code I added a validation for the name field so that it has to have a value).
2. You can create a domain without any coded values (empty list not null list!)
3. You cannot create two domains with the same name, an exception will be thrown: COMException: Domain name already in use.
4. You cannot delete a domain with a name of “” or null – an exception is thrown that is NOT COMException - System.ArgumentException: Invalid function arguments – from the DeleteDomain method(my DB is doomed…). I manually deleted it from the database it was the only line in GDB_ITEMS where Name,PhysicalName,Path and Url were empty string (and just because its good to know – the type for domains is ‘8C368B12-A12E-4C7E-9638-C9C64E69E98F’).
How do I use a domain in a feature field by C# code?
You can read more about it here.
- #region Assign Domains
- public void AssignDomain(string layerName, string fieldName, string domainName)
- {
- var featureClass = GetFeatureClass(layerName);
- var domain = ((IWorkspaceDomains) _workspace).DomainByName[domainName];
- var field = featureClass.GetField(fieldName);
- if (field.Type != domain.FieldType)
- throw new ApplicationException(
- "The field and the domain are not of the same type. Cannot assign domain to field.");
- var schemaLock = (ISchemaLock)featureClass;
- try
- {
- schemaLock.ChangeSchemaLock(esriSchemaLock.esriExclusiveSchemaLock);
- ((IClassSchemaEdit)featureClass).AlterDomain(fieldName, domain);
- }
- finally
- {
- schemaLock.ChangeSchemaLock(esriSchemaLock.esriSharedSchemaLock);
- }
- }
- #endregion
Weird things (found while unit testing):
1. If you try to delete a domain that is in use it will throw a COMException: The domain is used as a default domain
2. To remove a domain simply do it with null - AlterDomain(fieldName, null) . I added a method called RemoveDomain that does that.
In GDB_ITEMS a field in a feature class that uses domain looks like:
- <GPFieldInfoEx xsi:type="typens:GPFieldInfoEx">
- <Name>Type</Name>
- <AliasName>Type</AliasName>
- <ModelName>Type</ModelName>
- <DomainName>DefaultDomain</DomainName>
- <FieldType>esriFieldTypeSmallInteger</FieldType>
- <IsNullable>false</IsNullable>
- </GPFieldInfoEx>
And a field in a feature class that doesn’t use domain looks like:
- <GPFieldInfoEx xsi:type="typens:GPFieldInfoEx">
- <Name>Type</Name>
- <AliasName>Type</AliasName>
- <ModelName>Type</ModelName>
- <FieldType>esriFieldTypeSmallInteger</FieldType>
- <IsNullable>false</IsNullable>
- </GPFieldInfoEx>
The end result was this:
- #region Create/Delete Domains
- /// <summary>
- /// Create a domain in the DB
- /// </summary>
- /// <param name="name"></param>
- /// <param name="type"></param>
- /// <param name="domains"></param>
- public void CreateDomain(string name, esriFieldType type, List<KeyValuePair<object ,string >> domains)
- {
- if (String.IsNullOrEmpty(name))
- throw new ArgumentException("Parameter name cannot be null or empty.", "name");
- ICodedValueDomain codedValueDomain = new CodedValueDomainClass();
- foreach (var pair in domains)
- {
- codedValueDomain.AddCode(pair.Key, pair.Value);
- }
- // The code to set the common properties for the new coded value domain.
- var domain = (IDomain)codedValueDomain;
- domain.Name = name;
- domain.FieldType = type;
- domain.SplitPolicy = esriSplitPolicyType.esriSPTDuplicate;
- domain.MergePolicy = esriMergePolicyType.esriMPTDefaultValue;
- ((IWorkspaceDomains) _workspace).AddDomain(domain);
- }
- /// <summary>
- /// Delete a domain from the DB
- /// </summary>
- /// <param name="name"></param>
- public void DeleteDomain(string name)
- {
- if (String.IsNullOrEmpty(name))
- throw new ArgumentException("Parameter name cannot be null or empty.", "name");
- ((IWorkspaceDomains)_workspace).DeleteDomain(name);
- }
- //For tests
- internal IDomain GetDomain(string name)
- {
- return ((IWorkspaceDomains)_workspace).DomainByName[name];
- }
- #endregion
- #region Assign Domains
- /// <summary>
- /// Assign the domain to the field in layerName
- /// </summary>
- /// <param name="layerName"></param>
- /// <param name="fieldName"></param>
- /// <param name="domainName"></param>
- public void AssignDomain(string layerName, string fieldName, string domainName)
- {
- if (String.IsNullOrEmpty(domainName))
- throw new ArgumentException("Parameter name cannot be null or empty.", domainName);
- var domain = ((IWorkspaceDomains)_workspace).DomainByName[domainName];
- AssignDomain(layerName, fieldName, domain);
- }
- /// <summary>
- /// Remove the domain usage from the field
- /// </summary>
- /// <param name="layerName"></param>
- /// <param name="fieldName"></param>
- public void RemoveDomain(string layerName, string fieldName)
- {
- AssignDomain(layerName, fieldName, (IDomain)null);
- }
- private void AssignDomain(string layerName, string fieldName, IDomain domain)
- {
- if (String.IsNullOrEmpty(layerName) || String.IsNullOrEmpty(fieldName))
- throw new ArgumentException("Parameter name cannot be null or empty.");
- var featureClass = GetFeatureClass(layerName);
- if(domain != null)
- {
- var field = featureClass.GetField(fieldName);
- if (field.Type != domain.FieldType)
- throw new ApplicationException(
- "The field and the domain are not of the same type. Cannot assign domain to field.");
- }
- var schemaLock = (ISchemaLock)featureClass;
- try
- {
- schemaLock.ChangeSchemaLock(esriSchemaLock.esriExclusiveSchemaLock);
- ((IClassSchemaEdit)featureClass).AlterDomain(fieldName, domain);
- }
- finally
- {
- schemaLock.ChangeSchemaLock(esriSchemaLock.esriSharedSchemaLock);
- }
- }
- //For Tests
- internal IField GetLayerField(string layerName, string fieldName)
- {
- return GetFeatureClass(layerName).GetField(fieldName);
- }
- #endregion
//TODO: post this only after writing about extension methods and WorkspaceUtils
//TODO: Refactor the code…
Resources:
ESRI: A quick tour of attribute domains
ESRI: A quick tour of subtypes
ESRI: About maintaining attribute integrity while editing (using domains)
ESRI: Exercise 3: Creating subtypes and attribute domains
ESRI: Creating and modifying domains
ESRI: Assigning domains to fields
Keywords: ESRI, ArcSDE, SDE, domain, subtype, ArcCatalog, ArcMap, ArcDesktop,IWorkspace, C#, Add, Delete, ArcObjects