Static inheritence

  • Thread starter Ren? Paw Christensen
  • Start date
R

Ren? Paw Christensen

Hi.

Considering the following code, I want this call:

Something.Method();

To return "Something".

public class BaseClass {
public static string Method() {
return <type where the method was called>.Name;
}
}

public class Something : BaseClass {
}

The code illustrates the problem, but in real life I won't return the
name of the Something type, but rather use the Something type in my
implementation in the BaseClass. Is this possible?
__________________________________________________________

If the above is not possible one solution is this:

public class BaseClass {
public static string Method(Type theType) {
return theType.Name;
}
}

public class Something : BaseClass {
}

But then I have to call it like this:

Something.Method(typeof(Something));



Live Long and Prosper
René Paw Christensen
 
N

Nick Malik

You can use a strategy pattern, but I don't know if it will work for static
members... any reason why you can't use a singleton instead of inheriting a
class with static members?

public class BaseClass {
protected abstract void PerformHiddenFunction();
public string VisibleMethod()
{
// do some stuff
this.PerformHiddenFunction();
// do more stuff
}
}

public class ChildClass : BaseClass
{
protected override void PerformHiddenFunction()
{
// do stuff that only this class would know how to do.
}
}

In the calling code:
ChildClass c = new ChildClass();
c.VisibleMethod(); // this will call the "PerformHiddenFunction"
method defined in the child class,
//even though the VisibleMethod is
defined in the
//base class and is not overridden in
the child class.


HTH

--- Nick
 
R

Ren? Paw Christensen

Hi Nick.

Thank you for the reply.
You are right, I can insted use non static methods. This works:

public class BaseClass {
public string Method() {
return GetType().Name;
}
}

public class Something : BaseClass {
}

This calling code returns "Something":

Something s = new Something();
s.Method(); // Returns "Something"

This requires me to create an instance of the dereived Something
class, which is possible, but often in this case I only need to work
with the Something type and reflections.





This is what I'm doing.

I'm creating a data class which represents one record in a database.
This class can perform operations like Save, Delete etc. and it also
has some general static methods like CreateTable, DeleteTable,
GetAllRecords, CountAllRecords etc.

My data class is the base class, each database table is represented
with a class inheriting the base data class. The properties which
represents a field in the database table is decorated with an
attribute, containing the field name, type, key status etc.

Example: My base data class has a static CreateTable method. It needs
to read the attributes on the type it should work with, then create
the SQL query and execute in in the database.
Currently I have to pass the type of the class to the static
CreateTable method, or I can make it non static and then create a
instance of the child class and call the CreateTable method. But then
I have these null objects, which don't represent a record in the
table.

In your example, I should implement a CreateTable method in each child
class, plus all the other methods.

Some code to illustrate my text above - I hope it helps the
understanding of what I'm doing.


public abstract class DataBase {
public static void CreateTable(type type) {
... get the attributes of the type
... create SQL query
... execute query
}
public static void DeleteTable(Type type) .....
... more static methods
public static ArrayList GetAllRecords(Type type) {
... get the attributes of the type
... create SQL query
... execute query and get DataView
... foreach record in the DataView
... use reflection to create a new instance of the
type
... set the property values using reflection and the
attributes
... return the objects in a ArrayList
}

public void Save() { // Non static, so no problem here!
... get the attributes of the GetType() type
... use reflection to get the property values
... create SQL query
... execute query
}
public void Delete() .....
... more non static methods
}

Then each time i need a table in the database, I create and use a
class like this:

[TableAttribute("TableName")]
public MyTable : DataBase {
// Field name, type, length, is key, unique
[FieldAttribute("Guid", FieldType.Char, 50, true, false)]
public Guid Key {
...
}
[FieldAttribute("CustomerName", FieldType.Char, 255, false,
false)]
public string Name {
...
}
public string PostingAddress {
// No attribute, so this is not a field in the database
table
get {
return Name + "\r\n" + Address;
}
}
}

Now, I can have several MyTable like classes, and I only want to put
as little in them as possible. If i want all my MyTable records I use
this code:

MyTable.GetAllRecords(typeof(MyTable));

But I would like to do it like this:

MyTable.GetAllRecords();

Finally I could go non static in the DataBase class and have an empty
MyTable object:

MyTable mt = new MyTable();
mt.GetAllRecords();


Live Long and Prosper
René Paw Christensen
 
N

Nick Malik

Interesting design,
Thanks for sharing.

Did you want any feedback on the design?

--- Nick

Ren? Paw Christensen said:
Hi Nick.

Thank you for the reply.
You are right, I can insted use non static methods. This works:

public class BaseClass {
public string Method() {
return GetType().Name;
}
}

public class Something : BaseClass {
}

This calling code returns "Something":

Something s = new Something();
s.Method(); // Returns "Something"

This requires me to create an instance of the dereived Something
class, which is possible, but often in this case I only need to work
with the Something type and reflections.





This is what I'm doing.

I'm creating a data class which represents one record in a database.
This class can perform operations like Save, Delete etc. and it also
has some general static methods like CreateTable, DeleteTable,
GetAllRecords, CountAllRecords etc.

My data class is the base class, each database table is represented
with a class inheriting the base data class. The properties which
represents a field in the database table is decorated with an
attribute, containing the field name, type, key status etc.

Example: My base data class has a static CreateTable method. It needs
to read the attributes on the type it should work with, then create
the SQL query and execute in in the database.
Currently I have to pass the type of the class to the static
CreateTable method, or I can make it non static and then create a
instance of the child class and call the CreateTable method. But then
I have these null objects, which don't represent a record in the
table.

In your example, I should implement a CreateTable method in each child
class, plus all the other methods.

Some code to illustrate my text above - I hope it helps the
understanding of what I'm doing.


public abstract class DataBase {
public static void CreateTable(type type) {
... get the attributes of the type
... create SQL query
... execute query
}
public static void DeleteTable(Type type) .....
... more static methods
public static ArrayList GetAllRecords(Type type) {
... get the attributes of the type
... create SQL query
... execute query and get DataView
... foreach record in the DataView
... use reflection to create a new instance of the
type
... set the property values using reflection and the
attributes
... return the objects in a ArrayList
}

public void Save() { // Non static, so no problem here!
... get the attributes of the GetType() type
... use reflection to get the property values
... create SQL query
... execute query
}
public void Delete() .....
... more non static methods
}

Then each time i need a table in the database, I create and use a
class like this:

[TableAttribute("TableName")]
public MyTable : DataBase {
// Field name, type, length, is key, unique
[FieldAttribute("Guid", FieldType.Char, 50, true, false)]
public Guid Key {
...
}
[FieldAttribute("CustomerName", FieldType.Char, 255, false,
false)]
public string Name {
...
}
public string PostingAddress {
// No attribute, so this is not a field in the database
table
get {
return Name + "\r\n" + Address;
}
}
}

Now, I can have several MyTable like classes, and I only want to put
as little in them as possible. If i want all my MyTable records I use
this code:

MyTable.GetAllRecords(typeof(MyTable));

But I would like to do it like this:

MyTable.GetAllRecords();

Finally I could go non static in the DataBase class and have an empty
MyTable object:

MyTable mt = new MyTable();
mt.GetAllRecords();


Live Long and Prosper
René Paw Christensen
 
R

Ren? Paw Christensen

Hi Nick.

Any comments and ideas are welcome.

I'm still concidering which solution to use:

* Static method with Type argument
* Non static, requires a "null/empty" object instance
* Data object scanner class 1)

1) A one instance database object, which scans the active assembly
(loaded types) for classes decorated with the table attribute, and
then add a argument to the table attribute, a Guid. Then i can do
something like this:

theDatabaseEngine.CreateTagle(<the guid>);

It then lookup the type to work with in its internal type cache
(Hashtable) of scanned and found data objects.
The guid(s) can be stored in a struct or something.


Thanx.


Live Long and Prosper
René Paw Christensen
 
N

Nick Malik

It just seems like you are creating a fairly complicated method of driving
the creation of database tables from code, when database tables are only
created from code ONCE, and then have to be tweaked after you are done
anyway, to get the declarative referential integrity right.

Also, the code that you were having some trouble with, at the start of this
thread, is code that deals with Insert, Update, and Delete methods on the
table, and there is a perfectly servicable object that has working code for
this: the DataTable object. Why not just encapsulate the DataTable object,
or even the DataAdapter, under your object. You won't need to code anything
for the table operations... and you can focus on the DDL extensions that the
DataSet object doesn't already provide.

In other words, why write a new object when you can extend an existing one?

Anyway, good luck, whatever road you choose.

--- Nick
 

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