new operator with deep class structure

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • olympus_mons@gmx.de

    #1

    new operator with deep class structure

    Hi,

    I generated C# classes from some complex XMLSchemas usind xsd.exe. The
    result is that I get a class hierarchy that is quite deep (well for me
    8 levels are deep). What I'm curiuos about is, that if I create an
    instance of my top level element I still need to create instances of
    all sub-elements. What would be the best way to do some sort of "deep
    new" operator, that recursively creates instances for all sub-classes
    (the complete structure consists of 89 different classes).

    Lets say my class structure is like
    Top
    +-->Sub1
    +-->Sub1_1
    +-->Sub1_2
    +-->Sub2
    +-->Sub2_1
    +-->Sub2_2

    and instead of:
    Top top = new Top();
    top.sub1 = new Sub1();
    top.sub1.sub1_1 = new Sub1_1();
    top.sub1.sub1_2 = new Sub1_2();
    ....

    just do:
    Top top = new Top(true);

    which will create instances for all sub-elements.

    Of couse I could code a creator method that just does this. But maybe
    this is not the best approach. So what would a experienced C#-guy do?
    Any idea?

    Thanks,
    Stefan

    P.S.: You probably noticed that I must be new to C#/FW2.0...

  • olympus_mons@gmx.de

    #2
    Re: new operator with deep class structure

    Yes - you are right, I did not make this point very clear. The top
    level class contains members which are of the sub class types. And the
    sub-level members in turn contain members which are of typ sub-sub-
    level class and so on. Sorry about the confusion.

    Stefan

    Comment

    • olympus_mons@gmx.de

      #3
      Re: new operator with deep class structure

      Pete and "PvdG42",

      just to make it clear I provide a sample of what XSD.EXE created from
      the XMLSchemas:

      My top class is "Balance":

      public partial class Balance
      {
      private BalanceGeneral generalField;
      private BalanceAssets assetsField;
      private BalanceLiabilit ies liabilitiesFiel d;

      // followed by correspondig get/set property methods
      public BalanceGeneral general
      {
      get
      {
      return this.generalFie ld;
      }
      set
      {
      this.generalFie ld = value;
      }
      }
      public BalanceAssets assets
      {
      // get/set for assetsField
      }
      }

      public partial class BalanceGeneral
      {
      private System.DatTime effectiveDateFi eld;
      private int numberOfEmploye esField;
      ...
      // followed by correspondig get/set property methods
      public DateTime effectiveDate
      {
      // get/set for effectiveDateFi eld
      }
      ...
      }

      public partial class BalanceAssets
      {
      ...
      private BalanceAssetsSt ocks stocksField;
      ...
      }

      Again: there is no inheritance involved. Each class consist of member
      fields that are either simple types (as DateTime, int or decimal) or
      class types like BalanceGeneral or BalanceAssets. I have 89 classes
      and about 160 simple type member fields (mostly of type decimal, the
      "leaves" of my "balance tree") from which a balance is "constructe d".
      This tree like structure is at most 8 levels deep.

      Now to fill in the effective date of a balance I need to create the
      appropriate class instances for all members in the path to the "leaf"
      effectiveDate:

      Balance balance = new Balance();
      balance.general = new BalanceGeneral( );
      balance.general .effectiveDate = DateTime.Today( );


      In the case of some decimal value in the path down to balance assets
      this would be:

      balance.assets = new BalanceAssets() ;
      balance.assests .stocks = new BalanceAssetsSt ocks();
      balance.assests .stocks.subMemb er1 = new
      BalanceAssetsSt ocksSubMember1( );
      ....
      balance.assests .stocks.subMemb er1.subMember11 .subMember111.s ubMember1111.so meValue
      = 1000;

      What I want to know If there is some "mechanism" so that I can avoid
      all those "new" operators. I just do a "deep new" on the top level
      class and all other instances for the sub-member fields are created,
      too. Now I could code a constructor for my top level class Balance
      that would do just this or a constructor for each of the 89 classes.
      But as I said, this "bunch of classes" is generated from XMLSchemas.
      If there is a future change in one of the schemas I will have to
      carefully adapt my contructors. And more worse there are three other
      data trees besides "Balance" for which I have to generate classes from
      the corresponding XML schemas. So that's why I'm asking if there is
      some way to automate this process, so that I can just code:

      Balance balance = new Balance(true); // this is the constructor that
      will create all member instances
      balance.general .effectiveDate = DateTime.Today( );
      balance.assests .stocks.subMemb er1.subMember11 .subMember111.s ubMember1111.so meValue
      = 1000;

      So before I start to code this constructor I wanted to ask some C#
      expert if there is another approach that I didn't think of.
      Stefan

      Comment

      • Peter Duniho

        #4
        Re: new operator with deep class structure

        olympus_mons@gm x.de wrote:
        [...]
        What I want to know If there is some "mechanism" so that I can avoid
        all those "new" operators. I just do a "deep new" on the top level
        class and all other instances for the sub-member fields are created,
        too.
        Well, what's your intent with respect to populating the contained classes?

        For contained class instances, you must do _some_ initialization
        explicitly. There's no automatic way for that to happen. By default
        the reference to the contained instance is null, and it doesn't get to
        be non-null unless you write some code that does that explicitly.

        However, if you have some reasonable natural way to instantiate the
        top-level node, then surely you also have some natural way to
        instantiate the contained nodes, all the way down your containment tree.
        For example, presumably you aren't instantiating the root node without
        data to use to initialize it. In the same way, shouldn't you have data
        before you go instantiating the contained nodes? And if so, wouldn't
        the natural method be to simply go through the data as it exists,
        initializing the relevant data structures as necessary?

        I'm a bit confused by what appears to be an intent on your part to
        initialize data structures for which you have no data yet. While I
        think that I do finally understand the basic scenario you're dealing
        with, it seems to me that the design requirement that the entire
        containment tree be completely populated all at once is unnecessary and
        is overcomplicatin g the implementation.
        [...]
        If there is a future change in one of the schemas I will have to
        carefully adapt my contructors.
        If there is a future change, won't you have to change all of your code?
        For example:

        And more worse there are three other
        data trees besides "Balance" for which I have to generate classes from
        the corresponding XML schemas. So that's why I'm asking if there is
        some way to automate this process, so that I can just code:
        >
        Balance balance = new Balance(true); // this is the constructor that
        will create all member instances
        balance.general .effectiveDate = DateTime.Today( );
        balance.assests .stocks.subMemb er1.subMember11 .subMember111.s ubMember1111.so meValue
        = 1000;
        The code above depends on the containment tree. If the schema changes,
        that will presumably change the fields within the Balance class,
        requiring you to change any code that refers to them. Thus, any code
        referring to the properties "general" and "assets" needs to be changed
        to address that change.

        Basically, any change that would require some manual change to the way
        contained members are initialized will also require some manual change
        to the way they are accessed, and vice a versa. Trying to automatically
        instantiate the entire tree with empty instances is not only likely to
        be bad design, it's also solving a fairly minimal part of the overall
        problem.

        Pete

        Comment

        • olympus_mons@gmx.de

          #5
          Re: new operator with deep class structure

          Pete,

          thanks for taking time and sharing your thoughts.

          The data comes from a database and needs to be filled into the data
          structure. Then the data is serialized to XML and send to a web
          service. The data structure in the database is "flat, although I have
          created a key table that contains information about parent/child
          relationship for balance elements. It also contains XMLSchema element
          names and types (these correspond to C# class and property names). I
          did this just for informational purposes but now I used this to
          generate C# code for my "deep" constructor. The whole process is to
          some degree automated by SQL queries that result in C# code. So now I
          can create my data structure classes using XSD.EXE and add a "deep"
          constructor. The amount of manually coding all this is minimized to a
          bit of copy & paste. My solution is now ready to be apllied to the
          other XMLschema objects. The C# code that loops through the balance
          data and fills in the "flat" data into the data tree is also
          generated. Thus any change in the XMLSchema can quickly be turned into
          new code.

          I don't know if this is really the best solution but it works for me.

          Stefan

          Comment

          • Garfilone

            #6
            Re: new operator with deep class structure

            If you have a 8-level hierarchy tree, i think you'd better consider
            more on your design. Inheritance is one of the core features of the OO
            Language, but not recommended.

            olympus_m...@gm x.de wrote:
            Hi,
            >
            I generated C# classes from some complex XMLSchemas usind xsd.exe. The
            result is that I get a class hierarchy that is quite deep (well for me
            8 levels are deep). What I'm curiuos about is, that if I create an
            instance of my top level element I still need to create instances of
            all sub-elements. What would be the best way to do some sort of "deep
            new" operator, that recursively creates instances for all sub-classes
            (the complete structure consists of 89 different classes).
            >
            Lets say my class structure is like
            Top
            +-->Sub1
            +-->Sub1_1
            +-->Sub1_2
            +-->Sub2
            +-->Sub2_1
            +-->Sub2_2
            >
            and instead of:
            Top top = new Top();
            top.sub1 = new Sub1();
            top.sub1.sub1_1 = new Sub1_1();
            top.sub1.sub1_2 = new Sub1_2();
            ...
            >
            just do:
            Top top = new Top(true);
            >
            which will create instances for all sub-elements.
            >
            Of couse I could code a creator method that just does this. But maybe
            this is not the best approach. So what would a experienced C#-guy do?
            Any idea?
            >
            Thanks,
            Stefan
            >
            P.S.: You probably noticed that I must be new to C#/FW2.0...

            Comment

            • olympus_mons@gmx.de

              #7
              Re: new operator with deep class structure

              Garfilone,

              again, this is not a class hierarchy based on inheritance, it's a data
              hierarchy based on a bunch of classes. As these classes are generated
              from an XMLSchema using XSD.EXE I have no influence on the desgin.
              Also I think this quite normal with XMLSchema based data structures.

              Stefan

              Comment

              Working...