new operator with deep class structure

O

olympus_mons

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...
 
P

PvdG42

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...

Are your classes related via inheritance? I.e. is Sub1 a subclass of Top and
are Sub1_1 and Sub1_2 subclasses of Sub1?
Are you actually trying to instantiate all the classes (Top, Sub1, Sub1_1
and Sub1_2), or are you just trying to instantiate the subclasses (Sub1_1
and Sub1_2)?
I'm asking because your example:
<quote>
just do:
Top top = new Top(true);

which will create instances for all sub-elements.
</quote>
seems to indicate that you don't understand how inheritance works.
In the example above, instantiating Sub1_1 calls constructors for Top, Sub1
and Sub1_1 in that order, creating a "complete" instance of Sub1_1.
Unless you have a specific need for the superclass instances as well as
Sub1_1 and Sub1_2 instances, there's no point in creating them.
If you implement your Top top = new Top(true), do you plan to add parameters
to indicate how many instances of each subclass to create?

If I've misunderstood your post, please excuse these ramblings.
 
O

olympus_mons

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
 
P

Peter Duniho

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).


I agree with the person posting as "PvdG42" that your question is confusing.

Terms like "class hierarchy" and "sub-class" do imply that you're using
inheritance. On the other hand, inheritance relates to the classes
themselves, not the instances of the classes. As "PvdG42" says, you
don't need to instantiate any of the base classes in order to
instantiate the most-derived class in that hierarchy chain.

Your example seems to reinforce that this is in fact what you're talking
about.

However, it seems that there may be a possibility that you aren't
actually dealing with a class hierarchy, but instead a data hierarchy.
In which case, instantiating multiple instances based on your data may
actually make sense.

This is also consistent with your claim that you are new to C#.
Creating new classes programmatically is not the sort of thing someone
new to C# would be likely to be doing, but creating a hierarchical data
tree is.

Perhaps you can clarify what the actual scenario is. In particular, are
you actually creating classes dynamically, in which you have something
that would look something like this:

class Top {}
class Sub1 : Top {}
class Sub1_1 : Sub1 {}

etc.

If not, then you aren't actually creating a class hierarchy, nor are you
creating sub-classes, in spite of what you wrote.

Pete
 
O

olympus_mons

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 BalanceLiabilities liabilitiesField;

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

public partial class BalanceGeneral
{
private System.DatTime effectiveDateField;
private int numberOfEmployeesField;
...
// followed by correspondig get/set property methods
public DateTime effectiveDate
{
// get/set for effectiveDateField
}
...
}

public partial class BalanceAssets
{
...
private BalanceAssetsStocks 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 "constructed".
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 BalanceAssetsStocks();
balance.assests.stocks.subMember1 = new
BalanceAssetsStocksSubMember1();
....
balance.assests.stocks.subMember1.subMember11.subMember111.subMember1111.someValue
= 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.subMember1.subMember11.subMember111.subMember1111.someValue
= 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
 
P

Peter Duniho

[...]
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 overcomplicating 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.subMember1.subMember11.subMember111.subMember1111.someValue
= 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
 
O

olympus_mons

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
 
G

Garfilone

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.
 
O

olympus_mons

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top