Select to view content in your preferred language

Connect to Geodatabase Featureclass and/or shapefile

4075
16
Jump to solution
02-10-2013 06:17 PM
GinoMellino
Frequent Contributor
Hi All,

This is probably very simple. I can load a geodatabase featureclass with IWorkspace::OpenFromFile and the FileGDBWorkspaceFactory CoClass and likewise with ShapefileWorkspaceFactoryClass for shapefiles...is there a way to load both with the same generic workspace factory or do I have to set a different one based on what is selected in my custom Add data dialog?

Thanks in advance
Gino
0 Kudos
16 Replies
LeoDonahue
Deactivated User
Yes I was talking about Factory method pattern.  I was thinking something along those lines.  A class where you just pass a string in the constructor, no workspace factory, and it spit out the appropriate workspace.

You would not want to pass a string, because that is not type safe.  What if someone passed in a misspelled string?
0 Kudos
AlexanderGray
Honored Contributor
You would not want to pass a string, because that is not type safe.  What if someone passed in a misspelled string?


Not a string containing the type, a string containing the path to the workspace.  Based on the type of string, *.gdb, *mdb,  *.sde etc. or the contents of the directory (info sub dir) or shapefiles, etc. the code decides which workspace factory is appropriate to instantiate.
0 Kudos
LeoDonahue
Deactivated User
right the constructor receives the string param that then decides what kind of workspace to create.  What if I passed in the string "taco"?
0 Kudos
AlexanderGray
Honored Contributor
Well same as if you try openFromFile with string "taco" on a valid workspace factory.  In this case, failure just comes earlier.  The whole point is validating the string before you even create the workspace factory.   In the process of trying to figure out the type of workspace there is a step that checks if it is a valid directory or file, if not throw an argument exception.  If after trying a bunch of combination, the code still can't figure out a valid workspace type, throw an argument exception.
0 Kudos
LeoDonahue
Deactivated User
But wouldn't you still want to pass an interface type to this factory method that decides how to return a workspace, rather than a string? 

There is more than one esriWorkspaceType.esriFileSystemWorkspace, which could be shapefile, in memory, etc.

Just as there is more than one type of esriWorkspaceType.esriLocalDatabaseWorkspace

If you declare a IWorkspaceFactory variable as a concrete type, then that type becomes the parameter for the WorkspaceFactory constructor.
0 Kudos
AlexanderGray
Honored Contributor
Well the exercise would be fraught with danger of returning the wrong type of workspace but to pass in the type as a parameter negates the purpose.  Which is why I have never done it.  However it is done in esri code because the GxDialog returns IGxObjects that have an IName.  The IName allows you to open the item which means creating a workspace somewhere.  I just haven't quite figured out how to do it with the IGxObject and IGxObjectFactory (which is strongly typed.)  Somewhere in the GxDialog, it recognizes the file types on disc and it can later open by creating the appropriate workspace.  I think we have run off the original topic here.
0 Kudos
EricBader
Honored Contributor
....

This is a Java example, so sorry for that.



Ha! No need to ever apologize for using Java. Thanks for these great discussions.
0 Kudos