Doubt about Aggregation, Composition???

Doubt about Aggregation, Composition???

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


kdlc posted on Thursday, March 01, 2007

Hello Everyone!

 

I've this scenario (Pretty Common!):

 Customer Object Root Object

 Invoice Object Root Object

 

The doubt that I have its related with the Invoice Having a (HAS A) Customer Object,  how can I implement the aggregation??

Should I use a Customer Root Object inside the Invoice Object and Load it, Save It, Etc in the Invoice DATAPORTAL Methods? Or should I interact with the database directly ???

 

What do you guys recommend??

 

Something like this:

Private Overloads Sub DataPortal_Fetch(ByVal criteria As Criteria)

Using cn As New SqlConnection(Database.RedStarDBConnection)

cn.Open()

Using cm As SqlCommand = cn.CreateCommand

cm.CommandType = CommandType.StoredProcedure

cm.CommandText = "sp_SelectInvoice"

cm.Parameters.AddWithValue("@InvoiceID", criteria.Id)

 

Using dr As New SafeDataReader(cm.ExecuteReader)

 

dr.Read()

 

With dr

mInvoiceID = .GetInt32("InvoiceID")

mCustomerID = .GetInt32("CustomerID")

xCustomer = Customer.GetCustomer(mCustomerID)

‘Load Customer Information

With xCustomer

mCustomerID = .CustomerID

mFullName = .FullName

mFullAddress = .FullAddress

mHomePhone = .HomePhone

mWorkPhone = .WorkPhone

mCellPhone = .CellPhone

End With

‘load Invoice Information

mInvoiceDate = .GetSmartDate("InvoiceDate", mInvoiceDate.EmptyIsMin)

 

...more code …

 

 

THANKS in ADVANCE!

jeff replied on Thursday, March 01, 2007

I prefer doing a join and loading the customer related fields from the same datareader. Seems cleaner to me to keep objects like this seperate if all you need are some read only fields from another object. I've seen this called "borrowed fields" on this forum in the past. But if you have a use case that calls for editing both root objects at once, you could always create a non-csla class just for this purpose. It could have references to the required root objects and could be responsible for loading/updating them together.

hurcane replied on Thursday, March 01, 2007

If you are using this same customer object in other use cases, I would consider keeping the database code separate in each object. It's not as efficient, but it's often good enough.

Copyright (c) Marimer LLC