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

  • Thread starter Thread starter nyathancha
  • Start date Start date
Bruce said:
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.
 
<[email protected]> a écrit dans le message de (e-mail address removed)...

| 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<animalT> where animalT : Animal
{
public static implicit operator TaggedAnimal<animalT> (Animal input)
{
return new TaggedAnimal<animalT>(input);
}

public static implicit operator Animal (TaggedAnimal<animalT> input)
{
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<Zebra> taggedZebra = 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
 
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 ...
 
Joanna said:
<[email protected]> a écrit dans le message de (e-mail address removed)...

| 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<animalT> where animalT : Animal
{
public static implicit operator TaggedAnimal<animalT> (Animal input)
{
return new TaggedAnimal<animalT>(input);
}

public static implicit operator Animal (TaggedAnimal<animalT> input)
{
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<Zebra> taggedZebra = 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

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<animalT> where 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...
 
<[email protected]> a écrit dans le message de (e-mail address removed)...

| 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<animalT> where 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
 
<[email protected]> a écrit dans le message de (e-mail address removed)...

| 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<animalT> where 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

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

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

Back
Top