By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,852 Members | 994 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,852 IT Pros & Developers. It's quick & easy.

Simple Inheritance : Creating derived classes from instances of base class.

P: n/a
Hi,
How Do I create an instance of a derived class from an instance of a
base class, essentially wrapping up an existing base class with some
additional functionality. The reason I need this is because I am not
always able to control/create all the different constructors the base
class has. My problem can be described in code as follows ...

/* This is the base class with a whole heap of
constructors/functionality*/
public class Animal
{
Public Animal()
{/**/}
public Animal(int x)
{/**/}

}

/* This is the derived class that adds some functionality to the base*/
public class TaggedAnimal : Animal

{
Public string GetTag()
{/**/}

}
In my main method I have a whole heap of Animal classes that are
generated. I would like to convert them to TaggedAnimals and start
using them. I don't want to just use another method/class to implement
the GetTag() functionality for a given Animal (using animal as a member
of another class type) because there are other controls/classes/methods
that expect an Animal Type and I would like to pass the same object to
them as well.

So the main code would look something like this :

public class MyMainClass
{
public void MyMainMethod()
{
/* Animals get constructed here, I don't have much control here and
don't want to.*/
System.Collections.Arraylist allAnimals =
ThirdPartyORMTool.GetAllAnimals();

/* But once I have the instances, I would like to inherit from them
to add my own functionality. The TagAllAnimals method
casts/constructs/creates the instances of Animals into instances of
TaggedAnimals */
System.Collections.Arraylist allTaggedAnimals =
TagAllAnimals(allAnimals);
/* Now I can use the tagged animal for various controls to
control/display any functionality in the base or the derived class*/
Gridview.DataSource = allTaggedAnimals;

/* And I can also use the same collection for any others that expect
an Animal class (the base class) */
ThirdPartyORMTool.SaveAnimals(allTaggedAnimals);
ThirdPartyORMTool.DoSomeOtherAnimalRelatedStuff(al lTaggedAnimals);
}

}

How would I go about doing this? Basically, my first instinct was to do
something like this

public class TaggedAnimal : Animal

{

/* Pass an instance of the base class to the constructor */
public TaggedAnimal(Animal currentAnimal)
{
/* Set the base class to the instance of the base class that was
passed in*/
base = currentAnimal;
}

Public string GetTag()
{/**/}

}
but in C# .net 2.0, it complains that "base is a not valid in this
context".

I might be missing something trivial here, but how do I set/control the
instance of the base class without explicitly having to create the base
class by calling its constructor with the required parameters. Or by
creating the derived class to begin with.

Any Help is much appreciated.

Jan 19 '07 #1
Share this Question
Share on Google+
26 Replies


P: n/a
<ny********@hotmail.comwrote in message
news:11*********************@a75g2000cwd.googlegro ups.com...
Hi,
How Do I create an instance of a derived class from an instance of a
base class, essentially wrapping up an existing base class with some
additional functionality. The reason I need this is because I am not
always able to control/create all the different constructors the base
class has. My problem can be described in code as follows ...

/* This is the base class with a whole heap of
constructors/functionality*/
public class Animal
{
Public Animal()
{/**/}
public Animal(int x)
{/**/}

}
I would create a copy constructor in the base Animal class that takes in an
Animal and sets all the data in 'this' with the values from the passed in
instance of Animal.

public Animal(Animal copy)
{
this.x = Animal.GetX();
// etc
}
/* This is the derived class that adds some functionality to the base*/
public class TaggedAnimal : Animal

{
Public string GetTag()
{/**/}

}
Then add a constructor to TaggedAnimal that takes in an Animal.

public TaggedAnimal(Animal an) : base (an)
In my main method I have a whole heap of Animal classes that are
generated. I would like to convert them to TaggedAnimals and start
using them. I don't want to just use another method/class to implement
the GetTag() functionality for a given Animal (using animal as a member
of another class type) because there are other controls/classes/methods
that expect an Animal Type and I would like to pass the same object to
them as well.

So the main code would look something like this :

public class MyMainClass
{
public void MyMainMethod()
{
/* Animals get constructed here, I don't have much control here and
don't want to.*/
System.Collections.Arraylist allAnimals =
ThirdPartyORMTool.GetAllAnimals();

/* But once I have the instances, I would like to inherit from them
to add my own functionality. The TagAllAnimals method
casts/constructs/creates the instances of Animals into instances of
TaggedAnimals */
System.Collections.Arraylist allTaggedAnimals =
TagAllAnimals(allAnimals);
In here, for each instance of Animal in allAnimals you would create a
TaggedAnimal, passing in the Animal to the constructor.
/* Now I can use the tagged animal for various controls to
control/display any functionality in the base or the derived class*/
Gridview.DataSource = allTaggedAnimals;

/* And I can also use the same collection for any others that expect
an Animal class (the base class) */
ThirdPartyORMTool.SaveAnimals(allTaggedAnimals);
ThirdPartyORMTool.DoSomeOtherAnimalRelatedStuff(al lTaggedAnimals);

How would I go about doing this? Basically, my first instinct was to do
something like this

public class TaggedAnimal : Animal

{

/* Pass an instance of the base class to the constructor */
public TaggedAnimal(Animal currentAnimal)
{
/* Set the base class to the instance of the base class that was
passed in*/
base = currentAnimal;
}
You can't assign to the base instance like this. You could assign
properties in base based on the same property value in currentAnimal, or add
the copy constructor to Animal as I have stated above.
Public string GetTag()
{/**/}

}
but in C# .net 2.0, it complains that "base is a not valid in this
context".

I might be missing something trivial here, but how do I set/control the
instance of the base class without explicitly having to create the base
class by calling its constructor with the required parameters. Or by
creating the derived class to begin with.
You can't. Once you are within your derived classes constructor, your base
instance is already created. You can't assign a new base instance.
--
Tom Porterfield

Jan 19 '07 #2

P: n/a
Tom Porterfield wrote:
<ny********@hotmail.comwrote in message
news:11*********************@a75g2000cwd.googlegro ups.com...
Hi,
How Do I create an instance of a derived class from an instance of a
base class, essentially wrapping up an existing base class with some
additional functionality. The reason I need this is because I am not
always able to control/create all the different constructors the base
class has. My problem can be described in code as follows ...

/* This is the base class with a whole heap of
constructors/functionality*/
public class Animal
{
Public Animal()
{/**/}
public Animal(int x)
{/**/}

}

I would create a copy constructor in the base Animal class that takes in an
Animal and sets all the data in 'this' with the values from the passed in
instance of Animal.

public Animal(Animal copy)
{
this.x = Animal.GetX();
// etc
}
/* This is the derived class that adds some functionality to the base*/
public class TaggedAnimal : Animal

{
Public string GetTag()
{/**/}

}

Then add a constructor to TaggedAnimal that takes in an Animal.

public TaggedAnimal(Animal an) : base (an)
Unfortunately I can't create a copy constructor. Thats why i said in
the original post "because I am not always able to control/create all
the different constructors the base class has.". The problem is that
the base classes are auto generated for me in the data access layer by
the ORM tool. I don't want to modify auto generated code. And
modifying/tweaking the ORM to add custom code doesn't seem very elegant
either.
In my main method I have a whole heap of Animal classes that are
generated. I would like to convert them to TaggedAnimals and start
using them. I don't want to just use another method/class to implement
the GetTag() functionality for a given Animal (using animal as a member
of another class type) because there are other controls/classes/methods
that expect an Animal Type and I would like to pass the same object to
them as well.

So the main code would look something like this :

public class MyMainClass
{
public void MyMainMethod()
{
/* Animals get constructed here, I don't have much control here and
don't want to.*/
System.Collections.Arraylist allAnimals =
ThirdPartyORMTool.GetAllAnimals();

/* But once I have the instances, I would like to inherit from them
to add my own functionality. The TagAllAnimals method
casts/constructs/creates the instances of Animals into instances of
TaggedAnimals */
System.Collections.Arraylist allTaggedAnimals =
TagAllAnimals(allAnimals);

In here, for each instance of Animal in allAnimals you would create a
TaggedAnimal, passing in the Animal to the constructor.
/* Now I can use the tagged animal for various controls to
control/display any functionality in the base or the derived class*/
Gridview.DataSource = allTaggedAnimals;

/* And I can also use the same collection for any others that expect
an Animal class (the base class) */
ThirdPartyORMTool.SaveAnimals(allTaggedAnimals);
ThirdPartyORMTool.DoSomeOtherAnimalRelatedStuff(al lTaggedAnimals);

How would I go about doing this? Basically, my first instinct was to do
something like this

public class TaggedAnimal : Animal

{

/* Pass an instance of the base class to the constructor */
public TaggedAnimal(Animal currentAnimal)
{
/* Set the base class to the instance of the base class that was
passed in*/
base = currentAnimal;
}

You can't assign to the base instance like this. You could assign
properties in base based on the same property value in currentAnimal, or add
the copy constructor to Animal as I have stated above.
Public string GetTag()
{/**/}

}
but in C# .net 2.0, it complains that "base is a not valid in this
context".

I might be missing something trivial here, but how do I set/control the
instance of the base class without explicitly having to create the base
class by calling its constructor with the required parameters. Or by
creating the derived class to begin with.

You can't. Once you are within your derived classes constructor, your base
instance is already created. You can't assign a new base instance.
That does seem to be the case, but can we intercept it before the
derived constructor is called and pass it a new base somehow, rather
than letting the CLR instantiate a new base. Something along the lines
of

public TaggedAnimal(Animal currentAnimal) : base = currentAnimal
{

}

I would prefer not to use reflection as it will be expensive to run
over a collection of objects

--
Tom Porterfield
Jan 19 '07 #3

P: n/a

ny********@hotmail.com wrote:
Hi,
How Do I create an instance of a derived class from an instance of a
base class, essentially wrapping up an existing base class with some
additional functionality. The reason I need this is because I am not
always able to control/create all the different constructors the base
class has. My problem can be described in code as follows ...

/* This is the base class with a whole heap of
constructors/functionality*/
public class Animal
{
Public Animal()
{/**/}
public Animal(int x)
{/**/}

}

/* This is the derived class that adds some functionality to the base*/
public class TaggedAnimal : Animal

{
Public string GetTag()
{/**/}

}
In my main method I have a whole heap of Animal classes that are
generated. I would like to convert them to TaggedAnimals and start
using them. I don't want to just use another method/class to implement
the GetTag() functionality for a given Animal (using animal as a member
of another class type) because there are other controls/classes/methods
that expect an Animal Type and I would like to pass the same object to
them as well.

So the main code would look something like this :

public class MyMainClass
{
public void MyMainMethod()
{
/* Animals get constructed here, I don't have much control here and
don't want to.*/
System.Collections.Arraylist allAnimals =
ThirdPartyORMTool.GetAllAnimals();

/* But once I have the instances, I would like to inherit from them
to add my own functionality. The TagAllAnimals method
casts/constructs/creates the instances of Animals into instances of
TaggedAnimals */
System.Collections.Arraylist allTaggedAnimals =
TagAllAnimals(allAnimals);
/* Now I can use the tagged animal for various controls to
control/display any functionality in the base or the derived class*/
Gridview.DataSource = allTaggedAnimals;

/* And I can also use the same collection for any others that expect
an Animal class (the base class) */
ThirdPartyORMTool.SaveAnimals(allTaggedAnimals);
ThirdPartyORMTool.DoSomeOtherAnimalRelatedStuff(al lTaggedAnimals);
}

}

How would I go about doing this? Basically, my first instinct was to do
something like this

public class TaggedAnimal : Animal

{

/* Pass an instance of the base class to the constructor */
public TaggedAnimal(Animal currentAnimal)
{
/* Set the base class to the instance of the base class that was
passed in*/
base = currentAnimal;
}

Public string GetTag()
{/**/}

}
but in C# .net 2.0, it complains that "base is a not valid in this
context".

I might be missing something trivial here, but how do I set/control the
instance of the base class without explicitly having to create the base
class by calling its constructor with the required parameters. Or by
creating the derived class to begin with.

Any Help is much appreciated.
So... if I understand you correctly, you want to add a certain piece of
functionality across all of an existing class hierarchy over which you
have little (or no control)?

I swear that there's a Design Pattern for that, but it slips my mind.

Anyone...?

Jan 19 '07 #4

P: n/a
I had a quick look at the GOF book, to no avail....

Bruce Wood wrote:
ny********@hotmail.com wrote:
Hi,
How Do I create an instance of a derived class from an instance of a
base class, essentially wrapping up an existing base class with some
additional functionality. The reason I need this is because I am not
always able to control/create all the different constructors the base
class has. My problem can be described in code as follows ...

/* This is the base class with a whole heap of
constructors/functionality*/
public class Animal
{
Public Animal()
{/**/}
public Animal(int x)
{/**/}

}

/* This is the derived class that adds some functionality to the base*/
public class TaggedAnimal : Animal

{
Public string GetTag()
{/**/}

}
In my main method I have a whole heap of Animal classes that are
generated. I would like to convert them to TaggedAnimals and start
using them. I don't want to just use another method/class to implement
the GetTag() functionality for a given Animal (using animal as a member
of another class type) because there are other controls/classes/methods
that expect an Animal Type and I would like to pass the same object to
them as well.

So the main code would look something like this :

public class MyMainClass
{
public void MyMainMethod()
{
/* Animals get constructed here, I don't have much control here and
don't want to.*/
System.Collections.Arraylist allAnimals =
ThirdPartyORMTool.GetAllAnimals();

/* But once I have the instances, I would like to inherit from them
to add my own functionality. The TagAllAnimals method
casts/constructs/creates the instances of Animals into instances of
TaggedAnimals */
System.Collections.Arraylist allTaggedAnimals =
TagAllAnimals(allAnimals);
/* Now I can use the tagged animal for various controls to
control/display any functionality in the base or the derived class*/
Gridview.DataSource = allTaggedAnimals;

/* And I can also use the same collection for any others that expect
an Animal class (the base class) */
ThirdPartyORMTool.SaveAnimals(allTaggedAnimals);
ThirdPartyORMTool.DoSomeOtherAnimalRelatedStuff(al lTaggedAnimals);
}

}

How would I go about doing this? Basically, my first instinct was to do
something like this

public class TaggedAnimal : Animal

{

/* Pass an instance of the base class to the constructor */
public TaggedAnimal(Animal currentAnimal)
{
/* Set the base class to the instance of the base class that was
passed in*/
base = currentAnimal;
}

Public string GetTag()
{/**/}

}
but in C# .net 2.0, it complains that "base is a not valid in this
context".

I might be missing something trivial here, but how do I set/control the
instance of the base class without explicitly having to create the base
class by calling its constructor with the required parameters. Or by
creating the derived class to begin with.

Any Help is much appreciated.

So... if I understand you correctly, you want to add a certain piece of
functionality across all of an existing class hierarchy over which you
have little (or no control)?

I swear that there's a Design Pattern for that, but it slips my mind.

Anyone...?
Jan 19 '07 #5

P: n/a
"Bruce Wood" <br*******@canada.comschrieb im Newsbeitrag
news:11**********************@v45g2000cwv.googlegr oups.com...
>
ny********@hotmail.com wrote:
>Hi,
How Do I create an instance of a derived class from an instance of a
base class, essentially wrapping up an existing base class with some
additional functionality. The reason I need this is because I am not
always able to control/create all the different constructors the base
class has. My problem can be described in code as follows ...

/* This is the base class with a whole heap of
constructors/functionality*/
public class Animal
{
Public Animal()
{/**/}
public Animal(int x)
{/**/}

}

/* This is the derived class that adds some functionality to the base*/
public class TaggedAnimal : Animal

{
Public string GetTag()
{/**/}

}
In my main method I have a whole heap of Animal classes that are
generated. I would like to convert them to TaggedAnimals and start
using them. I don't want to just use another method/class to implement
the GetTag() functionality for a given Animal (using animal as a member
of another class type) because there are other controls/classes/methods
that expect an Animal Type and I would like to pass the same object to
them as well.

So the main code would look something like this :

public class MyMainClass
{
public void MyMainMethod()
{
/* Animals get constructed here, I don't have much control here and
don't want to.*/
System.Collections.Arraylist allAnimals =
ThirdPartyORMTool.GetAllAnimals();

/* But once I have the instances, I would like to inherit from them
to add my own functionality. The TagAllAnimals method
casts/constructs/creates the instances of Animals into instances of
TaggedAnimals */
System.Collections.Arraylist allTaggedAnimals =
TagAllAnimals(allAnimals);
/* Now I can use the tagged animal for various controls to
control/display any functionality in the base or the derived class*/
Gridview.DataSource = allTaggedAnimals;

/* And I can also use the same collection for any others that expect
an Animal class (the base class) */
ThirdPartyORMTool.SaveAnimals(allTaggedAnimals) ;
ThirdPartyORMTool.DoSomeOtherAnimalRelatedStuff(a llTaggedAnimals);
}

}

How would I go about doing this? Basically, my first instinct was to do
something like this

public class TaggedAnimal : Animal

{

/* Pass an instance of the base class to the constructor */
public TaggedAnimal(Animal currentAnimal)
{
/* Set the base class to the instance of the base class that was
passed in*/
base = currentAnimal;
}

Public string GetTag()
{/**/}

}
but in C# .net 2.0, it complains that "base is a not valid in this
context".

I might be missing something trivial here, but how do I set/control the
instance of the base class without explicitly having to create the base
class by calling its constructor with the required parameters. Or by
creating the derived class to begin with.

Any Help is much appreciated.

So... if I understand you correctly, you want to add a certain piece of
functionality across all of an existing class hierarchy over which you
have little (or no control)?

I swear that there's a Design Pattern for that, but it slips my mind.

Anyone...?
The only way, this can be done is by either:

- add the method to the code of the base class (wich surely is only
possible, if one has access to that code.)

- use a static method, that has a first parameter of type Animal. That does
the same, with some restrictions: It can't be used in delegate creation as a
insctance method, with an instance expression of Animal. But in C# 2.0 this
can be solved by an anonymous method wich catches a variable of type Animal.

In C# 3.0 this method could be declared as extension method, so it can be
called as if it were an instance method.

Christof
Jan 19 '07 #6

P: n/a
Would you be willing to wrap your Animal objects in TaggedAnimal clothing?
In other words your TaggedAnimal would contain an Animal. This permits you
to reference the tag properties/methods through the TA reference but still
reference Animal properties via the Animal object it contains.

myAnimal.Tag()
myAnimal.Animal.AnimalProperty

The collection would work the same way, methods you wrote are in the
TaggedAnimals collection which contains a collection of Animals. You could
even hide your ThirdPartyORMTool methods in the TaggedAnimal Collection
class.

TaggedAnimalCollection.GetAllAnimals() would fill the inner collection.
<ny********@hotmail.comwrote in message
news:11*********************@s34g2000cwa.googlegro ups.com...
>I had a quick look at the GOF book, to no avail....

Bruce Wood wrote:
>ny********@hotmail.com wrote:
Hi,
How Do I create an instance of a derived class from an instance of a
base class, essentially wrapping up an existing base class with some
additional functionality. The reason I need this is because I am not
always able to control/create all the different constructors the base
class has. My problem can be described in code as follows ...

/* This is the base class with a whole heap of
constructors/functionality*/
public class Animal
{
Public Animal()
{/**/}
public Animal(int x)
{/**/}

}

/* This is the derived class that adds some functionality to the base*/
public class TaggedAnimal : Animal

{
Public string GetTag()
{/**/}

}
In my main method I have a whole heap of Animal classes that are
generated. I would like to convert them to TaggedAnimals and start
using them. I don't want to just use another method/class to implement
the GetTag() functionality for a given Animal (using animal as a member
of another class type) because there are other controls/classes/methods
that expect an Animal Type and I would like to pass the same object to
them as well.

So the main code would look something like this :

public class MyMainClass
{
public void MyMainMethod()
{
/* Animals get constructed here, I don't have much control here and
don't want to.*/
System.Collections.Arraylist allAnimals =
ThirdPartyORMTool.GetAllAnimals();

/* But once I have the instances, I would like to inherit from them
to add my own functionality. The TagAllAnimals method
casts/constructs/creates the instances of Animals into instances of
TaggedAnimals */
System.Collections.Arraylist allTaggedAnimals =
TagAllAnimals(allAnimals);
/* Now I can use the tagged animal for various controls to
control/display any functionality in the base or the derived class*/
Gridview.DataSource = allTaggedAnimals;

/* And I can also use the same collection for any others that expect
an Animal class (the base class) */
ThirdPartyORMTool.SaveAnimals(allTaggedAnimals);
ThirdPartyORMTool.DoSomeOtherAnimalRelatedStuff(al lTaggedAnimals);
}

}

How would I go about doing this? Basically, my first instinct was to do
something like this

public class TaggedAnimal : Animal

{

/* Pass an instance of the base class to the constructor */
public TaggedAnimal(Animal currentAnimal)
{
/* Set the base class to the instance of the base class that was
passed in*/
base = currentAnimal;
}

Public string GetTag()
{/**/}

}
but in C# .net 2.0, it complains that "base is a not valid in this
context".

I might be missing something trivial here, but how do I set/control the
instance of the base class without explicitly having to create the base
class by calling its constructor with the required parameters. Or by
creating the derived class to begin with.

Any Help is much appreciated.

So... if I understand you correctly, you want to add a certain piece of
functionality across all of an existing class hierarchy over which you
have little (or no control)?

I swear that there's a Design Pattern for that, but it slips my mind.

Anyone...?

Jan 19 '07 #7

P: n/a
This looks to me like Decorator, but Decorator requires that you
inherit from each class, which is what you're trying to avoid, if I
understand you correctly.

A question for the folks who are on .NET 2.0: Can you do this with
generics:

public class GenericDecorator<Twhere T : Animal
{
...
}

? That is, can a generic class inherit from its own type parameter?

ny********@hotmail.com wrote:
I had a quick look at the GOF book, to no avail....

Bruce Wood wrote:
ny********@hotmail.com wrote:
Hi,
How Do I create an instance of a derived class from an instance of a
base class, essentially wrapping up an existing base class with some
additional functionality. The reason I need this is because I am not
always able to control/create all the different constructors the base
class has. My problem can be described in code as follows ...
>
/* This is the base class with a whole heap of
constructors/functionality*/
public class Animal
{
Public Animal()
{/**/}
public Animal(int x)
{/**/}
>
}
>
/* This is the derived class that adds some functionality to the base*/
public class TaggedAnimal : Animal
>
{
Public string GetTag()
{/**/}
>
}
>
>
In my main method I have a whole heap of Animal classes that are
generated. I would like to convert them to TaggedAnimals and start
using them. I don't want to just use another method/class to implement
the GetTag() functionality for a given Animal (using animal as a member
of another class type) because there are other controls/classes/methods
that expect an Animal Type and I would like to pass the same object to
them as well.
>
So the main code would look something like this :
>
public class MyMainClass
{
public void MyMainMethod()
{
/* Animals get constructed here, I don't have much control here and
don't want to.*/
System.Collections.Arraylist allAnimals =
ThirdPartyORMTool.GetAllAnimals();
>
/* But once I have the instances, I would like to inherit from them
to add my own functionality. The TagAllAnimals method
casts/constructs/creates the instances of Animals into instances of
TaggedAnimals */
System.Collections.Arraylist allTaggedAnimals =
TagAllAnimals(allAnimals);
>
>
/* Now I can use the tagged animal for various controls to
control/display any functionality in the base or the derived class*/
Gridview.DataSource = allTaggedAnimals;
>
/* And I can also use the same collection for any others that expect
an Animal class (the base class) */
ThirdPartyORMTool.SaveAnimals(allTaggedAnimals);
ThirdPartyORMTool.DoSomeOtherAnimalRelatedStuff(al lTaggedAnimals);
>
>
}
>
}
>
How would I go about doing this? Basically, my first instinct was to do
something like this
>
public class TaggedAnimal : Animal
>
{
>
/* Pass an instance of the base class to the constructor */
public TaggedAnimal(Animal currentAnimal)
{
/* Set the base class to the instance of the base class that was
passed in*/
base = currentAnimal;
}
>
Public string GetTag()
{/**/}
>
}
>
>
but in C# .net 2.0, it complains that "base is a not valid in this
context".
>
I might be missing something trivial here, but how do I set/control the
instance of the base class without explicitly having to create the base
class by calling its constructor with the required parameters. Or by
creating the derived class to begin with.
>
Any Help is much appreciated.
So... if I understand you correctly, you want to add a certain piece of
functionality across all of an existing class hierarchy over which you
have little (or no control)?

I swear that there's a Design Pattern for that, but it slips my mind.

Anyone...?
Jan 19 '07 #8

P: n/a
I don't believe so, it is simple containment. He can create a TaggedAnimal
class that has one property an Animal object. He can add methods that add
"tag" behavior and can reference the Animal property if he ever needs to
pass the Animal itself to something outside of his control.

As I understood it his own routines understand TaggedAnimal objects but some
of the existing routines only accept Animal objects. These routines aren't
going to understand anything about tagging in any case so it doesn't need
(nor want) a TaggedAnimal.

Who knows? :-)
"Bruce Wood" <br*******@canada.comwrote in message
news:11**********************@s34g2000cwa.googlegr oups.com...
This looks to me like Decorator, but Decorator requires that you
inherit from each class, which is what you're trying to avoid, if I
understand you correctly.

A question for the folks who are on .NET 2.0: Can you do this with
generics:

public class GenericDecorator<Twhere T : Animal
{
...
}

? That is, can a generic class inherit from its own type parameter?

Jan 19 '07 #9

P: n/a

Tom Leylan wrote:
I don't believe so, it is simple containment. He can create a TaggedAnimal
class that has one property an Animal object. He can add methods that add
"tag" behavior and can reference the Animal property if he ever needs to
pass the Animal itself to something outside of his control.

As I understood it his own routines understand TaggedAnimal objects but some
of the existing routines only accept Animal objects. These routines aren't
going to understand anything about tagging in any case so it doesn't need
(nor want) a TaggedAnimal.

Who knows? :-)
Maybe I misunderstood his original problem, then.

As I understand it, he has an ORM that is generating lots of different,
unrelated classes from data representations. He wants to graft on a Tag
property (or some other method / property / whatever) to every class
and then use the resulting class interchangeably with the original
class. The issue, as I understand it, is that he doesn't want to have
to write all of the subclasses individually, by hand, and maintain
them, and he doesn't want to have to modify the base classes, which are
automatically generated.

I could be wrong, though.

Another solution might be partial classes in C# 2.0: just modify every
automatically generated class to be partial, then write any additional
code in another file, for each class. At least then the additions
wouldn't be overwritten when you used the ORM tool to regenerate the
classes. In fact, this is how the ORM tool should be doing things:
generating partial classes that you can then expand at will.

Jan 19 '07 #10

P: n/a

ny********@hotmail.com wrote:
public class MyMainClass
{
public void MyMainMethod()
{
/* Animals get constructed here, I don't have much control here and
don't want to.*/
System.Collections.Arraylist allAnimals =
ThirdPartyORMTool.GetAllAnimals();

/* But once I have the instances, I would like to inherit from them
to add my own functionality. The TagAllAnimals method
casts/constructs/creates the instances of Animals into instances of
TaggedAnimals */
System.Collections.Arraylist allTaggedAnimals =
TagAllAnimals(allAnimals);
/* Now I can use the tagged animal for various controls to
control/display any functionality in the base or the derived class*/
Gridview.DataSource = allTaggedAnimals;

/* And I can also use the same collection for any others that expect
an Animal class (the base class) */
ThirdPartyORMTool.SaveAnimals(allTaggedAnimals);
ThirdPartyORMTool.DoSomeOtherAnimalRelatedStuff(al lTaggedAnimals);
}

}

How would I go about doing this?
It looks like what you want is a proxy.

public interface IAnimal
{
void Eat();
}

public interface ITaggedAnimal : IAnimal
{
string getTag();
}

public class TaggedAnimalProxy : ITaggedAnimal
{
private Animal m_Target;

public TaggedAnimalProxy(IAnimal target)
{
m_Target = target;
}

public string getTag()
{
return "tag";
}

public void Eat()
{
m_Target.Eat();
}
}

public static void Main()
{
IAnimal animal = GetSomeAnimalObject();
ITaggedAnimal taggedAnimal = new TaggedAnimalProxy(animal);
}

Does that help?

Brian

Jan 19 '07 #11

P: n/a
I believe you recapped it more or less correctly. I'm not sure if they are
"unrelated" animal types since he expressly mentions an "Animal" base type.
He can't (apparently) subclass this the way he wants to (or doesn't want to)
so I thought he might contain them in a specialized TaggedAnimal class and
be done with it.

Again I certainly don't know for sure :-)

"Bruce Wood" <br*******@canada.comwrote in message
news:11********************@a75g2000cwd.googlegrou ps.com...
>
Tom Leylan wrote:
>I don't believe so, it is simple containment. He can create a
TaggedAnimal
class that has one property an Animal object. He can add methods that
add
"tag" behavior and can reference the Animal property if he ever needs to
pass the Animal itself to something outside of his control.

As I understood it his own routines understand TaggedAnimal objects but
some
of the existing routines only accept Animal objects. These routines
aren't
going to understand anything about tagging in any case so it doesn't need
(nor want) a TaggedAnimal.

Who knows? :-)

Maybe I misunderstood his original problem, then.

As I understand it, he has an ORM that is generating lots of different,
unrelated classes from data representations. He wants to graft on a Tag
property (or some other method / property / whatever) to every class
and then use the resulting class interchangeably with the original
class. The issue, as I understand it, is that he doesn't want to have
to write all of the subclasses individually, by hand, and maintain
them, and he doesn't want to have to modify the base classes, which are
automatically generated.

I could be wrong, though.

Another solution might be partial classes in C# 2.0: just modify every
automatically generated class to be partial, then write any additional
code in another file, for each class. At least then the additions
wouldn't be overwritten when you used the ORM tool to regenerate the
classes. In fact, this is how the ORM tool should be doing things:
generating partial classes that you can then expand at will.

Jan 19 '07 #12

P: n/a

Tom Leylan wrote:
I believe you recapped it more or less correctly. I'm not sure if they are
"unrelated" animal types since he expressly mentions an "Animal" base type.
He can't (apparently) subclass this the way he wants to (or doesn't want to)
so I thought he might contain them in a specialized TaggedAnimal class and
be done with it.

Again I certainly don't know for sure :-)
My impression is that the "Animal" thing was just and example. In
reality he's using an ORM, so he'll have lots of different, unrelated
base classes to contend with. At least, that's what I understood.

Jan 19 '07 #13

P: n/a
Animal is almost assuredly just an example. If he is dealing with lots of
different classes then I would suggested the example include Dog(), Cat(),
etc. So basically you think he wants TaggableCat(), TaggableDog(), etc.?
So again can't he just contain these? The Cat and Dog objects are exposed
for what they are when he needs them to be and they have tag functionality
by virtue of their container.

Perhaps his interest has waned or he has solved it another way...

"Bruce Wood" <br*******@canada.comwrote in message
news:11**********************@38g2000cwa.googlegro ups.com...
>
Tom Leylan wrote:
>I believe you recapped it more or less correctly. I'm not sure if they
are
"unrelated" animal types since he expressly mentions an "Animal" base
type.
He can't (apparently) subclass this the way he wants to (or doesn't want
to)
so I thought he might contain them in a specialized TaggedAnimal class
and
be done with it.

Again I certainly don't know for sure :-)

My impression is that the "Animal" thing was just and example. In
reality he's using an ORM, so he'll have lots of different, unrelated
base classes to contend with. At least, that's what I understood.

Jan 19 '07 #14

P: n/a
Hi,
Thanks for the replies guys. No, my interest hasn't waned yet. After
the friday evening post, i had some stuff to do on saturday morning
before I checked the newsgroups again in the evening. Sorry about the
delay :-)

First of all "Animal" is an example, maybe I should have used
"customer" with the northwind database as a reference.

Tom, I did consider using the decorator pattern. You are right in some
ways that the methods that take the base "Animal" wouldn't know about
the "TaggedAnimal" class anyway and I could just pass them the animal I
get from "TaggedAnimal.GetAnimal()". But the problem is when I am
binding the object to a control such as a GridView, I want to display
the properties from the "TaggedAnimal" class as well as the base class.
This means that each of the properties in the base class would have to
be exposed as a property in the derived class. So, the TaggedAnimal
would look something like

public TaggedAnimal
{
Animal currentAnimal;
public TaggedAnimal(Animal CurrentAnimal){currentAnimal =
CurrentAnimal;}

public string Weight {get {return currentAnimal.Weight;}}
public string Height {get {return currentAnimal.Height;}}
/* etc. etc. Plus there will be Tagged animal specific properties. */

}
The problem with this approach is that every time I change the
underlying database, say by adding another column "BloodType", I don't
just have to use the ORM tool to regenerate my ORM DB layer, I will
also have to change some of the code in my own DB/Business Logic layer.
Basically with a new column, I would have to go back and change the
TaggedAnimal class to

public TaggedAnimal
{
Animal currentAnimal;
public TaggedAnimal(Animal CurrentAnimal){currentAnimal =
CurrentAnimal;}

public string Weight {get {return currentAnimal.Weight;}}
public string Height {get {return currentAnimal.Height;}}
public string BloodType {get {return currentAnimal.BloodType;}}
/* etc. etc. Plus there will be Tagged animal specific properties. */

}

And I have to do that everytime I make some change to the underlying
database, which is exactly what I am trying to avoid by using the ORM
tool.
But if the TaggedAnimal inherited from the Animal class, the data in
the new column would be visible in the TaggedAnimal as well, without me
having to do anything. All I have to do is change the view layer to
include another column in the Gridview that displays the BloodType (or
not even that, if i set the AutoGenerate property to true in the
gridview)
I suppose I can still use the decorater pattern and use the
GetAnimal()."PropertyName" in the boundfield column. Do something like

<asp:BoundColumn HeaderText="Tag" DataField="Tag"></asp:BoundColumn>
<asp:BoundColumn HeaderText="Height"
DataField="GetAnimal().Height"></asp:BoundColumn>
<asp:BoundColumn HeaderText="Weight"
DataField="GetAnimal().Weight"></asp:BoundColumn>

Does the Gridview allow for calling methods as part of the datafield?
Need to try it out.
Nonetheless, It still seems to me that a good "design" for this calls
for the use of inheritance rather than composition. The functionality
that I am adding is basically transforming/manipulating a small bit of
data in the underlying class. This shouldn't require me to change the
type of the class itself. In a way, it is still an "Animal", I haven't
changed its nature or used it as a part of a larger, different entity.
All I did was add a tiny bit of information/processing on top, so
conceptually it seems to me that it should retain its "Animal" nature
(type).
Bruce,
Forgive me for the use of the example, I see it has made some things a
bit murkier. The property I want to add is not necessarily the same for
all the classes. So I will have to subclass each of the generated base
classes/entities anyway. For example another class (related to the
database table) might be "Equipment". I might want to add a property
that calculates the age of the equipment based on the current time and
"AcquiredDate" property of the Equipment class, then I would do
something similar like

public DatedEquipment : Equipment
{
public DatedEquipment(Equipment currentEquipment) : base =
currentEquipment
{}
public string Age
{
get
{
return (System.datetime.now - base.AcquiredDate).ToString();
/* .. Or something along those lines.*/
}
}
}

Clearly any custom properties/processing that I add to the Equipment
class will be different than that I add to the Anmial class so each of
the class will have to be subclassed again. (For the ones that need
custom properties/processing anyway).

But the problem you posed is very interesting as well, and that would
definitely be a cool pattern (not to mention some killer use of
generics).

The root of my problem is not so much that I need "dynamism" in my
inheritance hierarchy ( I can subclass each class as i said), its more
along the lines of "the base class has already been instantiated (by
the ORM, when i call "ThirdPartyORMTool.GetAllAnimals();"), the type
hierarchy has been defined statically at design time (which is fine),
but now I need to use the already existing base class when
instantiating a subclass"

Thanks again for the help guys, much appreciated.

On Jan 20, 8:24 am, "Tom Leylan" <tley...@nospam.netwrote:
Animal is almost assuredly just an example. If he is dealing with lots of
different classes then I would suggested the example include Dog(), Cat(),
etc. So basically you think he wants TaggableCat(), TaggableDog(), etc.?
So again can't he just contain these? The Cat and Dog objects are exposed
for what they are when he needs them to be and they have tag functionality
by virtue of their container.

Perhaps his interest has waned or he has solved it another way...

"Bruce Wood" <brucew...@canada.comwrote in messagenews:11**********************@38g2000cwa.go oglegroups.com...
Tom Leylan wrote:
I believe you recapped it more or less correctly. I'm not sure if they
are
"unrelated" animal types since he expressly mentions an "Animal" base
type.
He can't (apparently) subclass this the way he wants to (or doesn't want
to)
so I thought he might contain them in a specialized TaggedAnimal class
and
be done with it.
Again I certainly don't know for sure :-)
My impression is that the "Animal" thing was just and example. In
reality he's using an ORM, so he'll have lots of different, unrelated
base classes to contend with. At least, that's what I understood.
Jan 20 '07 #15

P: n/a
Hi,
Thanks for the replies guys. No, my interest hasn't waned yet. After
the friday evening post, i had some stuff to do on saturday morning
before I checked the newsgroups again in the evening. Sorry about the
delay :-)

First of all "Animal" is an example, maybe I should have used
"customer" with the northwind database as a reference.

Tom, I did consider using the decorator pattern. You are right in some
ways the methods that take the base "Animal" wouldn't know about the
"TaggedAnimal" class anyway and I could just pass them the animal I get
from "TaggedAnimal.GetAnimal()". But the problem is when I am binding
the object to a control such as a GridView, I want to display the
properties from the "TaggedAnimal" class as well as the base class.
This means that each of the properties in the base class would have to
be exposed as a property in the derived class. So, the TaggedAnimal
would look something like

public TaggedAnimal
{
Animal currentAnimal;
public TaggedAnimal(Animal CurrentAnimal){currentAnimal =
CurrentAnimal;}

public string Weight {get {return currentAnimal.Weight;}}
public string Height {get {return currentAnimal.Height;}}
/* etc. etc. Plus there will be Tagged animal specific properties. */

}
The problem with this approach is that every time I change the
underlying database, say by adding another column "BloodType", I don't
just have to use the ORM tool to regenerate my ORM DB layer, I will
also have to change some of the code in my own DB/Business Logic layer.
Basically with a new column, I would have to go back and change the
TaggedAnimal class to

public TaggedAnimal
{
Animal currentAnimal;
public TaggedAnimal(Animal CurrentAnimal){currentAnimal =
CurrentAnimal;}

public string Weight {get {return currentAnimal.Weight;}}
public string Height {get {return currentAnimal.Height;}}
public string BloodType {get {return currentAnimal.BloodType;}}
/* etc. etc. Plus there will be Tagged animal specific properties. */

}

And I have to do that everytime I make some change to the underlying
database, which is exactly what I am trying to avoid by using the ORM
tool.
But if the TaggedAnimal inherited from the Animal class, the data in
the new column would be visible in the TaggedAnimal as well, without me
having to do anything. All I have to do is change the view layer to
include another column in the Gridview that displays the BloodType (or
not even that, if i set the AutoGenerate property to true in the
gridview)
I suppose I can still use the decorater pattern and use the
GetAnimal()."PropertyName" in the boundfield column. Do something like

<asp:BoundColumn HeaderText="Tag" DataField="Tag"></asp:BoundColumn>
<asp:BoundColumn HeaderText="Height"
DataField="GetAnimal().Height"></asp:BoundColumn>
<asp:BoundColumn HeaderText="Weight"
DataField="GetAnimal().Weight"></asp:BoundColumn>

Does the Gridview allow for calling methods as part of the datafield?
Need to try it out.
Nonetheless, It still seems to me that a good "design" for this calls
for the use of inheritance rather than composition. The functionality
that I am adding is basically transforming/manipulating a small bit of
data in the underlying class. This shouldn't require me to change the
type of the class itself. In a way, it is still an "Animal", I haven't
changed its nature or used it as a part of a larger, different entity.
All I did was add a tiny bit of information/processing on top, so
conceptually it seems to me that it should retain its "Animal" nature
(type).
Bruce,
Forgive me for the use of the example, I see it has made some things a
bit murkier. The property I want to add is not necessarily the same for
all the classes. So I will have to subclass each of the generated base
classes/entities. For example another class (related to the database
table) might be "Equipment". I might want to add a property that
calculates the age of the equipment based on the current time and
"AcquiredDate" property of the Equipment class, then I would do
something similar like

public DatedEquipment : Equipment
{
public DatedEquipment(Equipment currentEquipment) : base =
currentEquipment
{}
public string Age
{
get
{
return (System.datetime.now - base.AcquiredDate).ToString();
/* .. Or something along those lines.*/
}
}
}

Clearly any custom properties/processing that I add to the Equipment
class will be different than that I add to the Anmial class so each of
the class will have to be subclassed again. (For the ones that need
custom properties/processing anyway).

But the problem you posed is very interesting as well, and that would
definitely be a cool pattern (not to mention some killer use of
generics).

The root of my problem is not so much that I need "dynamism" in my
inheritance hierarchy ( I can subclass each class as i said), its more
along the lines of "the base class has already been instantiated (by
the ORM, when i call "ThirdPartyORMTool.GetAllAnimals();"), the type
hierarchy has been defined statically at design time (which is fine),
but now I need to use the already existing base class when
instantiating a subclass"

Thanks again for the help guys, much appreciated.

Tom Leylan wrote:
Animal is almost assuredly just an example. If he is dealing with lots of
different classes then I would suggested the example include Dog(), Cat(),
etc. So basically you think he wants TaggableCat(), TaggableDog(), etc.?
So again can't he just contain these? The Cat and Dog objects are exposed
for what they are when he needs them to be and they have tag functionality
by virtue of their container.

Perhaps his interest has waned or he has solved it another way...

"Bruce Wood" <br*******@canada.comwrote in message
news:11**********************@38g2000cwa.googlegro ups.com...

Tom Leylan wrote:
I believe you recapped it more or less correctly. I'm not sure if they
are
"unrelated" animal types since he expressly mentions an "Animal" base
type.
He can't (apparently) subclass this the way he wants to (or doesn't want
to)
so I thought he might contain them in a specialized TaggedAnimal class
and
be done with it.

Again I certainly don't know for sure :-)
My impression is that the "Animal" thing was just and example. In
reality he's using an ORM, so he'll have lots of different, unrelated
base classes to contend with. At least, that's what I understood.
Jan 20 '07 #16

P: n/a
why don't you write an operator that converts animals in tagedAnimals?

ny********@hotmail.com schrieb:
Hi,
Thanks for the replies guys. No, my interest hasn't waned yet. After
the friday evening post, i had some stuff to do on saturday morning
before I checked the newsgroups again in the evening. Sorry about the
delay :-)

First of all "Animal" is an example, maybe I should have used
"customer" with the northwind database as a reference.

Tom, I did consider using the decorator pattern. You are right in some
ways the methods that take the base "Animal" wouldn't know about the
"TaggedAnimal" class anyway and I could just pass them the animal I get
from "TaggedAnimal.GetAnimal()". But the problem is when I am binding
the object to a control such as a GridView, I want to display the
properties from the "TaggedAnimal" class as well as the base class.
This means that each of the properties in the base class would have to
be exposed as a property in the derived class. So, the TaggedAnimal
would look something like

public TaggedAnimal
{
Animal currentAnimal;
public TaggedAnimal(Animal CurrentAnimal){currentAnimal =
CurrentAnimal;}

public string Weight {get {return currentAnimal.Weight;}}
public string Height {get {return currentAnimal.Height;}}
/* etc. etc. Plus there will be Tagged animal specific properties. */

}
The problem with this approach is that every time I change the
underlying database, say by adding another column "BloodType", I don't
just have to use the ORM tool to regenerate my ORM DB layer, I will
also have to change some of the code in my own DB/Business Logic layer.
Basically with a new column, I would have to go back and change the
TaggedAnimal class to

public TaggedAnimal
{
Animal currentAnimal;
public TaggedAnimal(Animal CurrentAnimal){currentAnimal =
CurrentAnimal;}

public string Weight {get {return currentAnimal.Weight;}}
public string Height {get {return currentAnimal.Height;}}
public string BloodType {get {return currentAnimal.BloodType;}}
/* etc. etc. Plus there will be Tagged animal specific properties. */

}

And I have to do that everytime I make some change to the underlying
database, which is exactly what I am trying to avoid by using the ORM
tool.
But if the TaggedAnimal inherited from the Animal class, the data in
the new column would be visible in the TaggedAnimal as well, without me
having to do anything. All I have to do is change the view layer to
include another column in the Gridview that displays the BloodType (or
not even that, if i set the AutoGenerate property to true in the
gridview)
I suppose I can still use the decorater pattern and use the
GetAnimal()."PropertyName" in the boundfield column. Do something like

<asp:BoundColumn HeaderText="Tag" DataField="Tag"></asp:BoundColumn>
<asp:BoundColumn HeaderText="Height"
DataField="GetAnimal().Height"></asp:BoundColumn>
<asp:BoundColumn HeaderText="Weight"
DataField="GetAnimal().Weight"></asp:BoundColumn>

Does the Gridview allow for calling methods as part of the datafield?
Need to try it out.
Nonetheless, It still seems to me that a good "design" for this calls
for the use of inheritance rather than composition. The functionality
that I am adding is basically transforming/manipulating a small bit of
data in the underlying class. This shouldn't require me to change the
type of the class itself. In a way, it is still an "Animal", I haven't
changed its nature or used it as a part of a larger, different entity.
All I did was add a tiny bit of information/processing on top, so
conceptually it seems to me that it should retain its "Animal" nature
(type).
Bruce,
Forgive me for the use of the example, I see it has made some things a
bit murkier. The property I want to add is not necessarily the same for
all the classes. So I will have to subclass each of the generated base
classes/entities. For example another class (related to the database
table) might be "Equipment". I might want to add a property that
calculates the age of the equipment based on the current time and
"AcquiredDate" property of the Equipment class, then I would do
something similar like

public DatedEquipment : Equipment
{
public DatedEquipment(Equipment currentEquipment) : base =
currentEquipment
{}
public string Age
{
get
{
return (System.datetime.now - base.AcquiredDate).ToString();
/* .. Or something along those lines.*/
}
}
}

Clearly any custom properties/processing that I add to the Equipment
class will be different than that I add to the Anmial class so each of
the class will have to be subclassed again. (For the ones that need
custom properties/processing anyway).

But the problem you posed is very interesting as well, and that would
definitely be a cool pattern (not to mention some killer use of
generics).

The root of my problem is not so much that I need "dynamism" in my
inheritance hierarchy ( I can subclass each class as i said), its more
along the lines of "the base class has already been instantiated (by
the ORM, when i call "ThirdPartyORMTool.GetAllAnimals();"), the type
hierarchy has been defined statically at design time (which is fine),
but now I need to use the already existing base class when
instantiating a subclass"

Thanks again for the help guys, much appreciated.

Tom Leylan wrote:
Animal is almost assuredly just an example. If he is dealing with lots of
different classes then I would suggested the example include Dog(), Cat(),
etc. So basically you think he wants TaggableCat(), TaggableDog(), etc.?
So again can't he just contain these? The Cat and Dog objects are exposed
for what they are when he needs them to be and they have tag functionality
by virtue of their container.

Perhaps his interest has waned or he has solved it another way...

"Bruce Wood" <br*******@canada.comwrote in message
news:11**********************@38g2000cwa.googlegro ups.com...
>
Tom Leylan wrote:
>I believe you recapped it more or less correctly. I'm not sure if they
>are
>"unrelated" animal types since he expressly mentions an "Animal" base
>type.
>He can't (apparently) subclass this the way he wants to (or doesn't want
>to)
>so I thought he might contain them in a specialized TaggedAnimal class
>and
>be done with it.
>>
>Again I certainly don't know for sure :-)
>
My impression is that the "Animal" thing was just and example. In
reality he's using an ORM, so he'll have lots of different, unrelated
base classes to contend with. At least, that's what I understood.
>
Jan 20 '07 #17

P: n/a
you could then use Reflection to set all the properties of the
baseclass.

ny********@hotmail.com schrieb:
Hi,
Thanks for the replies guys. No, my interest hasn't waned yet. After
the friday evening post, i had some stuff to do on saturday morning
before I checked the newsgroups again in the evening. Sorry about the
delay :-)

First of all "Animal" is an example, maybe I should have used
"customer" with the northwind database as a reference.

Tom, I did consider using the decorator pattern. You are right in some
ways the methods that take the base "Animal" wouldn't know about the
"TaggedAnimal" class anyway and I could just pass them the animal I get
from "TaggedAnimal.GetAnimal()". But the problem is when I am binding
the object to a control such as a GridView, I want to display the
properties from the "TaggedAnimal" class as well as the base class.
This means that each of the properties in the base class would have to
be exposed as a property in the derived class. So, the TaggedAnimal
would look something like

public TaggedAnimal
{
Animal currentAnimal;
public TaggedAnimal(Animal CurrentAnimal){currentAnimal =
CurrentAnimal;}

public string Weight {get {return currentAnimal.Weight;}}
public string Height {get {return currentAnimal.Height;}}
/* etc. etc. Plus there will be Tagged animal specific properties. */

}
The problem with this approach is that every time I change the
underlying database, say by adding another column "BloodType", I don't
just have to use the ORM tool to regenerate my ORM DB layer, I will
also have to change some of the code in my own DB/Business Logic layer.
Basically with a new column, I would have to go back and change the
TaggedAnimal class to

public TaggedAnimal
{
Animal currentAnimal;
public TaggedAnimal(Animal CurrentAnimal){currentAnimal =
CurrentAnimal;}

public string Weight {get {return currentAnimal.Weight;}}
public string Height {get {return currentAnimal.Height;}}
public string BloodType {get {return currentAnimal.BloodType;}}
/* etc. etc. Plus there will be Tagged animal specific properties. */

}

And I have to do that everytime I make some change to the underlying
database, which is exactly what I am trying to avoid by using the ORM
tool.
But if the TaggedAnimal inherited from the Animal class, the data in
the new column would be visible in the TaggedAnimal as well, without me
having to do anything. All I have to do is change the view layer to
include another column in the Gridview that displays the BloodType (or
not even that, if i set the AutoGenerate property to true in the
gridview)
I suppose I can still use the decorater pattern and use the
GetAnimal()."PropertyName" in the boundfield column. Do something like

<asp:BoundColumn HeaderText="Tag" DataField="Tag"></asp:BoundColumn>
<asp:BoundColumn HeaderText="Height"
DataField="GetAnimal().Height"></asp:BoundColumn>
<asp:BoundColumn HeaderText="Weight"
DataField="GetAnimal().Weight"></asp:BoundColumn>

Does the Gridview allow for calling methods as part of the datafield?
Need to try it out.
Nonetheless, It still seems to me that a good "design" for this calls
for the use of inheritance rather than composition. The functionality
that I am adding is basically transforming/manipulating a small bit of
data in the underlying class. This shouldn't require me to change the
type of the class itself. In a way, it is still an "Animal", I haven't
changed its nature or used it as a part of a larger, different entity.
All I did was add a tiny bit of information/processing on top, so
conceptually it seems to me that it should retain its "Animal" nature
(type).
Bruce,
Forgive me for the use of the example, I see it has made some things a
bit murkier. The property I want to add is not necessarily the same for
all the classes. So I will have to subclass each of the generated base
classes/entities. For example another class (related to the database
table) might be "Equipment". I might want to add a property that
calculates the age of the equipment based on the current time and
"AcquiredDate" property of the Equipment class, then I would do
something similar like

public DatedEquipment : Equipment
{
public DatedEquipment(Equipment currentEquipment) : base =
currentEquipment
{}
public string Age
{
get
{
return (System.datetime.now - base.AcquiredDate).ToString();
/* .. Or something along those lines.*/
}
}
}

Clearly any custom properties/processing that I add to the Equipment
class will be different than that I add to the Anmial class so each of
the class will have to be subclassed again. (For the ones that need
custom properties/processing anyway).

But the problem you posed is very interesting as well, and that would
definitely be a cool pattern (not to mention some killer use of
generics).

The root of my problem is not so much that I need "dynamism" in my
inheritance hierarchy ( I can subclass each class as i said), its more
along the lines of "the base class has already been instantiated (by
the ORM, when i call "ThirdPartyORMTool.GetAllAnimals();"), the type
hierarchy has been defined statically at design time (which is fine),
but now I need to use the already existing base class when
instantiating a subclass"

Thanks again for the help guys, much appreciated.

Tom Leylan wrote:
Animal is almost assuredly just an example. If he is dealing with lots of
different classes then I would suggested the example include Dog(), Cat(),
etc. So basically you think he wants TaggableCat(), TaggableDog(), etc.?
So again can't he just contain these? The Cat and Dog objects are exposed
for what they are when he needs them to be and they have tag functionality
by virtue of their container.

Perhaps his interest has waned or he has solved it another way...

"Bruce Wood" <br*******@canada.comwrote in message
news:11**********************@38g2000cwa.googlegro ups.com...
>
Tom Leylan wrote:
>I believe you recapped it more or less correctly. I'm not sure if they
>are
>"unrelated" animal types since he expressly mentions an "Animal" base
>type.
>He can't (apparently) subclass this the way he wants to (or doesn't want
>to)
>so I thought he might contain them in a specialized TaggedAnimal class
>and
>be done with it.
>>
>Again I certainly don't know for sure :-)
>
My impression is that the "Animal" thing was just and example. In
reality he's using an ORM, so he'll have lots of different, unrelated
base classes to contend with. At least, that's what I understood.
>
Jan 20 '07 #18

P: n/a
Hmmm... As i said in my earlier posts, reflection seems a bit top
heavy, especially if i have to do it for objects in a potentiall large
collection.

What would be good (or hoping to find) is some way of letting the CLR
know which instance of a base object to use and turn off its default
base object creation functionality.

A constructor such as public " TaggedAnimal(Animal currentAnimal) :
base = currentAnimal " would ofcourse be ideal, but is clearly not
supported. Maybe there is an event that is fired when the CLR is
creating the base class that I can subscribe to and intercept?
something along the lines of ...

public TaggedAnimal : Animal
{
public static void ReplaceBaseClass(object obj, EventArgs arg)
{
/* Hopfully, CLR will pass me some stuff in the arguments that will
allow me to change the base */
}

public TaggedAnimal(Animal currentAnimal) : base
{

}
}

And in the main class I can do something like

Public void MyMainMehtod()
{
System.CLR.OnBaseCreationEvent(typeof (TaggedAnimal)) +=
TaggedAnimal.ReplaceBaseClass()
}

Or something along those lines... You get the idea. Just thinking out
loud now basically....
Mike wrote:
you could then use Reflection to set all the properties of the
baseclass.

ny********@hotmail.com schrieb:
Hi,
Thanks for the replies guys. No, my interest hasn't waned yet. After
the friday evening post, i had some stuff to do on saturday morning
before I checked the newsgroups again in the evening. Sorry about the
delay :-)

First of all "Animal" is an example, maybe I should have used
"customer" with the northwind database as a reference.

Tom, I did consider using the decorator pattern. You are right in some
ways the methods that take the base "Animal" wouldn't know about the
"TaggedAnimal" class anyway and I could just pass them the animal I get
from "TaggedAnimal.GetAnimal()". But the problem is when I am binding
the object to a control such as a GridView, I want to display the
properties from the "TaggedAnimal" class as well as the base class.
This means that each of the properties in the base class would have to
be exposed as a property in the derived class. So, the TaggedAnimal
would look something like

public TaggedAnimal
{
Animal currentAnimal;
public TaggedAnimal(Animal CurrentAnimal){currentAnimal =
CurrentAnimal;}

public string Weight {get {return currentAnimal.Weight;}}
public string Height {get {return currentAnimal.Height;}}
/* etc. etc. Plus there will be Tagged animal specific properties. */

}
The problem with this approach is that every time I change the
underlying database, say by adding another column "BloodType", I don't
just have to use the ORM tool to regenerate my ORM DB layer, I will
also have to change some of the code in my own DB/Business Logic layer.
Basically with a new column, I would have to go back and change the
TaggedAnimal class to

public TaggedAnimal
{
Animal currentAnimal;
public TaggedAnimal(Animal CurrentAnimal){currentAnimal =
CurrentAnimal;}

public string Weight {get {return currentAnimal.Weight;}}
public string Height {get {return currentAnimal.Height;}}
public string BloodType {get {return currentAnimal.BloodType;}}
/* etc. etc. Plus there will be Tagged animal specific properties. */

}

And I have to do that everytime I make some change to the underlying
database, which is exactly what I am trying to avoid by using the ORM
tool.
But if the TaggedAnimal inherited from the Animal class, the data in
the new column would be visible in the TaggedAnimal as well, without me
having to do anything. All I have to do is change the view layer to
include another column in the Gridview that displays the BloodType (or
not even that, if i set the AutoGenerate property to true in the
gridview)
I suppose I can still use the decorater pattern and use the
GetAnimal()."PropertyName" in the boundfield column. Do something like

<asp:BoundColumn HeaderText="Tag" DataField="Tag"></asp:BoundColumn>
<asp:BoundColumn HeaderText="Height"
DataField="GetAnimal().Height"></asp:BoundColumn>
<asp:BoundColumn HeaderText="Weight"
DataField="GetAnimal().Weight"></asp:BoundColumn>

Does the Gridview allow for calling methods as part of the datafield?
Need to try it out.
Nonetheless, It still seems to me that a good "design" for this calls
for the use of inheritance rather than composition. The functionality
that I am adding is basically transforming/manipulating a small bit of
data in the underlying class. This shouldn't require me to change the
type of the class itself. In a way, it is still an "Animal", I haven't
changed its nature or used it as a part of a larger, different entity.
All I did was add a tiny bit of information/processing on top, so
conceptually it seems to me that it should retain its "Animal" nature
(type).
Bruce,
Forgive me for the use of the example, I see it has made some things a
bit murkier. The property I want to add is not necessarily the same for
all the classes. So I will have to subclass each of the generated base
classes/entities. For example another class (related to the database
table) might be "Equipment". I might want to add a property that
calculates the age of the equipment based on the current time and
"AcquiredDate" property of the Equipment class, then I would do
something similar like

public DatedEquipment : Equipment
{
public DatedEquipment(Equipment currentEquipment) : base =
currentEquipment
{}
public string Age
{
get
{
return (System.datetime.now - base.AcquiredDate).ToString();
/* .. Or something along those lines.*/
}
}
}

Clearly any custom properties/processing that I add to the Equipment
class will be different than that I add to the Anmial class so each of
the class will have to be subclassed again. (For the ones that need
custom properties/processing anyway).

But the problem you posed is very interesting as well, and that would
definitely be a cool pattern (not to mention some killer use of
generics).

The root of my problem is not so much that I need "dynamism" in my
inheritance hierarchy ( I can subclass each class as i said), its more
along the lines of "the base class has already been instantiated (by
the ORM, when i call "ThirdPartyORMTool.GetAllAnimals();"), the type
hierarchy has been defined statically at design time (which is fine),
but now I need to use the already existing base class when
instantiating a subclass"

Thanks again for the help guys, much appreciated.

Tom Leylan wrote:
Animal is almost assuredly just an example. If he is dealing with lots of
different classes then I would suggested the example include Dog(), Cat(),
etc. So basically you think he wants TaggableCat(), TaggableDog(), etc.?
So again can't he just contain these? The Cat and Dog objects are exposed
for what they are when he needs them to be and they have tag functionality
by virtue of their container.
>
Perhaps his interest has waned or he has solved it another way...
>
"Bruce Wood" <br*******@canada.comwrote in message
news:11**********************@38g2000cwa.googlegro ups.com...

Tom Leylan wrote:
I believe you recapped it more or less correctly. I'm not sure if they
are
"unrelated" animal types since he expressly mentions an "Animal" base
type.
He can't (apparently) subclass this the way he wants to (or doesn't want
to)
so I thought he might contain them in a specialized TaggedAnimal class
and
be done with it.
>
Again I certainly don't know for sure :-)

My impression is that the "Animal" thing was just and example. In
reality he's using an ORM, so he'll have lots of different, unrelated
base classes to contend with. At least, that's what I understood.
Jan 20 '07 #19

P: n/a

ny********@hotmail.com wrote:
Bruce,
Forgive me for the use of the example, I see it has made some things a
bit murkier. The property I want to add is not necessarily the same for
all the classes. So I will have to subclass each of the generated base
classes/entities. For example another class (related to the database
table) might be "Equipment". I might want to add a property that
calculates the age of the equipment based on the current time and
"AcquiredDate" property of the Equipment class, then I would do
something similar like

public DatedEquipment : Equipment
{
public DatedEquipment(Equipment currentEquipment) : base =
currentEquipment
{}
public string Age
{
get
{
return (System.datetime.now - base.AcquiredDate).ToString();
/* .. Or something along those lines.*/
}
}
}

Clearly any custom properties/processing that I add to the Equipment
class will be different than that I add to the Anmial class so each of
the class will have to be subclassed again. (For the ones that need
custom properties/processing anyway).

But the problem you posed is very interesting as well, and that would
definitely be a cool pattern (not to mention some killer use of
generics).

The root of my problem is not so much that I need "dynamism" in my
inheritance hierarchy ( I can subclass each class as i said), its more
along the lines of "the base class has already been instantiated (by
the ORM, when i call "ThirdPartyORMTool.GetAllAnimals();"), the type
hierarchy has been defined statically at design time (which is fine),
but now I need to use the already existing base class when
instantiating a subclass"
What about using partial classes? Can your ORM tool generate partial
classes? Then you could actually alter each class in a separate .cs
file and the additional properties / methods you add would survive
regeneration of the class by the ORM tool.

Jan 20 '07 #20

P: n/a
Bruce Wood wrote:
ny********@hotmail.com wrote:
Bruce,
Forgive me for the use of the example, I see it has made some things a
bit murkier. The property I want to add is not necessarily the same for
all the classes. So I will have to subclass each of the generated base
classes/entities. For example another class (related to the database
table) might be "Equipment". I might want to add a property that
calculates the age of the equipment based on the current time and
"AcquiredDate" property of the Equipment class, then I would do
something similar like

public DatedEquipment : Equipment
{
public DatedEquipment(Equipment currentEquipment) : base =
currentEquipment
{}
public string Age
{
get
{
return (System.datetime.now - base.AcquiredDate).ToString();
/* .. Or something along those lines.*/
}
}
}

Clearly any custom properties/processing that I add to the Equipment
class will be different than that I add to the Anmial class so each of
the class will have to be subclassed again. (For the ones that need
custom properties/processing anyway).

But the problem you posed is very interesting as well, and that would
definitely be a cool pattern (not to mention some killer use of
generics).

The root of my problem is not so much that I need "dynamism" in my
inheritance hierarchy ( I can subclass each class as i said), its more
along the lines of "the base class has already been instantiated (by
the ORM, when i call "ThirdPartyORMTool.GetAllAnimals();"), the type
hierarchy has been defined statically at design time (which is fine),
but now I need to use the already existing base class when
instantiating a subclass"

What about using partial classes? Can your ORM tool generate partial
classes? Then you could actually alter each class in a separate .cs
file and the additional properties / methods you add would survive
regeneration of the class by the ORM tool.
That would certainly be a very nice solution. I will check it out
tommorrow and let you guys know.

Jan 21 '07 #21

P: n/a
<ny********@hotmail.coma écrit dans le message de news:
11**********************@q2g2000cwa.googlegroups.c om...

| A constructor such as public " TaggedAnimal(Animal currentAnimal) :
| base = currentAnimal " would ofcourse be ideal, but is clearly not
| supported. Maybe there is an event that is fired when the CLR is
| creating the base class that I can subscribe to and intercept?
| something along the lines of ...

You cannot change an object's type but you can either derive from or
encapsulate such objects.

Are you using VS2005 ? If so, a generic wrapper class would seem to be an
ideal solution.

public class TaggedAnimal<animalTwhere animalT : Animal
{
public static implicit operator TaggedAnimal<animalT(Animal input)
{
return new TaggedAnimal<animalT>(input);
}

public static implicit operator Animal (TaggedAnimal<animalTinput)
{
return input.animal;
}

private Animal animal;

... // extra properties and methods
}

This allows you to create things like this :

public class Zebra : Animal
{
...
}

{
Zebra z = new Zebra();

TaggedAnimal<ZebrataggedZebra = z;

...
}

Your ORM can then store instance of TaggedAnimal<animalT>, each of which
will hold a reference to the animal to which its tagged information belongs.

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Jan 21 '07 #22

P: n/a
The ORM can (and does) generate partial classes. The problem is that
the partial classes all have to be in the same module (i.e project).
This means that the partial classes I write for the extended
functionality will have to sit in the project generated by the ORM
tool, and I would rather not touch the generated project at all if i
can help it. (other than adding it to my solution)

Does anyone know how you can have partial classes that span two
different projects? by specifying that they are part of the same module
somehow?

I am trying out Joanna's solution now ...

Bruce Wood wrote:
ny********@hotmail.com wrote:
Bruce,
Forgive me for the use of the example, I see it has made some things a
bit murkier. The property I want to add is not necessarily the same for
all the classes. So I will have to subclass each of the generated base
classes/entities. For example another class (related to the database
table) might be "Equipment". I might want to add a property that
calculates the age of the equipment based on the current time and
"AcquiredDate" property of the Equipment class, then I would do
something similar like

public DatedEquipment : Equipment
{
public DatedEquipment(Equipment currentEquipment) : base =
currentEquipment
{}
public string Age
{
get
{
return (System.datetime.now - base.AcquiredDate).ToString();
/* .. Or something along those lines.*/
}
}
}

Clearly any custom properties/processing that I add to the Equipment
class will be different than that I add to the Anmial class so each of
the class will have to be subclassed again. (For the ones that need
custom properties/processing anyway).

But the problem you posed is very interesting as well, and that would
definitely be a cool pattern (not to mention some killer use of
generics).

The root of my problem is not so much that I need "dynamism" in my
inheritance hierarchy ( I can subclass each class as i said), its more
along the lines of "the base class has already been instantiated (by
the ORM, when i call "ThirdPartyORMTool.GetAllAnimals();"), the type
hierarchy has been defined statically at design time (which is fine),
but now I need to use the already existing base class when
instantiating a subclass"

What about using partial classes? Can your ORM tool generate partial
classes? Then you could actually alter each class in a separate .cs
file and the additional properties / methods you add would survive
regeneration of the class by the ORM tool.
Jan 22 '07 #23

P: n/a

Joanna Carter [TeamB] wrote:
<ny********@hotmail.coma écrit dans le message de news:
11**********************@q2g2000cwa.googlegroups.c om...

| A constructor such as public " TaggedAnimal(Animal currentAnimal) :
| base = currentAnimal " would ofcourse be ideal, but is clearly not
| supported. Maybe there is an event that is fired when the CLR is
| creating the base class that I can subscribe to and intercept?
| something along the lines of ...

You cannot change an object's type but you can either derive from or
encapsulate such objects.

Are you using VS2005 ? If so, a generic wrapper class would seem to be an
ideal solution.

public class TaggedAnimal<animalTwhere animalT : Animal
{
public static implicit operator TaggedAnimal<animalT(Animal input)
{
return new TaggedAnimal<animalT>(input);
}

public static implicit operator Animal (TaggedAnimal<animalTinput)
{
return input.animal;
}

private Animal animal;

... // extra properties and methods
}

This allows you to create things like this :

public class Zebra : Animal
{
...
}

{
Zebra z = new Zebra();

TaggedAnimal<ZebrataggedZebra = z;

...
}

Your ORM can then store instance of TaggedAnimal<animalT>, each of which
will hold a reference to the animal to which its tagged information belongs.

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Hi Joanna,

This runs into the same issues I had with using the decorator pattern.
While the object is cast as a TaggedAnimal, I can't automatically get
access to any methods/properties in the Animal class. The only way to
do this in a decorator would be to expose the underlying class with
another property and when I tie the TaggedAnimal to a grid for example,
I would have to do things like

<asp:BoundColumn HeaderText="Tag" DataField="Tag"></asp:BoundColumn>
<asp:BoundColumn HeaderText="Height"
DataField="Animal.Height"></asp:BoundColumn>
<asp:BoundColumn
HeaderText="Weight"DataField="Animal.Weight"></asp:BoundColumn>

But as I was saying in the previous post, it would be nice if the
TaggedAnimal actually retained its Type as Animal rather than being a
different type altogether.

By doing "public class TaggedAnimal<animalTwhere animalT : Animal", I
am still creating a new class type.

In the end it seems there really is no way of getting around the
decorator...

Jan 23 '07 #24

P: n/a
<ny********@hotmail.coma écrit dans le message de news:
11*********************@11g2000cwr.googlegroups.co m...

| But as I was saying in the previous post, it would be nice if the
| TaggedAnimal actually retained its Type as Animal rather than being a
| different type altogether.
|
| By doing "public class TaggedAnimal<animalTwhere animalT : Animal", I
| am still creating a new class type.
|
| In the end it seems there really is no way of getting around the
| decorator...

Then how about :

public class Animal
{
...

public Animal(Animal other) // copy constructor
{
// copy all fields from other
}
}

public class TaggedAnimal<animalT: Animal where animalT : Animal
{
public TaggedAnimal(animalT other) : base(other) // copy constructor
{
// initialise tagged stuff
}
}

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Jan 23 '07 #25

P: n/a


On Jan 23, 10:08 pm, "Joanna Carter [TeamB]" <joa...@not.for.spam>
wrote:
<nyathan...@hotmail.coma écrit dans le message de news:
1169516320.117368.87...@11g2000cwr.googlegroups.co m...

| But as I was saying in the previous post, it would be nice if the
| TaggedAnimal actually retained its Type as Animal rather than being a
| different type altogether.
|
| By doing "public class TaggedAnimal<animalTwhere animalT : Animal", I
| am still creating a new class type.
|
| In the end it seems there really is no way of getting around the
| decorator...

Then how about :

public class Animal
{
...

public Animal(Animal other) // copy constructor
{
// copy all fields from other
}

}public class TaggedAnimal<animalT: Animal where animalT : Animal
{
public TaggedAnimal(animalT other) : base(other) // copy constructor
{
// initialise tagged stuff
}

}Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
This requires me to change/add another constructor to the base class.
The base classes are automatically generated by the ORM tool and I
would rather not put any code in them.

Jan 24 '07 #26

P: n/a
On Jan 23, 8:27 pm, nyathan...@hotmail.com wrote:
On Jan 23, 10:08 pm, "Joanna Carter [TeamB]" <joa...@not.for.spam>
wrote:
<nyathan...@hotmail.coma écrit dans le message de news:
1169516320.117368.87...@11g2000cwr.googlegroups.co m...
| But as I was saying in the previous post, it would be nice if the
| TaggedAnimal actually retained its Type as Animal rather than being a
| different type altogether.
|
| By doing "public class TaggedAnimal<animalTwhere animalT : Animal",I
| am still creating a new class type.
|
| In the end it seems there really is no way of getting around the
| decorator...
Then how about :
public class Animal
{
...
public Animal(Animal other) // copy constructor
{
// copy all fields from other
}
}public class TaggedAnimal<animalT: Animal where animalT : Animal
{
public TaggedAnimal(animalT other) : base(other) // copy constructor
{
// initialise tagged stuff
}
}Joanna
--
Joanna Carter [TeamB]
This requires me to change/add another constructor to the base class.
The base classes are automatically generated by the ORM tool and I
would rather not put any code in them.
But you could use the partial class facility to add nothing more than a
copy constructor to the ORM-generated partial classes. The copy
constructor would survive regeneration by the ORM tool, and you could
still have TaggedAnimal exist outside the data layer by using Joanna's
example.

Jan 24 '07 #27

This discussion thread is closed

Replies have been disabled for this discussion.