[mapguide-users] Re: Maestro API - InitialView of WebLayout is null ?!

Jackie Ng jumpinjackie at gmail.com
Thu Jul 21 22:38:05 EDT 2011


Hi Crispin,

There is a design pattern used with most of the resource interfaces
throughout the Maestro API which I will explain right now.

Child properties of any resource object, by the virtue of being reference
types in .net can be null. This has certain quirks when serializing to XML
due to:

 1. The XML schema stating that certain elements must be specified. 
 2. Null properties do not get serialized out. So if you require a certain
element to be omitted instead of being serialized out as an empty XML tag,
you would set its property to null.

What the ObjectFactory does it creates a resource object that *should* be
immediately serializable to XML without violating any constraints imposed by
its respective XML schema. Any child reference type properties which cannot
be null will also be initialized with an object with corresponding default
values. 

The ObjectFactory solves the common problem in the previous Maestro API
where you would instantiate a resource object, but run into
NullReferenceExceptions because you forgot to initialize one of its child
properties. 

So why is wl.Map.InitialView null when you create the IWebLayout from the
ObjectFactory?

This is because the InitialView element in the WebLayout XML schema is
*optional*. With no InitialView element in the WebLayout, the AJAX viewer
will use the extents of the referenced Map Definition instead. The
ObjectFactory will only fill in the properties that are *required*, optional
ones you have to create and assign to the object yourself

So you do you create and assign an InitialView? The IWebLayout interface has
a CreateDefaultView() method which you can then set the X/Y/Scale and then
assign it to the Map component of the Web Layout object.

So instead of this:

   IWebLayout wl = ObjectFactory.CreateWebLayout(conn, mdfId);

   txtStats.Text += "\nMap: " + wl.Map.ResourceId;                       //
OK
   txtStats.Text += "\nScale: " + wl.Map.InitialView.Scale;                
// null ref 

Do this:

   IWebLayout wl = ObjectFactory.CreateWebLayout(conn, mdfId);

   txtStats.Text += "\nMap: " + wl.Map.ResourceId;                       //
OK
   IMapView initView = wl.CreateDefaultView();
   initView.CenterX = 123000;
   initView.CenterY = 456000;
   initView.Scale = 10000;

   wl.InitialView = initView;
   txtStats.Text += "\nScale: " + wl.Map.InitialView.Scale;                
// now OK

And this is the pattern you should always look for: If an interface has
child properties which are interfaces themselves, there should also be
"factory" methods also available to allow you to create such instances that
can then be assigned to said child property.

Now what I've said thus far concerns creating resource objects *from
scratch*. If you're creating resource objects from XML, then it's simply
using the ResourceTypeRegistry.Deserialize() method. Usage is simple. Feed
it an xml string and you get back an IResource object, which you can then
cast to your desired resource interface (because all resource interfaces
derive from IResource). 

So in your case, it would be:

   IWebLayout wl = (IWebLayout)ResourceTypeRegistry.Deserialize(xml);
   txtStats.Text += "\nMap: " + wl.Map.ResourceId;                       //
OK
   txtStats.Text += "\nScale: " + wl.Map.InitialView.Scale;              //
Also OK

Please note however that by default, the ResourceTypeRegistry can only
deserialize XML content that use version 1.0.0 of their respective XML
schema. Additional schema versions have to be registered with the
ResourceTypeRegistry to be recognised. For Maestro 3.5, I've added a
convenience library (OSGeo.MapGuide.ExtendedObjectModels) that does all this
registration work for you.

Hope this helps.

- Jackie

--
View this message in context: http://osgeo-org.1803224.n2.nabble.com/Maestro-API-InitialView-of-WebLayout-is-null-tp6607143p6609197.html
Sent from the MapGuide Users mailing list archive at Nabble.com.


More information about the mapguide-users mailing list