Method to "override" shared members in child classes

I'm currently trying to create a kind of model in vb.net that can be used to create / retrieve records in a database.

I created a model of the main class with a common function for retrieving datasets, for example. Model.find (). Now I would like to create classes that inherit the main class of the model, for example. separate for users: UserModel.find () => "SELECT * FROM users".

Now I need to find a way to tell the class which it should use. I was thinking about the abstract string "table", which is a constant in each "child model", but how can this be implemented since it is not possible to override common members?

Thanks in advance!

Change Perhaps this will simplify things a bit, which I mean:

Public Class Model Public Shared _controller As Controller Public Shared table As String Protected Shared tableFields As String() Shared reader As Npgsql.NpgsqlDataReader Public Shared Function find() Dim a As ArrayList = New ArrayList 'Test if the tablefields are already known to the class, if not, get them If tableFields Is Nothing Then getTableFields() End If Dim query As String = "SELECT " + String.Join(", ", tableFields) + " FROM " + table reader = _controller.executeReader(query) While reader.Read o = New Model Dim v As New Hashtable For Each field In tableFields v(field) = reader(field) Next o.values = v a.Add(o) End While reader.Close() Return DirectCast(a.ToArray(GetType(Model)), Model()) End Function Public values As Hashtable Public Sub New() End Sub End Class 

So, I need a generic method that finds all the records in the database and returns an array of instances of its own type, for example. Model(). This is why I wanted to keep the general search method and not tied to the instance.

+6
source share
4 answers

I think you could use Generics . Here I inserted an example

All classes in your domain can inherit from the Entity class.

 Public MustInherit Class Entity '... End Class 

Your model class using the Find method

 Public Class Model Public Shared Sub Find(Of T As Entity)() ' You could know the name of T to find the table Dim tableName As String = GetType(T).Name '... End Sub End Class 

One class of your domain, for example: User class

 Public Class User Inherits Entity ' ... End Class 

And finally, an example of how you could create a Find method

 Model.Find(Of User)() '... 

I don’t know if this is what you mean, do you find it useful?

+4
source share

You can make your main class abstract, and each subclass will have to return its own table name through its own implementation (e.g. getTableName ). Thus, you will only need to maintain the logic of the method in the main class.

0
source share

In such cases, the Singleton design pattern is usually used: create an instance method overridden by inheriting classes. Each class inherits this instance method to return a Singleton object belonging to this class.

Here is one way to do this:

 MustInherit Class BaseClass Public MustOverride Function getTableName() As String End Class Class Class1 Inherits BaseClass Private Shared TableName As String = "myTable1" Public Overrides Function getTableName() As String Return TableName End Function End Class Class Class2 Inherits BaseClass Private Shared TableName As String = "myTable2" Public Overrides Function getTableName() As String Return TableName End Function End Class 

EDIT: A completely different approach. You can have a base class to hold some kind of dictionary that associates class types (or type names) with the correct table:

 Class BaseClass Private Shared myDictionary As New Collections.Generic.Dictionary(Of Type, String) Friend Shared Sub RegisterType(ByVal childType As Type, ByVal tableName As String) myDictionary.Add(childType, tableName) End Sub Public Shared Function getTableName(ByVal childType As Type) As String Return myDictionary.Item(childType) End Function End Class Class Class1 Shared Sub New() BaseClass.RegisterType(GetType(Class1), "table1") End Sub End Class Class Class2 Shared Sub New() BaseClass.RegisterType(GetType(Class2), "table2") End Sub End Class 
0
source share

General (static) objects or members of an object cannot be inherited or redefined. Inheritance is, for example, an object. Since you do not need to instantiate a static class, you cannot inherit it. Same thing with methods. A static method does not have to be virtual (Overridable in VB), because it defines a method that performs tasks without an instance of the class. Then this makes it impossible to use the fields or properties of the instance in the static (Shared in VB) method. This is a bad design that tries so.

In fact, each static (Shared) class should be marked as NotInheritable in VB and only define an empty default constructor. This is a VB leak regarding OOP concepts.

0
source share

All Articles