Welcome Guest! To enable all features please Login or Register.



How to reference enumeration data from your code
#1 Posted : Wednesday, October 24, 2012 7:13:17 PM(UTC)

When you write your business logic, you oftentimes need to reference specific values of your enumeration items in your code, to use enumeration names to retrieve lookup tables from a lookup cache by name, or to use enumeration properties to retrieve additional attributes of the corresponding Header objects.

Xomega allows you to generate constant strings for names of both static and dynamic enumerations and their additional properties, as well as for values of individual items of static enumerations. This will allow you to avoid hardcoding these strings all over your business logic, which will help with refactoring should any of the values or names change. The following steps explain how to generate such constants.
  1. Open the global configuration file for your model, which contains config element as the root. If you don't have such file, add one to the Xomega project by clicking the Add New Item menu on the project and selecting the Xomega Model Configuration template.
  2. Add or locate the xfk:enumerations element inside the global config element, where the xfk prefix should be mapped to the "http://www.xomega.net/framework" namespace.
  3. Set the namespace attribute of that element to your custom namespace for the generated enumeration classes that contain the constant strings.
  4. Open the Properties tab of the Enumeration Constants generator in the Static Data folder.
  5. Set the Output Path parameter to where you want the generated class to go. We recommend using a specific naming convention for the generated files, such as ending them with .gen.cs or starting with an underscore, which would keep them separate from the non-generated files.
  6. If you want the generator to automatically add the generated file to one of your projects, then you need to specify the path to the project file in the Add To Project parameter.
  7. If you want to additionally add links to the generated files into one of the projects, then you can set the Link Into Project attribute to point to that project's file. This is useful for Silverlight solutions, where you cannot simply reference a project that is not built for Silverlight from another Silverlight project, but you can link the files into that project to use the same code base.
  8. If you need to link the generated files into another project as described in the previous step, you can also specify a virtual folder in the project structure to group the linked files by setting the Link Path attribute.
  9. Run the generator and add or link the generated files to your project as necessary.
  10. Review and build the project to make sure the files were generated properly and without errors.

The following example shows how to specify a custom namespace for the generated enumeration classes that contain the constant enumeration strings.
<config xmlns="http://www.xomega.net/omodel" xmlns:xfk="http://www.xomega.net/framework">
  <xfk:enumerations namespace="MyProject.Enumerations"/>

After you generate the constant strings, you will be able to access them from your code as follows.
LookupCache cache = LookupCache.Get(LookupCache.Global);
LookupTable tbl = cache.GetLookupTable(Enumerations.Operators.EnumName);
Header oper = tbl.LookupById(Enumerations.Operators.Contains);
object isMultival = oper[Enumerations.Operators.Attributes.Multival];
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.