design for supporting entities with images
Posted
by
brainydexter
on Programmers
See other posts from Programmers
or by brainydexter
Published on 2012-03-15T07:02:23Z
Indexed on
2012/03/29
11:41 UTC
Read the original article
Hit count: 277
I have multiple entities like Hotels, Destination Cities etc which can contain images. The way I have my system setup right now is, I think of all the images belonging to this universal set (a table in the DB contains filePaths to all the images).
When I have to add an image to an entity, I see if the entity exists in this universal set of images. If it exists, attach the reference to this image, else create a new image. E.g.:
class ImageEntityHibernateDAO {
public void addImageToEntity(IContainImage entity, String filePath,
String title, String altText) {
ImageEntity image = this.getImage(filePath);
if (image == null)
image = new ImageEntity(filePath, title, altText);
getSession().beginTransaction();
entity.getImages().add(image);
getSession().getTransaction().commit();
}
}
My question is: Earlier I had to write this code for each entity (and each entity would have a Set collection). So, instead of re-writing the same code, I created the following interface:
public interface IContainImage {
Set<ImageEntity> getImages();
}
Entities which have image collections also implements IContainImage
interface. Now, for any entity that needs to support adding Image functionality, all I have to invoke from the DAO looks something like this:
// in DestinationDAO::addImageToDestination {
imageDao.addImageToEntity(destination, imageFileName, imageTitle, imageAltText);
// in HotelDAO::addImageToHotel {
imageDao.addImageToEntity(hotel, imageFileName, imageTitle, imageAltText);
It'd be great help if someone can provide me some critique on this design ? Are there any serious flaws that I'm not seeing right away ?
© Programmers or respective owner