Aggregation relation between tables

Aggregation relation between tables

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


Samurai posted on Friday, February 01, 2008

Hello folks! I'm in a middle of some project and I want to know how should I implement Aggregation relation between tables, in database, in CSLA.net. Well, I have 3 tabels. The first one represents Blogs (with primary key - ID_Blog), the second one represents Categories (with primary key - ID_Category) and the third one represents relation between previous mentioned tables (with primary key - ID_Blog, ID_Category). I hope somebody responds me by some example.

Regards!

JoeFallon1 replied on Friday, February 01, 2008

The third table is commonly called a Junction Table. It resolves the Many-Many relationship into two 1-Many relationships.

I tend to think of this in 2 directions. You may only need to use one of these directions. It depends on you use case(s).

Use Case #1: For a given blog get an editable list of Categories.

Blogs is a root BO and contains a child collection of BlogCategories. Each child BO in BlogCatgories is a Category object.  You should have a factory method on the Blog which takes a PK named ID_Blog and retrieves all the data about the Blog from the Blog table. This root BO then fetches its child collection of categories based on ID_Blog. The SQL statement does a join between the Junction table and the Category table and SELECTs data based on ID_Blog.

Use Case #2: For a given Category get an editable list of Blogs.
(Reverse the terms above.)

You may not need use case #2. But if you do, you will be building different objects for Blog. One is a root and the other is a child. You may decide to use a switchable object but that is a maintenance headache and you will be better off using different names for them.

Joe

Samurai replied on Monday, February 04, 2008

Joe, thank you for very helpfull answer! I need to ask you something else. If I use Use Case 1#, how should I generate Category object. I suppose it should be Child object, but without parent. Am I right?

Regards!

SomeGuy replied on Monday, February 04, 2008

You are going to have 2 Editable Root Objects, Blog and Category.

If a Blog can be in more than one Category you will have a Child Collection (BlogCategories) of Editable Child Objects (BlogCategory).

You will have similar objects for Category; CategoryBlogs, and CategoryBlog.

Look at the Project and Resource objects in the Project Tracker example app.

E

 

JoeFallon1 replied on Monday, February 04, 2008

Samurai:
If I use Use Case 1#, how should I generate Category object.

Regards!

===================================================================

Use Case #1:

BOs for an editable screen:

1. Root BO = Blog

2. Editable Child Collection = BlogCategories

3. Child BO = Category

===================================================================

BOs for a Read Only screen:

1. Root BO = Blog

2. Read Only Collection = BlogCategoriesROC

Nested in the ROC is a class named CategoryInfo.

===================================================================

Joe

 

Samurai replied on Tuesday, February 05, 2008

Ok, I've succesfully made this part of application, but I still have to modify code to  insert datas into Junction table. If I wrote:
           " Dim objBlog As Blog = Blog.NewBlog(333)
            objBlog.StartDate = "2008.01.01"
            objBlog.EndDate = "2008.12.31"
            Dim objC As Category = Category.NewCategory
            objC.Name = "Sport"
            objC.BlogCategory.Add(objC)
            objBlog.Save()"

...I would save datas only into table Blog and into table Category. How should I implement Insert into BlogCategories table (for example, I have field "Date" beside Id_Blog and Id_Category). Also, would you explain me how to use transaction for that insert? Thanks

Regards!

Samurai replied on Tuesday, February 12, 2008

Hello! Does anyone have an idea how to implement update/insert into Junction table? In this case it is BlogCategories table, which has Id_Blog and Id_Category.
Actually, my question is, who should delegate insert/update method for BlogCategories

Regards!

rsbaker0 replied on Tuesday, February 12, 2008

I would think the "junctions" would be a "child" collection (BusinessListBase) of the object being edited.  When the object is saved, it saves the child list of junctions.

 

 

Copyright (c) Marimer LLC