tags:

views:

32

answers:

1

So for creating structured XML where the structure is defined via a class structure of some sort. Is there a nice design pattern for wrapping the language so that only defined operations can happen?

Each item contains their own XElement)

  • MSBuildDoc (the doc generator)
  • MSBuildTarget(a target)
  • MSBuildMessageTask
  • MsBuildCallTask
  • MsBuildBuildTask
  • MSBuildTaskOutputElement

I'm trying to construct a framework such that orphaning is not possible, only valid combinations work, valid combinations are walkable from an instance of an item.

The interdependence comes in the fact that using Add(common interface or superclass), or AddAfterSelf(common interface or superclass), etc.. methods requires the child/sibling to already be created. If the constructors are public then you can directly create an orphan.

Examples:

var msb=new MSBuildDoc();
var mainTarget=new MSBuildtarget(); //created as orphan
msb.Add(mainTarget); // add is free to accept anything even invalid child types

Using Create methods(static or not) would directly create orphans.

Using AddSubType methods requires implementation details of the subType (what is needed to construct it) to live on the parent.

Examples:

var x=new MSBuildDoc();
var mainTarget=x.AddTarget("Main") //AddTarget method is defined in the parent class
  .SetAttributeFluent("BuildInParallel","true");
mainTarget.AddBuildTask(alltargets);

I realize that making an exhaustive wrapper is nearly pointless and ridiculous. I am trying to wrap the very few parts that I use in my project so that the core class is very readable and very maintainable.

A: 

You could simply require that the constructor of each type requires a parameter that is the parent node:

var msb = new MSBuildDoc(); 
var mainTarget = new MSBuildtarget( msb ); // constructor calls msb.Add(m_XElem)

This pattern is not uncommon when you want to avoid orphaned objects. Alternatively, you could use a full-fledged builder pattern, and have type build and return their potential children.

LBushkin