hallipr
New member
- Joined
- Nov 10, 2008
- Messages
- 1
- Programming Experience
- 10+
In the past, I have relied on Datasets, LINQ, or other various designers to define my classes and haven't ventured too far into custom classes and collections from scratch, other than simple classes. I would like to at least understand the basics of the standard Parent.Children collection and managing the Child.Parent references associated with the relationship.
Lets start with a basic Author->Books example:
We can create some books and authors, and cross reference them manually:
This is about as far as I have needed to go up until now. I pulled a bit of code out of a LINQtoSQL class and added it to the BookInfo class:
This allows the book to reference it's author control the authors reference to the book by adding and removing itself from the authors Books collection.
What happens if a book is removed from an AuthorInfo.Books list directly?
We would end up with a Book that still has an AuthorInfo in the _Author field, but the book no longer belongs to the Author's Books collection.
Is it standard to write code on both sides of the relationship to control the references, or should this all be done as code on the parent side responding to events from either the Children collection (AuthorInfo.Books), or the Child (BookInfo) passed up through the collection?
Should a custom BooksList collection be written that passes events like BookRemoved(BookInfo) and AuthorChanged(BookInfo, AuthorInfo)?
It seems like a generic collection should work while still maintaining tight control over the refrences.
Lets start with a basic Author->Books example:
VB.NET:
Public Class BookInfo
Public Title As String
Public Author As AuthorInfo
End Class
Public Class AuthorInfo
Public Name As String
Public Books As New List(of BookInfo)
End Class
We can create some books and authors, and cross reference them manually:
VB.NET:
'Create Authors
Dim Author1 As New AuthorInfo
Author1.Name = "J. Frost"
Dim Author2 As New AuthorInfo
Author2.Name = "S. Sunny"
'Create Books
Dim Book1 As New BookInfo
Book1.Title = "Winter Story"
Dim Book2 As New BookInfo
Book2.Title = "Summer Things"
Dim Book3 As New BookInfo
Book3.Title = "Beach Days"
'Cross Reference Authors -> Books
Book1.Author = Author1
Book2.Author = Author2
Book3.Author = Author2
Author1.Books.Add(Book1)
Author2.Books.Add(Book2)
Author2.Books.Add(Book3)
This is about as far as I have needed to go up until now. I pulled a bit of code out of a LINQtoSQL class and added it to the BookInfo class:
VB.NET:
Public Class BookInfo
Public Title As String
Private _Author As AuthorInfo
Public Property Author() As AuthorInfo
Get
Return _Author
End Get
Set(ByVal value As AuthorInfo)
Dim previousValue As AuthorInfo = Me._Author
If previousValue IsNot value Then
'Only proceed if we are indeed changing the books author
If previousValue IsNot Nothing Then
'If _author was already set, set _author to nothing and remove this book from the author's Books List
Me._Author = Nothing
previousValue.Books.Remove(Me)
End If
Me._Author = value
If value IsNot Nothing Then
'If we're not setting the author to nothing, add the current book to the authors Books list.
value.Books.Add(Me)
End If
End If
End Set
End Property
End Class
This allows the book to reference it's author control the authors reference to the book by adding and removing itself from the authors Books collection.
What happens if a book is removed from an AuthorInfo.Books list directly?
We would end up with a Book that still has an AuthorInfo in the _Author field, but the book no longer belongs to the Author's Books collection.
Is it standard to write code on both sides of the relationship to control the references, or should this all be done as code on the parent side responding to events from either the Children collection (AuthorInfo.Books), or the Child (BookInfo) passed up through the collection?
Should a custom BooksList collection be written that passes events like BookRemoved(BookInfo) and AuthorChanged(BookInfo, AuthorInfo)?
It seems like a generic collection should work while still maintaining tight control over the refrences.