Inheriting a subclassed base class that has children issues

Inheriting a subclassed base class that has children issues

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


Tommybouy posted on Thursday, December 07, 2006

I did read a thread that starting talking about a similar situation but it never really answered my question.

As per suggestions here and from Rocky, we have subclassed all of the base classes to create our own baseclasses that we inherit from. We have an Entity object that inherits from this and has child collections of addresses and contact numbers. We also have Classes for Customers, Employees and Vendors that inherit from Entity. One of the problems we encountered was in the child classes that are children of Entity. There are many places in the code where the parent is passed as a parameter but because we have declared the Entity class like this;  

Partial Public MustInherit Class Entity(Of T As Entity(Of T))

   Inherits ACSBusinessBase(Of T)

we receive errors about too few type arguments if we pass Entity as the parent class. I have read about possibly using interfaces in this situation but I am unsure of what is best. We use the CodesSmith generator tool and we are VBers in case you didn't already figure that one out.

Thanks in advance for any help you can offer.

Tom

Brian Criswell replied on Thursday, December 07, 2006

Which line is the too few type arguments actually referring to?  And what is the definition of ACSBusinessBase?

Tommybouy replied on Tuesday, December 12, 2006

The ACSBusinessBase is defined as:

<Serializable()> _

Public MustInherit Class ACSBusinessBase(Of T As ACSBusinessBase(Of T))

#Region "Declarations"

   Inherits Csla.BusinessBase(Of ACSBusinessBase(Of T))

#End Region ' Declarations

The too few arguments error happens in many places such as

<NonSerialized()> _

Private executeInsertDelegate As predicateEx(Of SqlConnection, Entity)

Private Function OnExecuteInsert(ByVal cn As SqlConnection, ByVal parent As Entity) As Boolean

   If Not executeInsertDelegate Is Nothing Then

      Return executeInsertDelegate(cn, parent)

   End If

   Return True

End Function

It seems like the only way to get the error to go away is change Entity to Customer or Employee in these situations. That defeats the purpose though of having the the classes be children of Entity.

Thanks

xal replied on Tuesday, December 12, 2006

You don't have many possibilities...
You can change that "Entity" to be an interface.
You could declare your base class like


Public MustInherit Class ACSBusinessBase(Of T As ACSBusinessBase(Of T), P As Entity(Of P))

'Where P is parent type

And then in your method you use

Private Function OnExecuteInsert(ByVal cn As SqlConnection, ByVal parent As P) As Boolean

Although this is a workable solution, it might make it a pain in the ass to maintain. I'd say go with interfaces. There is probably a set of common properties that you'll want to access from the entity type, so if you make that an interface, you have no problems left...

You could also change the method to take an "Object", and cast to the correct type in the delegate's handler...

Andrés

Tommybouy replied on Tuesday, December 12, 2006

Thank you for your ideas. Is there a good interface example somewhere? It sounds like the best solution to use. I was reading one of Rocky's responses and his question was if the parent "acts-as" something different from itself then an interface was the best way. I'm just hoping to see and axample of it so I do it correctly.

Tom

xal replied on Tuesday, December 12, 2006

This is how you define an interface

Public Interface IEntity
    Property EntityName As String
    ReadOnlyProperty EntityId As Guid
    Function GetSomeData(paramA as Integer, paramB as String) as String
    Sub SetSqlCommandParams(cmd as SqlCommand)
End Interface

Notice the interface doesn't worry about implementation or visibility (Public, private, Protected, etc)

So, then you go and create your Entity class:

Public Class Entity(Of T As Entity(Of T))1
Inherits YourBaseClass(Of T)
Implements IEntity

Public Property EntityName() As String Implements IEntity.EntityName
...
End Property

End Class


VS will be a sport and help you out in that it will auto gen the structure of the code as soon as you hit "Enter" in the implements line.

I hope it helps!

Andrés

Copyright (c) Marimer LLC