This is the refactor for ESRI example for creating an Address Locator in a Personal GeoDatabase:
- public void AddressLocatorPersonalGDBViaArcObjects()
- {
- // Open the default local locator workspace to get the locator style.
- System.Object obj = Activator.CreateInstance(Type.GetTypeFromProgID(
- "esriLocation.LocatorManager"));
- ILocatorManager2 locatorManager2 = obj as ILocatorManager2;
- ILocatorWorkspace locatorWorkspace = locatorManager2.GetLocatorWorkspaceFromPath
- ("");
- // Get the locator style to base the new locator.
- ILocatorStyle locatorStyle = locatorWorkspace.GetLocatorStyle(
- "US Address - Dual Ranges");
- // Open the feature class to use as reference data.
- IWorkspaceFactory2 workspaceFactory2 = new AccessWorkspaceFactoryClass();
- IWorkspace workspace = workspaceFactory2.OpenFromFile(
- @"D:\workspace\arcobjects\location\redlands.mdb",
- 0);
- IFeatureWorkspace featureWorkspace = (IFeatureWorkspace) workspace;
- IFeatureClass featureClass = featureWorkspace.OpenFeatureClass("Streets");
- // Set the feature class as the primary reference data table for the locator.
- IDataset dataset = (IDataset) featureClass;
- IReferenceDataTables referenceDataTables = (IReferenceDataTables) locatorStyle;
- IEnumReferenceDataTable enumReferenceDataTable = referenceDataTables.Tables;
- enumReferenceDataTable.Reset();
- IReferenceDataTable referenceDataTable = enumReferenceDataTable.Next();
- IReferenceDataTableEdit referenceDataTableEdit = (IReferenceDataTableEdit)
- referenceDataTable;
- IName name = dataset.FullName;
- referenceDataTableEdit.Name_2 = (ITableName) name;
- // Store the new locator in the same workspace as the reference data.
- if (referenceDataTables.HasEnoughInfo)
- {
- locatorWorkspace = locatorManager2.GetLocatorWorkspaceFromPath(
- @"D:\workspace\arcobjects\location\redlands.mdb");
- ILocator locator = locatorWorkspace.AddLocator("New Redlands Locator",
- (ILocator) locatorStyle, "", null);
- }
- }
The first order of business is refactoring this method.
The first thing to go was this:
- IFeatureClass featureClass = featureWorkspace.OpenFeatureClass("Streets");
- // Set the feature class as the primary reference data table for the locator.
- IDataset dataset = (IDataset) featureClass;
To:
- IDataset dataset = (IDataset)featureWorkspace.OpenFeatureClass("Streets");
There was a few other things like: referenceDataTable->referenceDataTableEdit,
The end result was:
- public void AddressLocatorPersonalGDBViaArcObjects()
- {
- // Open the default local locator workspace to get the locator style.
- System.Object obj = Activator.CreateInstance(Type.GetTypeFromProgID(
- "esriLocation.LocatorManager"));
- var locatorManager2 = obj as ILocatorManager2;
- var locatorWorkspace = locatorManager2.GetLocatorWorkspaceFromPath
- ("");
- // Get the locator style to base the new locator.
- var locatorStyle = locatorWorkspace.GetLocatorStyle(
- "US Address - Dual Ranges");
- // Open the feature class to use as reference data.
- IWorkspaceFactory2 workspaceFactory2 = new AccessWorkspaceFactoryClass();
- var featureWorkspace = (IFeatureWorkspace)workspaceFactory2.OpenFromFile(
- @"D:\workspace\arcobjects\location\redlands.mdb",
- 0);
- // Set the feature class as the primary reference data table for the locator.
- var dataset = (IDataset)featureWorkspace.OpenFeatureClass("Streets");
- var referenceDataTables = (IReferenceDataTables)locatorStyle;
- var enumReferenceDataTable = referenceDataTables.Tables;
- enumReferenceDataTable.Reset();
- var referenceDataTableEdit = (IReferenceDataTableEdit) enumReferenceDataTable.Next();
- referenceDataTableEdit.Name_2 = (ITableName)dataset.FullName;
- // Store the new locator in the same workspace as the reference data.
- if (referenceDataTables.HasEnoughInfo)
- {
- locatorWorkspace = locatorManager2.GetLocatorWorkspaceFromPath(
- @"D:\workspace\arcobjects\location\redlands.mdb");
- locatorWorkspace.AddLocator("New Redlands Locator",
- (ILocator) locatorStyle, "", null);
- }
- }
39 lines without any loss of readability, not that it says much – it still not readable!
The next thing to look at is creating the ILocatorManager2, I don’t know about you but I prefer getting an exception about converting than about Null reference:
- (ILocatorManager2)Activator.CreateInstance(Type.GetTypeFromProgID("esriLocation.LocatorManager"));
(that also solved the problem of the ‘obj’ variable naming)
The next part is creating a Utils class for LocatorWorkspace:
- public class LocatorWorkspaceUtils
- {
- private readonly ILocatorManager2 _locatorManager;
- #region Singleton
- private static readonly LocatorWorkspaceUtils instance = new LocatorWorkspaceUtils();
- // Explicit static constructor to tell C# compiler
- // not to mark type as beforefieldinit
- static LocatorWorkspaceUtils()
- {
- }
- private LocatorWorkspaceUtils()
- {
- _locatorManager =
- (ILocatorManager2)Activator.CreateInstance(Type.GetTypeFromProgID("esriLocation.LocatorManager"));
- }
- public static LocatorWorkspaceUtils Instance
- {
- get { return instance; }
- }
- #endregion
It’s singleton with ILocatorManager already set, now the only thing we use it for is:
- private ILocatorWorkspace GetLocatorWorkspace(string fileLocation)
- {
- return _locatorManager.GetLocatorWorkspaceFromPath(fileLocation);
- }
Getting the style:
- public ILocatorStyle GetLocatorStyle(string styleName)
- {
- return GetLocatorStyle(styleName, "");
- }
- public ILocatorStyle GetLocatorStyle(string styleName, string fileLocation)
- {
- var locatorWorkspace = GetLocatorWorkspace(fileLocation);
- // Get the locator style to base the new locator.
- return locatorWorkspace.GetLocatorStyle(styleName);
- }
Adding the locator style:
- public void AddLocatorStyle(string styleName, string fileLocation, ILocator locatorStyle)
- {
- var locatorWorkspace = GetLocatorWorkspace(fileLocation);
- locatorWorkspace.AddLocator(styleName, locatorStyle, "", null);
- }
Setting the dataset name in the locator:
- public void SetLocatorDatasetName(IName datasetName, ILocatorStyle locatorStyle)
- {
- var referenceDataTables = (IReferenceDataTables)locatorStyle;
- var enumReferenceDataTable = referenceDataTables.Tables;
- enumReferenceDataTable.Reset();
- var referenceDataTableEdit = (IReferenceDataTableEdit)enumReferenceDataTable.Next();
- //var name = new TableNameClass {Name = datasetName};
- referenceDataTableEdit.Name_2 = (ITableName)datasetName;
- }
Checking if the locator is good to be saved:
- public bool IsLocatorValidForSave(ILocatorStyle locatorStyle)
- {
- return ((IReferenceDataTables)locatorStyle).HasEnoughInfo;
- }
Now the code looks like:
- public void AddressLocatorPersonalGDBViaArcObjects()
- {
- // Get the locator style to base the new locator.
- var locatorStyle = LocatorWorkspaceUtils.Instance.GetLocatorStyle("US Address - Dual Ranges");
- // Open the feature class to use as reference data.
- IWorkspaceFactory2 workspaceFactory2 = new AccessWorkspaceFactoryClass();
- var featureWorkspace = (IFeatureWorkspace)workspaceFactory2.OpenFromFile(
- @"D:\workspace\arcobjects\location\redlands.mdb",
- 0);
- // Set the feature class as the primary reference data table for the locator.
- var dataset = (IDataset)featureWorkspace.OpenFeatureClass("Streets");
- LocatorWorkspaceUtils.Instance.SetLocatorDatasetName(dataset.FullName, locatorStyle);
- if (LocatorWorkspaceUtils.Instance.IsLocatorValidForSave(locatorStyle))
- {
- LocatorWorkspaceUtils.Instance.AddLocatorStyle("New Redlands Locator",
- @"D:\workspace\arcobjects\location\redlands.mdb",
- (ILocator)locatorStyle);
- }
- }
Now something seems a bit off here, the only reason we open the workspace for the MDB file is to get the dataset FullName, WTF?!? Why not just pass the name as a… wait for it… string?
TODO: continue this
Keywords: ESRI, ArcObjects, refactor