This website is intended as archive for old content and forums.
Please visit http://opf3.codeplex.com for the project's new website.

General Discussion

FORUM TOPIC

What is Opf3? Why is Opf3? How is Opf3? All these burning questions and issues discussed here.

RSS
Steven
Posted on the 12/26/06 10:02 PM
A Question about inheritance
Reply Quote
 I'm having some trouble figuring something out and maybe its that im not used to creating databases for persistance. The good news here is that I can alter the design of the database to accomodate the persistance framework.

problem: I have an inheritance chain of objects that inherit from a baseclass (in this case Customer) these objects are Person and Company so (VB.Net here but C# samples are fine):

'---------------------------------------------------

Public MustInherit Class Customer
Public Property Id As Integer
  'Getter and Setter
End Property

Public Property Active As Boolean
  'Getter and Setter
End Property
End Class

Public class Person : Inherits Customer
End Class

Public class Company : Inherits Customer
End Class

'----------------------------------------------------


They have a large number of differing fields and as such are right now represented in two seperate tables. The only fields they do have in common

(inherited from Customer) would be ID and Active.

In the Database I currently have two tables: People, Companies.

I also have a table Called Orders which contains a CustomerId and CustomerType column (this is from the way I used to do things so this may be all wrong).

What I would like to do is be able to navigate from an Order Persistant to the Customer. Sounds easy right? It would be if they were in the same table (which would have something along the lines of a 20+ columns to handle the two different types of Customers). I know that if they were in the same table I could use a PersistantTypeSelector delegate to retrieve the correctly typed Object. However I would like to find some way to accomplish the same thing (more or less) without having to create a Customers table with 20+ columns.

What I have done now is to use the value of the CustomerType field to determine the Type of Persistant to set as T for a ObjectContext's GetObject(of T)("Id = {0}", Me.CustomerId) in a custom property called Customer of the Order object.

'-----------------------------------------------------
Public Partial Class Order

    Private _customer As Object

    Public ReadOnly Property Customer() As Object
        Get
            If IsNothing(Me._customer) Then

                Select Case Me.CustomerType
                    Case "PERSON"

                        Me._customer = ObjectContextFactory.GetObjectContext.GetObject(Of Person)(New Query.ObjectQuery(Of Person)("Id = {0}", Me.CustomerId))

                    Case "COMPANY"

                        Me._customer = ObjectContextFactory.GetObjectContext.GetObject(Of Company)(New Query.ObjectQuery(Of Company)("Id = {0}", Me.CustomerId))
                      
                End Select

            End If

            Return Me._customer
        End Get
    End Property

End Class
'------------------------------------------------------


question: What I would like to know is there a better way to do this? One with less custom code and logic? Other than creating a massive table with dozens of columns? Am I totally missing a concept here (wouldn't be the first time)? I'm looking for the most elegant solution with the easiest maintainable code.

I can redesign the database as needed so that isn't a problem.



Christian Liensberger [Moderator]
Posted on the 12/27/06 12:12 PM
RE: A Question about inheritance
Reply Quote
 This is a hard question. That's because you have one record in the database that could match to rows in two tables. Opf3 doesn't have an abbreviaton for that. You need to do some custom code here.

Do you have a lot such relations in your database?

Christian

Steven
Posted on the 12/27/06 1:54 PM
RE: A Question about inheritance
Reply Quote
 At this time yes I many tables that relate to the customers table in this same way. however this is mostly because I am used to creating a lot of custom code for handling the data. Something I would now prefer to do with opf3. There is no data in the database so I could change the database, I'm just not entirely too sure how to do that to accomplish the same goals in opf3.

Christian Liensberger [Moderator]
Posted on the 12/27/06 2:24 PM
RE: A Question about inheritance
Reply Quote
 Well. Opf3 doesn't do much here. In this case, you need to do some manual coding.

Another approach would be a re-design of the database to have only one table that holds the basic information (for a customer) and to which the other tables connect to. You then need to have other tables that hold the addtional information for the Person or Company.

Regards,
Christian

Steven
Posted on the 12/27/06 2:34 PM
RE: A Question about inheritance
Reply Quote
 I think a redesign of the tables is probably the best option (might still need some custom code but not as much). I figured this would be the case but wanted to make sure I wasn't missing something obvious. Thank you very much for your time on this.

I love opf3 and i didn't figure it could accomplish what i needed without custom code or altering the Db design (haven't found a O/R mapper that can). It's still my favorite choice over all the others I looked at.

Christian Liensberger [Moderator]
Posted on the 12/27/06 7:45 PM
RE: A Question about inheritance
Reply Quote
 Thanks for the positive feedback. Opf3 can cover a lot of the scenarios, but not everything Smile It's held flexible to have everything possible (with some custom code).

All times are in GMT.