Encapsulation Broken on Collection Default...Need Some Advice...

Encapsulation Broken on Collection Default...Need Some Advice...

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

NightOwl888 posted on Wednesday, January 09, 2008

I am working on designing a product page that contains multiple images of the same product.  The behavior I am looking for is similar to this page:


There is one requirement over and above this page though - I want to be able to select the default image that is shown when the page loads and persist this in the product database.

The database table design for this was really straightforward - there is a Product table and a ProductImage table, one Product to many ProductImages.  The DefaultProductImageID (the primary key of the ProductImage table) is a value stored in the Product table.

Now, the OOD part is where I am strugling with this concept. I have a Product, ProductImageCollection (editable child collection) and a ProductImage business object. At first it seemed that the ProductImageID was a property of the Product object. However, there is one piece of business logic that cannot be implemented inside CSLA if this is the case - and that is when the item that is selected is deleted from the collection. In this case, I would like to clear the DefaultProductImageID (assign it Guid.Empty).

I thought about the possibility of making the DefaultProductImageID a property of the ProductImageCollection so the Delete method could handle this behavior, but I ran into another snag - there is a business rule tied to this property because it is a required property of the Product. So I will need to communicate with the parent object to tell it whether or not the value exists to enforce this rule.  I would also have to have my Product object pass the default value to the child ProductImageCollection object when it is loaded from the database. At first I thought this breaks encapsulation, but that is what we are doing anyway - passing all of the data to the child and having it set its own values. Anyway, it seems like this should work, but I need to somehow tell my Parent object (perhaps with an event) when the value of my DefaultProductImageID field changes. If someone could point me to a code sample where an event from a collection can be raised to its parent in CSLA I would appreciate it.

So, it looks like my choices are:

1. Put the property on the Product object and have my UI logic clear this value if the item is deleted from the collection. While this works, it clearly has ramifications if the rule changes or the UI is duplicated. This clearly defeats the purpose of using CSLA in the first place to handle my business logic.

2. Put the property on the ProductImageCollection and have it raise an event to handle the required rule for the DefaultProductImageID.  I have never tried raising events in CSLA, but I know there are issues raising events so I may need some assistance with this.  Code samples how to approach this would be appreciated. I should point out here that I am using version 1.51 of CSLA with VB.NET 2.0. I am also using ActiveObjects (version, but since there is no documenatation for it anymore I am rowing without a paddle.

3. Some other solution that somebody here knows. I am open to suggestions.

Thanks in advance for your assistance.


JoeFallon1 replied on Wednesday, January 09, 2008

Put the property on the Product object and have the Product object update itself and the collection. Then it can handle all the issues for this UI and any other UI.



NightOwl888 replied on Wednesday, January 09, 2008

Ok, sounds reasonable. However the issue at hand here is how does my collection tell the Product object that the ProductImage that has been set as the default has been deleted from the collection so the Product can clear the property and mark the Product object broken because DefaultProductImageID is a required field? Keep in mind this will somehow have to happen within the "Remove" method of the collection.

Copyright (c) Marimer LLC