in

Community

Take part in the community surrounding CodeBreeze and other products from SteelBlue Solutions.

CodeBreeze Blog

Using Target Tags to identify parent-child relationships

CodeBreeze is not marketed as an ORM product and thus does not allow the identification of a parent-child relationship in the entity list.  It especially does not map this from database foreign-key information as more often than not, foreign key setup in a database is not a parent-child definition as many think it is.  It is merely a case of normalization and hierarchical representations.  Instead, Codebreeze being a full-featured code-generator supplies an answer to this dillemma using a different technique.  CodeBreeze allows you do define what's known as target-tags at the project template level.  Target-tags are simple strings that are defined in a project template and later attached to either business entities or query wrappers in a CodeBreeze Project.  You can think of this "attachment" as decorating classes with attributes.  And like .NET attributes, they are only as useful as that which calls upon them.

The CSLA Project Templates that ship with CodeBreeze define target tags called Parent, Child, IsParent, and IsChild.  To take advantage of these in a parent-child scenario within your generated code, you would create your CodeBreeze Project based on one of the CSLA Project Templates as you have probably been doing so already.  You then go to the Project menu and select the "Assign Target Tags" option.  The screen that comes up allows you to see a list of your business entities or your query wrapper, though I'm willing to say that more often than not you would assign target tags to business entities.  You'll see that everytime you select an entity, you'll see the list of available target tags with any possible values.  All target-tag values are simply string entries.  As of right now, you can only see target-tags for a selected business entity using this screen, but an upcoming CodeBreeze release will include a screen that will pivot this information; allowing you to select a target-tag and see their values for all entities in a single list.  There will also be an icon indicator in the project explorer in the future that will tell you that element in the project has one or more target tag values set.

A logical use for the target-tags defined in the CSLA project templates would be a situation where you have a business entity called Order and one called OrderDetail.  You would set the value of the IsParent target tag of the Order entity to the word "true" and the Child target tag of the Order entity to the word "OrderDetail".  Then you would set the value of the IsChild target tag of the OrderDetail to "true" and the Parent target tag to "Order".

This is a pattern that you would repeat for any other relationships you may have.  It's even possible to use both the IsParent and IsChild target tag on an entity if you have a parent-child-grandchild scenario, though getting to that level more than likely indicates you may have too complex a design.

Target-tag values on any project member are accessible from a code-template.  This is illustrated by the CSLA code templates that use the CSLA base classes, BusinessBase, BusinessListBase, and EditableRootListBase.

If you have an object defined in a code template of type CodeBreeze.Business.BusinessEntity to indicate the current entity for which you are currently generating code (say in a variable called _Entity), you can access the value of a target-tag by saying: _Entity.Tags("IsChild").Value.

Note that I'm using VB syntax here so of course, square brackets would have to be used in C#.  If you didn't set a value for a specific target-tag, this would simply return an empty string.  Also note that since all target-tags are string values, if you want to express a boolean, you would need to set the value to the string "true" or "false" and then perform a Convert.ToBoolean in the code template.  The CSLA code-templates check for these values and set appropriate simple variables in the InitializeVariables method of the code-templates code-behind class.

Later, throughout the code template you can use these values to determine what code gets generated.  In the case of the CodeBreeze CSLA templates, you'll see usage of IsChild and IsParent variables throughout to determine generated output.

If you have a situation where one parent entity may support more than one possible child entity type, you have define this using one target-tag.  Since target-tags can only have one string value for each entity, you would define your values using a delimiter then parse it out in the code-template later.

The target-tag approach keeps CodeBreeze from becoming "just another ORM" and also potentially solve many other problems besides parent-child relationship.  You can define a target-tag for anything you think you may need, then assign it and use it in a code template if you wish to alter the generated code at an entity-basis.

We hope this blog-entity helps you in deciphering how target-tags work and how they can be used to handle parent-child relationships.

Remember, CodeBreeze ships with CSLA templates that support the latest CSLA release (3.6).  Code-templates for previous CSLA versions still ship with the product but are NOT supported and will not continue to be enhanced.  Only the latest code-templates will be fixed or enhanced continuously.

As always, thanks for using CodeBreeze !

 

Only published comments... Mar 28 2009, 06:54 PM by miguel

Comments

 

Mark said:

Seems by going for ultimate flexibility with the string based tag and delimiters for N values, you lose any ability to "strongly type" the values; e.g. since the mapper just knows it's a string, and that's it, it cannot help when you type in "Oders" instead of "Orders".  Unless there's a hook for validating these tags when they're defined...

Just thinking out loud... hope it is helpful.

April 2, 2009 12:59 PM

Leave a Comment

(required)  
(optional)
(required)  
Add
2008 - All rights reserved
Powered by Community Server (Commercial Edition), by Telligent Systems