* The following is a great work by a friend of mine - Yoav Michaeli, assisted by Shani. Yoav wrote about it in his Hebrew blog, but never posted the code, so I asked for his permission to bring this to my blog.
The following is Yoav's attempt to solve two problems:
1. Creating ArcObjects in ArcGIS Server.
It is very annoying to create ArcObjects when writing ArcGIS Server Local code. i.e.
IPoint point = myServerContext.CreateObject(“esriGeometry.Point”) as IPoint;
This is not type-safe code, and does not lend itself well to refactoring. Also, many beginners with ArcObjects forget to use this syntax, and use "new" instead, which obviously doesn't work. The outcome is a lot of wasted time.
2. Converting between the ArcGIS Server code to client code, and vice versa.
When working with ArcMap or ArcEngine, you will create your objects in the standard way, i.e.
IPoint point = new PointClass();
But if you now decide to use this code over ArcGIS Server, you will have to manually change all your creation code to work against an IServerContext.
ArcObjectFactory
Yoav has solved this problem by introducing ArcObjectFactory, which confronts these issues with some (very simple) reflection magic.
You will use it like this:
IArcObjectFactory factory = new AgsArcObjectFactory(myServerContext);
IPoint point = factory.CreateObject<Point>();
Now, if you want to change your code to work on the client-side, you will just have to change the first line:
IArcObjectFactory factory = new ClientArcObjectFactory();
Of course, this is just an example. In real life you might want your class to expose an IArcObjectFactory property, and inject the actual factory via a constructor, or an IoC container such as Windsor. This way, the exact same code can be deployed in several enviroments with no code change at all.
Also, this approach makes writing unit-tests with mocks a lot easier. At my workplace, any code that uses ArcObjects works with an ArcObjectFactory.
The full code for ArcObjectFactory can be found here. Yoav - thanks again, and good luck in your new place!