How to create a CSLA template/base object?

How to create a CSLA template/base object?

Old forum URL: forums.lhotka.net/forums/t/3960.aspx


HerbertLehner posted on Thursday, November 29, 2007

Hi,

I created an approval system which consists of several workflow types with questionnaires and with attached approval process. The resulting CSLA objects are largely identical (workflow start/end date etc. [approx. 40 identical properties and 10 identical methods). My idea was to create a template object from which the specialized workflow objects can inherit. I started like this:

<Serializable()> _
Public Class WF_Base
  Inherits BusinessBase(Of WF_Base)

... base properties and methods

end class

Then I created a specialized CSLA workflow object which inherits from the template:

<Serializable()> _
Public Class WF_Type1
  Inherits WF_Base

... special properties and methods

  Public Overrides Function Save() As WF_Type1
    ... do something here
    Return MyBase.Save()
  End Function


end class


The problem is, that the Save-method of WF_Base returns type WF_Base and the overriden Save-method of WF_Type1 returns WF_Type1 which is not allowed.

Does anyone know how to solve the problem?

Thanks,

Herbert

JoeFallon1 replied on Thursday, November 29, 2007

Try something like this:

Public Overrides Function Save() As WF_Type1
 
'Return MyBase.Save

' ... do something here

  If IsDirty Then
   
Return DirectCast(DataPortal.Update(Me), WF_Type1)
 
Else
   
Return DirectCast(Me, WF_Type1)
 
End If

End Function

 

Joe

HerbertLehner replied on Friday, November 30, 2007

Thanks for the tip, but it solves only one half of the problem:

Public Overrides Function Save() As WF_Type1

is still defined to return WF_Type1 an causes a compiler error.

Herbert


triplea replied on Friday, November 30, 2007

What if you try the following (I'm not too good in VB so will give the example in C# and a poor attempt in VB):

C#:
public abstract class WF_Base<T> : BusinessBase<T> where T : WF_Base<T> { ... }
VB:
Public Class WF_Base<T> Inherits BusinessBase(Of WF_Base) Where T : WF_Base<T>

Then define your concrete classes as follows:

C#:
public class WF_Type1: WF_Base<WF_Type1>
{
   public override WF_Type1 Save() { ...  return base.Save(); }
}
VB:
Public Class WF_Type1 Inherits WF_Base<Of WF_Type1)
   Public Overrides Function Save()
      Return MyBase.Save()   'This should now be of type WF_Type1
   End Function
End Class

JoeFallon1 replied on Friday, November 30, 2007

I am not sure why you are getting a compile error.

I use that trick a lot.

Is there some reason you even have a Save method in Base?

If you are overriding it anyway then try removing it.

Joe

 

Copyright (c) Marimer LLC