Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Association Class

Status
Not open for further replies.

modika11

Programmer
Jan 31, 2008
20
Hi All,

I was wondering if you could help me out a little. I have been on here before and i must admit i do find this architecture stuff confusing at times, unfortunantly i work alone and dont have people to bounce ideas off. Where i get confused is where to put things more than anything else.

Anyway, i am developing an application which is a online branding tool. I have two classes, the first is brand and is simply a reperesentation of a brand and a product which is a representation of a product. I have an association class (as this is a many to many relationship) called brandProduct which basically has 4 properties (BrandID, ProductID, Brand (reference to a brand object), product(reference to a product object)) It also contains two methods (Insert Associtation, Delete Associtation). My confusion now comes in. What do i do when i want to get a list products that are associated with a brand, do i simply add a getProductsByBrandID method to my products class or do i create a getAssociatedProductsByBrandID in my BrandProduct Class and use the references i have to expose some additional properties (for instance create a read only proprerties productName and productImageUrl which get their data from the product reference in the class (which is lazy loaded))

I know this may be simply to you guys, but sometimes i just cant seem to get my head round these things and think so deeply into it that my head hurts :)

Hope some ideas will come my way.

Rob
 
I think you are confussing database wit OO design.

In OO design you Brand would have a colletion of products and/or you product would have a brand. Normally only one of two.

Christiaan Baes
Belgium

The future is out there
 
hi Chrissie,

So i guess i leave it as it is. The BrandProduct Class simply deals with the creation and deletion of the association between the two. In my Product Class i can create a brands collection (or vice versa my brand class can have a collection of product) which can be lazy loaded to only be retrieved when required. Any get methods such as getProductsByBrandID or GetBrandsByProductID can sit in the Brand and Product classes as thats where i guess they belong.

I think its the associations that confuse me, as they are easy to represent in Databases but not so in OO (or maybe thats just me)

Rob
 
Modika,

Chrissis is saying the exact opposite. There is no need for this association class thing......It's a mix of OO and RDB design.

Associations are very easy to represent. You pass one object as a parameter to another.

I think from what you said, you have two classes full stop, Brand and Product. Now, Brand and Product would both contain a collection for the other type. Hence you would have Brand having a collection of Products and Product containing a collection of Brands.

Your methods then look like as an example....

Brand.Add(product)
Brand.Remove(product)
Brand.Persist

When you do this, you would make sure the association is added or removed at both ends.

Your persist method would deal with the saving of the brand itself, any products contained within and the mapping.

C
 
I'm pretty sure a product can have only one brand but I could be misstaken.

And about those ID's, you don't need them in OO, in OO you would override the equals and hashcode methods so that the object becomes unique. Id's are used by DBAs. of course that is only a minor problem.

Th domain objects you describe (Brand and Product) shouldn't be aware were the data they use is comming from.

Things like getProductsByBrandID belong in the DAL (dataaccesslayer) not in the domainobjects.


Christiaan Baes
Belgium

The future is out there
 
Hi Guys,

Thanks for your input. I may have confused this in a way, it makes it sound like it is a bit of a shopping cart when a product has a brand, but its not, there is a brand selection process (for rebranding of buildings etc) so the products are base products that can be shared amongst brands and selected for projects so that is why there is a many to many relationship.

A brand may have 0 products and a product (although not often) may not be associated to any brand, so it is an adhoc selection process. So where do i put the method to associate the product to a brand? does that just sit in the product class then? What confuses me a little as it could sit in both Brand and Product, but where is it most appropriate.

Rob
 
Modika,

The answer is BOTH.

When you're on a product based screen, you'll likely want to link in one way, when you're on a brand based screen, you'll likely want to link in the other!

So you would have......

Brand.Add(product)
Brand.Remove(product)
Brand.Persist

AND

Product.Add(brand)
Product.Remove(brand)
Product.Persist

C
 
Thanks Craig,

I will take on board what has been said and adjust my classes accordingly.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top