Intermediate base classes in VB

Intermediate base classes in VB

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


nhwilly posted on Thursday, October 01, 2009

I read in the forums about putting an intermediate layer in the inheritance hierarchy so that changes in CSLA don't break my code. So I am going back to working code and trying to introduce this concept. I see the CSLA is moving ahead nicely and I want to keep up.

JoeF says to look on the forums for posts about this, but I can't find any VB examples.

Can anyone tell me what the syntax is for an intermediate subclass of :

BusinessListBase(of T, C)

This is making me nuts. Correction, more nuts.

Thanks in advance.

RockfordLhotka replied on Thursday, October 01, 2009

I think it is like this:

Public MustInherit Class MyBusinessBase(Of T As MyBusinessBase(Of T))
  Inherits BusinessBase(Of T)

The trick is to remember to put the constraints on the generic type(s).

nhwilly replied on Thursday, October 01, 2009

Thanks, Rocky.

Am I to understand that there must be a unique intermediate class for each of my subclasses?

Like this?

Public MustInherit Class MerchantProfileBase(Of T As MerchantProfileBase(Of T))
Inherits BusinessBase(Of T)
End Class

Public Class MerchantProfile
Inherits MerchantProfileBase(Of MerchantProfile)
End Class

Public MustInherit Class MerchantProfileListBase(Of T As MerchantProfileListBase(Of T, C), C As MerchantProfileBase(Of C))
Inherits BusinessListBase(Of MerchantProfileListBase(Of T, C), MerchantProfileBase(Of C))
End Class

Public Class MerchantProfileList
Inherits MerchantProfileListBase(Of MerchantProfileList, MerchantProfile)
End Class

JoeFallon1 replied on Thursday, October 01, 2009

These are what my intermediate base classes look like: (minus my common code)

-------------------------------------------------------------------------------------------------------------------------------------

<Serializable()> _
Public MustInherit Class MyBusinessBase(Of T As MyBusinessBase(Of T))
  Inherits BusinessBase(Of T)
End Class

-------------------------------------------------------------------------------------------------------------------------------------

<Serializable()> _
Public MustInherit Class MyBusinessListBase(Of T As MyBusinessListBase(Of T, C), C As MyBusinessBase(Of C))
  Inherits BusinessListBase(Of T, C)
End Class

-------------------------------------------------------------------------------------------------------------------------------------

<Serializable()> _
Public MustInherit Class MyReadOnlyBase(Of T As MyReadOnlyBase(Of T))
  Inherits ReadOnlyBase(Of T)
End Class

-------------------------------------------------------------------------------------------------------------------------------------

<Serializable()> _
Public MustInherit Class MyReadOnlyListBase(Of T As MyReadOnlyListBase(Of T, C), C As Core.IReadOnlyObject)
 
Inherits ReadOnlyListBase(Of T, C)
End Class

-------------------------------------------------------------------------------------------------------------------------------------

<Serializable()> _
Public MustInherit Class MyNameValueListBase(Of K, V)
  Inherits NameValueListBase(Of K, V)
End Class

-------------------------------------------------------------------------------------------------------------------------------------

<Serializable()> _
Public MustInherit Class MyCommandBase
  Inherits Csla.CommandBase
End Class

-------------------------------------------------------------------------------------------------------------------------------------

<Serializable()> _
Public MustInherit Class MyEditableRootListBase(Of T As {IEditableBusinessObject, ISavable})
  Inherits Csla.EditableRootListBase(Of T)

  Protected Sub New()
    Me.AllowEdit = True
    Me.AllowNew = True
    Me.AllowRemove = True
  End Sub

End Class

-------------------------------------------------------------------------------------------------------------------------------------

HTH
Joe

 

 

cmclernon replied on Tuesday, October 13, 2009

JoeFallon1:

These are what my intermediate base classes look like: (minus my common code)


-------------------------------------------------------------------------------------------------------------------------------------



_
Public MustInherit Class MyBusinessListBase(Of T As MyBusinessListBase(Of T, C), C As MyBusinessBase(Of C))
  Inherits BusinessListBase(Of T, C)
End Class


-------------------------------------------------------------------------------------------------------------------------------------




HTH
Joe


 


 



Hi Joe,
I have something like this too from your previous examples but I cannot seem to add in the IBusinessCollection interface for the broken rules. Can you post up the syntax you used for this? I have the following:

[Serializable()]
public abstract class MyBusinessListBase : BusinessListBase
where T : MyBusinessListBase
where C : MyBusinessBase,
IBusinessCollection
{}

but in the "IBusinessCollection.Item(int index, bool useInterface)" implementation there is no base.Item definition...the full compiler error is:

'Csla.BusinessListBase' does not contain a definition for 'Item'

Hope this makes sense - any help would be greatly appreciated.

Colm

JoeFallon1 replied on Tuesday, October 13, 2009

cmclernon:

Hi Joe, I have something like this too from your previous examples but I cannot seem to add in the IBusinessCollection interface for the broken rules. Can you post up the syntax you used for this? I have the following: [Serializable()] public abstract class MyBusinessListBase : BusinessListBase where T : MyBusinessListBase where C : MyBusinessBase, IBusinessCollection {} but in the "IBusinessCollection.Item(int index, bool useInterface)" implementation there is no base.Item definition...the full compiler error is: 'Csla.BusinessListBase' does not contain a definition for 'Item' Hope this makes sense - any help would be greatly appreciated. Colm

You are getting into my own specific code now. This is not something that is generally needed by everyone. I needed it over the years so I wrote it. If you really want to add then here it is:

Public Interface IBusinessCollection
  ReadOnly Property Item(ByVal index As Integer, ByVal useInterface As Boolean) As IMyBusinessObject
  ReadOnly Property IsDirty() As Boolean
  ReadOnly Property IsValid() As Boolean
  ReadOnly Property Count() As Integer
  ReadOnly Property IsSelfValid() As Boolean
End Interface

The interface above is declared separately from the list base class. 

In the list base class you implement the interface like this:

'useInterface is a fake parameter and is only used to change the method signature so we can Overload the Item method.
'It does not use the Boolean value at all.
'This way the Base Item method can still return a strongly typed child BO.
'This Item method returns the Interface version of the child BO as IEditableBusinessObject.
Public Overloads ReadOnly Property Item(ByVal index As Integer, ByVal useInterface As Boolean) As IMyBusinessObject Implements IBusinessCollection.Item
  Get
    Return MyBase.Item(index)
  End Get
End Property

Public Overloads ReadOnly Property Count() As Integer Implements IBusinessCollection.Count
  Get
   
Return MyBase.Count
 
End Get
End Property

Public Overloads ReadOnly Property IsDirty() As Boolean Implements IBusinessCollection.IsDirty
 
Get
   
Return MyBase.IsDirty
 
End Get
End Property

'JF 9/14/2006 - override CSLA code to look like this - no short circuiting involved here.
'run through all the child objects and if any are invalid then the collection is invalid
'call IsValid on all children so that when we examine Broken Rules we get the right set of values.
'Rocky stopped looping after he found the first invalid child.
Public Overrides ReadOnly Property IsValid() As Boolean Implements IBusinessCollection.IsValid
 
Get
   
Dim result As Boolean = True
   
For Each child As C In Me
     
If Not child.IsValid Then
       
result = False
     
End If
   
Next
   
Return result
 
End Get
End Property

Public ReadOnly Property IsSelfValid() As Boolean Implements IBusinessCollection.IsSelfValid
 
Get
   
Dim result As Boolean = True
   
For Each child As C In Me
     
If Not child.IsSelfValid Then
       
result = False
     
End If
   
Next
   
Return result
 
End Get
End Property

As I recall the main reason for writing this code and interface was so that I could treat every collection the same way for my reporting framework. I could cast it to the interface and then was guaranteed these properties existed.

Joe

 

cmclernon replied on Tuesday, October 13, 2009

Thanks for that Joe,

I'm at home now so will try it again tomorrow. My problem was the definition of MyBusinessListBase class. From memory I'm now using the following syntax (C#) but am getting an error and was wondering if you could post your working VB syntax and I could try and translate it:

[Serializable()] public abstract class MyBusinessListBase : BusinessListBase, IBusinessCollection where T : MyBusinessListBase where C : IMyBusinessObject
{
...
}

Thanks,

Colm

cmclernon replied on Tuesday, October 13, 2009

Don't know why but the post is removing the T, C definition where MyBusinessListBase is used.

JoeFallon1 replied on Wednesday, October 14, 2009

cmclernon:
Thanks for that Joe, I'm at home now so will try it again tomorrow. My problem was the definition of MyBusinessListBase class. From memory I'm now using the following syntax (C#) but am getting an error and was wondering if you could post your working VB syntax and I could try and translate it: [Serializable()] public abstract class MyBusinessListBase : BusinessListBase, IBusinessCollection where T : MyBusinessListBase where C : IMyBusinessObject { ... } Thanks, Colm

I posted all of my definitions at the top of this thread.

Joe

 

cmclernon replied on Wednesday, October 14, 2009

Thanks Joe,

There's no mention of the IBusinessCollection interface in the MyBusinessListBase definition - I couldn't get it to work as I expected it to in C#. However, I have got around my problem and no longer need to implement the IBusinessCollection interface.

Thanks for your help.

Colm

RockfordLhotka replied on Thursday, October 01, 2009

Not if your intent is just to have a common extension point, no. You should
have one base class per CSLA base class - so typically 6 of them:

BusinessBase
BusinessListBase
ReadOnlyBase
ReadOnlyListBase
NameValueListBase
CommandBase

And maybe

CriteriaBase
ObjectFactory

nhwilly replied on Friday, October 02, 2009

I just had a huge case of deja vu.

I took a computer course in college (1972 thank you very much) and the professor had extended PL/1 into PL/C (the C was for the college name).

His intent was to make the compiler correct the student's programming errors and attempt to compile and execute. Ambitious to say the least.

Most of the time the compiler got it wrong and took a severe left turn but went on to compile. As a student you were left not only debugging your mistakes, but the compiler's attempt to recover from them. It was the most confusing nonsense ever.

Fast forward...

I didn't really understand the class definition statement with respect to generics and Intellisense (which I absolutely think is great) cheerfully led me down the path of wrong (but compilable) definitions for the intermediate classes.

I suppose this makes you guys the professors now.

Only this time, I actually did read the assigned pages (all 700+ of them).

Thanks to you both. I shudder to think what I'd be doing if I hadn't discovered CSLA and this community.

I think "Professor Rocky" has a nice ring to it. :D

nhwilly replied on Friday, October 02, 2009

P.S. Joe, thanks for the code. It helped a lot. I've already made the changes and it's working great.

Copyright (c) Marimer LLC