ManagedObjectReference vs ManagedObject
One of the most common confusions that a newcomer has while learning vSphere API is the ManagedObjectReference, a.k.a MOR. If you read the API Reference, you will find a lot of them. Recently there was a question poping in the open source VIJava API forum. So I think it is worth explaining it here.
There are two major types in the vSphere API: managed object types are for these objects on the server side only; and data object types for properties, parameters, and results, which can be send back and forth between client and server. The MOR is a data object type, but used to represent managed objects living on server side. If you are familiar with C/C++ programming, you can think of it as pointer in some sense. Even better, think of it as a combination of table and primary key in relational database. I will explain it more soon.
The MOR has two properties: type and val, both are strings. The reason for it having type is that there are close to 100 managed object types but only one MOR for all of them. To distinguish them, type is therefore used and its value is limited to available types which are 93 in vSphere 4.1.
The val is the unique identifier for a managed object within a target server. For the same managed object, say a virtual machine, the val can be different when you get it from an ESX/ESXi or the vCenter that manages the ESX/ESXi. Now, let’s get back to the relational database analog just mentioned, the type is the “table” and val is the “primary key.”
Too abstract? Let’s take a look at a sample. A MOR representing a virtual machine may look like this:
type: VirtualMachine
val: vm-820
Once we are clear with MOR, let’s take a look at the ManagedObject. To your surprise, there isn’t a managed object type called ManagedObject in vSphere API reference. As a side story, I told one of my colleagues about this in a meeting and his response was, “I can’t believe it! I got to look it up and show you.” He never gets back to me afterwards.
If you use VI Java API, you do see a type/class ManagedObject which packs quite some code. It’s super class for every managed object types like HostSystem, VirtualMachine, etc. The main reason I added it to the VI Java API is for logical completeness of the object model. When logical, a nice object model comes naturally. From the implementation perspective, it does pull up much code for re-use across different subclasses.
With ManagedObject and its subtypes in VI Java API, you will have objects to work with and write much less code. Even nicer, you have all the typing in place. Before you can have a MOR object pointing to a virtual machine or a host, you mix them up and the compiler won’t catch the mistake. In VI Java API, there are VirtualMachine and HostSystem objects and you simply cannot confuse them or the compiler won’t let you go.
I am sure you all want the benefits of using VI Java API, but still wonder if there is a big performance difference between using MOR and ManagedObject (mainly its subtypes). In theory, there is a difference; in practice, you won’t notice any difference caused by this because the ManagedObject is a VERY thin layer. In fact, you will see 10 times performance boost using VI Java 2.0 because of the new Web Service engine I wrote from scratch.
Well, by this far I hope you understand well the MOR and ManagedObject. When using VI Java API, however, please avoid MOR as much as possible. Instead, use ManagedObject and its subtypes.
Hi Steve,
could you explain this in a more practical manner also for a powercli user?
I realy appreciate your effort. Thank you
regards
Andreas
Hi Andreas,
A great question. PowerCLI has a different object model that is similar to VI Perl. The model has “View” which is close to the “ManagedObject” in VI Java API, but not the same. The “View” does not have subtypes as does ManagedObject in VI Java API, therefore you cannot achieve type safe there. Also, the “View,” once created, retrieves and keeps a local cache of all properties of a managed object, which could be a big waste if you are interested in only one or few of them.
Hope it helps. Please let me know if you have more questions.
-Steve
Hi Steve,
thanks for your explanation. I try to think about this. Maby one or more questions will rise up
I also read your Demystifying 3 “View”s from VMware and WPowerCLI Mastery, Volume 2W from winternals.com
Thanks
regards
Andreas
Hi Steve,
On a different note.
I don’t see any VI Java release after 2010-08-26. Since vCenter5.0 is released now, will there be any new release in VI Java? I presume that the existing version will work great with vCenter5.0 as well.
Regards,
Tulsi
Hi Tulsi,
The existing vijava 2.1 should work with vCenter 5 as it is but no access to new features. A new version of vijava will be released after vSphere 5 GAs. Stay tuned.
Steve
Thank you so much Steve. Sure.
Regards,
Tulsi