Partilhar via


Object Templates

In Service Manager, we have a variety of scenarios where we needed to capture partial object state and apply that state at a later time. The state captured is usually all or part of some IT process. For example, when creating a Change Request from the UI, the user is prompted with a set of templates to choose from, each representing a different kind of change request. These templates are stored as Object Templates in MPs and pre-define a set of values to populate the change request form with.

Object templates can be used to store data about classes as well as projections. They are neither classes, nor objects, but rather blueprints (stored in management packs) for creating or modifying objects. In the attached MP you will find three different object templates.

  1.     <ObjectTemplate ID="NFL.NFCDivision.Template" TypeID="NFL.Division" >
  2.       <Property Path="$Target/Property[Type='NFL.Conference']/Name$">NFC</Property>
  3.     </ObjectTemplate>

The first two set properties of a class. The Path notation is the same as used in projections, with the exception that you specify the particular property you want to set as well. You can specify as many or as few properties as you want, these objects don’t need to be complete, but we do verification to ensure property values make sense.

Projections can also be defined:

  1.     <ObjectTemplate ID="NFL.New.Template" TypeID="NFL.Conference.All" >
  2.       <Property Path="$Target/Property[Type='NFL.Conference']/Name$">New Conference</Property>
  3.       <Object Path="$Target/Path[Relationship='NFL.ConferenceHostsDivision']$">
  4.         <Property Path="$Target/Property[Type='NFL.Conference']/Name$">New Conference</Property>
  5.         <Property Path="$Target/Property[Type='NFL.Division']/Name$">New Division 1</Property>
  6.         <Object Path="$Target/Path[Relationship='NFL.DivisionContainsTeam' TypeConstraint='NFC.Team']$">
  7.           <Property Path="$Target/Property[Type='NFC.Team']/Name$">New Team</Property>
  8.         </Object>
  9.       </Object>
  10.       <Object Path="$Target/Path[Relationship='NFL.ConferenceHostsDivision']$">
  11.         <Property Path="$Target/Property[Type='NFL.Conference']/Name$">New Conference</Property>
  12.         <Property Path="$Target/Property[Type='NFL.Division']/Name$">New Division 2</Property>
  13.       </Object>
  14.     </ObjectTemplate>

These are similar, but allow you to define a hierarchy of objects that conform to the projection definition the object template references. Whenever you want to define a new object, you need to ensure the endpoint of the relationship is concrete, or you need to specify the TypeConstraint to indicate what class you want to create, as in the example above.

In code, you can use these templates to create new objects or update existing objects:

  1.             // Connect to the management group

  2.             EnterpriseManagementGroup managementGroup =

  3.                 new EnterpriseManagementGroup("localhost");

  4.             // Get the management pack

  5.             ManagementPack nflManagementPack =

  6.                 managementGroup.ManagementPacks.GetManagementPack("NFL", null, new Version("1.0.0.0"));

  7.             // Get the object templates

  8.             ManagementPackObjectTemplate nfcDivisionTemplate =

  9.                 nflManagementPack.GetObjectTemplate("NFL.NFCDivision.Template");

  10.             ManagementPackObjectTemplate afcDivisionTemplate =

  11.                 nflManagementPack.GetObjectTemplate("NFL.AFCDivision.Template");

  12.             ManagementPackObjectTemplate nflTemplate =

  13.                 nflManagementPack.GetObjectTemplate("NFL.New.Template");

  14.             // Create a new NFC division using the template

  15.             CreatableEnterpriseManagementObject newNfcDivision =

  16.                 new CreatableEnterpriseManagementObject(managementGroup, nfcDivisionTemplate);

  17.             // Change it to an AFC division

  18.             newNfcDivision.ApplyTemplate(afcDivisionTemplate);

  19.             // Create a new projection from a template

  20.             EnterpriseManagementObjectProjection newNfl =

  21.                 new EnterpriseManagementObjectProjection(managementGroup, nflTemplate);

  22.             // You can also apply a template to an existing projection

  23.             newNfl.ApplyTemplate(nflTemplate);

NFL.xml