CSLA .NET

Vibrant discussion about CSLA .NET and using the framework to build great business applications.

Class Inheritance and Business Base

rated by 0 users
This post has 13 Replies | 2 Followers

Top 500 Contributor
Posts 38
Tim FOrd Posted: Thu, Jun 1 2006 4:14 AM
Hi All,

I need a little with the the understanding of the BusinessBase class and the use of the (of section)

I have 1 class called BestDay which has a set of properties etc

Public Class Bestday : Inherits BusinessBase(Of Bestday)

I also have a need for a customer Bestday Class and Location BestDay Class because i need to reuse all the properties from the bestday class and extended the interface with a customer id and location id. Is this possible??, i have tried this.

Public Class CustomerBestday : Inherits BusinessBase(Of Bestday)
Public Class LocationBestday : Inherits BusinessBase(Of Bestday)

But i can't and of the underlining bestday properties when i call this code from the interface.

        Dim objCustomerBestDay As BusinessObjects.CustomerBestday
        objCustomerBestDay.???? I only see the CustomerID property which i defined in CustomerBestdays

If this is possible i take it i will be able to overrides the dataportal section as i will need to call different storedprocedures to reutrn data. I have tried to explain this as best as possible, but as i'm starting out i have a feeling i might trying to run before i can walk.

Any help would be greatfully appreciated.

Thanks, Tim.
Top 10 Contributor
Posts 4,019
Andy replied on Thu, Jun 1 2006 8:26 AM
I think you need to change:

Public Class CustomerBestday : Inherits BusinessBase(Of Bestday)
Public Class LocationBestday : Inherits BusinessBase(Of Bestday)

to

Public Class CustomerBestday Inherits Bestday(Of Bestday)
Public Class LocationBestday Inherits Bestday(Of Bestday)


HTH
Andy
Top 500 Contributor
Posts 38
Hi Andy,

Thanks for you reply, i have tried both ways with no success. To to clarify i would like to inherit an object that has inherited the busines base object but still have the ability to over write methods etc. in the top level class.

so it would be like this
  • business base
    • best day
      • customer best day
        • location best day
the customer and location class would have a nother property attached to them to define the customer and location id. But i would still like to override the fetch methods etc to call different sotred procedures.

I know this is not very clear, but it the best that i can exaplin on what i'm trying to acheive.

Thanks, Tim.
Top 10 Contributor
Posts 4,019
Andy replied on Thu, Jun 1 2006 8:43 AM
Hmm,

What about:

Public Class CustomerBestday Inherits Bestday(Of CustomerBestDay)
Public Class LocationBestday Inherits CustomerBestday (Of LocationBestday )

Sorry, I'm not very good with VB; it coudl just be that you need to recompile or restart the IDE. 

I know there are a few rare occasions when VS2003 would 'forget' some of the properties / methods of an object because Intillisense gets trashed.

Andy
Top 500 Contributor
Posts 38
Right lets get it simpler.

BestDay class inherits BusinessBase(of BestDay)

I need to extend the interface of best day in to a CustomerBestDay class

Which i'm pretty sure we are on the right line of

Public Class CustomerBestDay: Inherits Bestday(of CustomerBestDay)
Or
Public Class CustomerBestDay: Inherits Bestday(of BestDay)

when i try and define either of the above in the class i get this error:-

Error    24    'BusinessObjects.Bestday' has no type parameters and so cannot have type arguments.

Cheers for your help andy.

Tim. 
Top 10 Contributor
Posts 9,431
Generics complicates inheritance rather a lot - that's one major drawback to their use.

You need to declare BestDay as a generic base class (and thus probably abstract):

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

Pretty much just like BusinessBase is declared. Then you can create your subclass:

Public Class CustomerBestDay : Inherits BestDay(Of CustomerBestDay)

Make sure you are using CSLA 2.0.1, because it includes a fix to allow the data portal to work properly with this sort of inheritance scheme.

If this doesn't get you want you want, then you need to avoid using generics - at least all the way through your hierarchy. Specifically, if you want to be able to create an instance of BestDay directly, then that must be the end of the generic inheritance chain, and your CustomerBestDay can't have strongly typed Save() and Clone() methods - they'll be of type BestDay and you'll need to cast the results yourself.

Rocky

Top 500 Contributor
Posts 38
Guys, thanks for you help exactly what i wanted.
Top 500 Contributor
Posts 38
Guys,

I seem to be getting an error when i now try and create a new instance of the CustomerBestDay Class

    Public Shared Function NewBestDay() As BestDay(Of CustomerBestDay)

        If Not CanAddObject() Then
            Throw New System.Security.SecurityException("User not authorized to add a BestDay")
        End If

        Return DataPortal.Create(Of CustomerBestDay)()

    End Function

and this

    Public Shared Function NewBestDay() As BestDay(Of CustomerBestDay)

        If Not CanAddObject() Then
            Throw New System.Security.SecurityException("User not authorized to add a BestDay")
        End If

        Return DataPortal.Create(Of BestDay(Of CustomerBestDay))()

    End Function

The error i get is {"Cannot create an abstract class."}and i just call the create method incorrectly or am i barking up the wrong tree here??

Thanks, Tim.
Top 500 Contributor
Posts 38
Just for reference, i have had a look on the forums and found this http://forums.lhotka.net/forums/thread/1205.aspx which is exactly the same issue.

Tim.
Top 10 Contributor
Posts 518
Make sure your criteria class is either in your derived class or if you keep it in your base class, make it inherit from criteria base.

Andrés
Top 500 Contributor
Posts 38
i Have this in my CustomerBestDay Class is that right?? 

 <Serializable()> _
  Private Class Criteria
        Private mId As Guid
        Public ReadOnly Property Id() As Guid
            Get
                Return mId
            End Get
        End Property

        Public Sub New(ByVal id As Guid)
            mId = id
        End Sub
    End Class

Sorry for being slack, but i haven't really used a framwork like this before. So quite a steap learning curve.

thansk, Tim.
Top 10 Contributor
Posts 518
Sorry, I missread your message.
Your factory method should be inside CustomerBestDay.

Public Shared Function NewCustomerBestDay() As CustomerBestDay
        If Not CanAddObject() Then
            Throw New System.Security.SecurityException("User not authorized to add a BestDay")
        End If
        Return DataPortal.Create(Of CustomerBestDay)()
End Function


Criteria doesn't participate in create in this instance. Also, if you don't need to bring default values from the db you should either mark your dataportal_create method as RunLocal() or just do a:
 Return New CustomerBestDay
inside your factory method.

If you're still having trouble, post the exact factory method that fails and tell us where the factory method is. Also post the declarations for each of the classes.

Andrés
Top 500 Contributor
Posts 38
Hi xal,

I have just this  minute got it working, a small error on my side ;) i had declare customerbestdays as mustinherit and thus getting the error "Cannot create abstract class" sorry about that.

But as i'm learning can you please tell me which one is the correct way of define this function.

    Public Shared Function NewBestDay(ByVal CustomerID As Guid) As BestDay(Of CustomerBestDay)

        If Not CanAddObject() Then
            Throw New System.Security.SecurityException("User not authorized to add a bestday")
        End If

        Dim objCriteria As New Criteria(CustomerID)

        Return DataPortal.Create(Of CustomerBestDay)(objCriteria)

    End Function

OR

    Public Shared Function NewBestDay(ByVal CustomerID As Guid) As CustomerBestDay

        If Not CanAddObject() Then
            Throw New System.Security.SecurityException("User not authorized to add a bestday")
        End If

        Return DataPortal.Create(Of CustomerBestDay)()

    End Function

The main difference being the Method declaration


Top 10 Contributor
Posts 518
Well, both return a strongly typed object of type CustomerBestDay, the difference is semanthic I'd say. I'd go with "As CustomerBestDay" instead of the other because it's simpler and more straightforward.

Andrés
Page 1 of 1 (14 items) | RSS

Copyright (c) 2006-2014 Marimer LLC. All rights reserved.
Email admin@lhotka.net for support.
Powered by Community Server (Non-Commercial Edition), by Telligent Systems