I have a base class called MyBase which inherits from Csla.BusinessBase(Of MyBase) like so
Protected Class MyBase(Of T As BusinessBase(Of T))
Inherits BusinessBase(Of T)
End Class
I then have another class that inherits from MyBase, called MyParent, like so
Public Class MyParent(Of T As MyBase(Of T))
Inherits MyBase(Of T)
End Class
Now 2 questions really...
1. Is this the correct way to implement inheritance, using the Of T command, like Csla?
2. I thought inheritance was supposed to be done like this because I read a while ago that inheriting objects should declare proeprties using the RegisterProperty constructor that accepts a Type paramter, like so
Private Shared IdProperty As PropertyInfo(Of Integer) = RegisterProperty(GetType(MyBase(Of T)), New PropertyInfo(Of Integer)("Id"))
Is this still the case? Do you need the specify the type parameter on any object that uses inheritance? If not, then i wonder if my objects could be simplified to something like
Protected Class MyBase(Of T As BusinessBase(Of T))
Inherits BusinessBase(Of T)
End Class
Public Class MyParent
Inherits MyBase(Of MyParent)
End Class
So as stated before, what is the correct way to implement inheritance in csla? And which is the correct/preferred way to define Properties in objects with inheritance?
Thanks in advance
Hi,
1. Yes, this is the correct way to implement inheritance for generic classes. If you need a common access method the the base class you should also create an Interface for each base class with the properties/methods.
2. You are not required to use the RegisterProperty methods that specify the actual type.
In C# the statement
public static readonly PropertyInfo<string> NameProperty = RegisterProperty<string>(c => c.Name);
will use the generic constraint on the class to find the type.
You cannot create a non-generic MyParent class and continue to inherit from that class. Only the "last" class can be non-generic. So your simplified solution will give a runtime exception when you run (due to how CSLA register the properties on a Type).
JonnyBee,
Thanks so i guess i am doing it correctly at the moment then.
So, say i have an object and i need to cast it as MyBase, i should create an interface called IMyBase and cast the object using that interface?
Dim lMyObject As IMyBase = DirectCast(Session("Object_In_Session"), IMyBase)
Is this what you are saying, as that was going to be my next question, as you cannot cast something to an object Of T
Yes,
As the base class is generic and anyone interested in the common properties/methods should not have knowledge of the actual subclass (the generic constraint) and thus you need a non-generic interface to expose the properties/methods.
Copyright (c) Marimer LLC