views:

229

answers:

4

I just got out of a small project and I've tried to follow a somewhat domain-driven design (well, what I think it is...). I've struggled a bit were some of my domain class started to need to load something. I'm not sure this is the right place for it.

Let's take for example a boring image slideshow. For my domain classes, I would define let's say an Image class and maybe a Slideshow class which contain a bunch of Image instances. Now, somewhere in my application, someone needs to load the actual image from the file system to display it.

So the questions are :

  1. Who is responsible for loading the actual image? The Image class, The Slideshow class or something outside the domain classes? Should my domain objects be organized differently?
  2. Where do I store this image once it's loaded? How do I pass it to the view?

Note: In a non-blocking, not multithreaded environment like Flash, loading stuff is always asynchronous.

Thanks

A: 

Here is an MVCS ImageGallery example I made using Robotlegs. I load the images in the component. The data, the xml or whatever data you use, is loaded and stored on the model. You certainly could load the image bytes and store them on the model. It would really depend on how you were planning on using them. For an image manipulation application, I would probably store the original bytes on the model and pass a copy for manipulation to the view.

Joel Hooks
A: 

Who is responsible for loading the actual image?

The controller.

Where do I store this image once it's loaded?

In the model.

How do I pass it to the view?

In Web applications, generally models are passed to the view via the HTTP request but it depends on what abstraction you're using. Flash I know little about but in Java, for example, there is an HttpServletRequest that has attributes on it. This is where the model goes. In PHP, the "global" state is really a request so that's where it goes.

cletus
Should the url and image data be in the same model class?
Subb
URL and image data can both be viewed as data so the model I guess.
cletus
+2  A: 

I know what you mean about getting confused with Domain Driven Design.

I am in the same boat as you, in the learning process of DDD. So take it easy on me if I am giving misleading information.

Having said that here is how I would approach this:

-Create a Service class called something like Projector.

-Create another Repository class called SlidehowsRepository. This is responsible for communicating with the database to load information. So it could contain your function that loads the files (or pointers to the files), also it could contain functions to load other sideshow information from the database. But its sole purpose is to load sideshow information from the database.

-Your Projector service class would then use the SlideshowsRepository to load some images into a list of Image classes. (I assume that is your intended use of the image class). Not sure what the Sideshow class was intended to be, but whatever it is it would probably be used here inside the Projector class. I see Projectors class as a coordinator of the work that needs to be done in order to flip through slides and give the appropriate information to your application (Or controller in the case of MVC). But it would not communicate directly with the database. It would use the SlideshowsRepository to get information out of the database.

So your controller in MVC would only have to instantiate the Projector and call very high level functions on it. Such as start, stop, next, loadSlideshowByName... etc...whatever else the projector can do. Once the Projector gives back the information you need, maybe a list of image classes filled with image information you intend to display, you just pass it on to your View from the controller in MVC. And as you probably know the View is supposed to be "dumb" so it doesen't do anything except know how to display the information you give it. But the business logic of the slide show could be warped into the Projector service class.

I think with this design you can use Dependency Injection as well. So your MVC controller depends on Projector and Projector depends on SlideshowsRepository, Slideshow and Image class. Or something similar depending on how you decide to set up your dependencies.

Does that makes sense? Or am I completely off topic for what you were looking for?

Roberto Sebestyen
+1  A: 

I'm not entirely sure what DDD is all about so keep that in mind while reading.

In the case of the slideshow, it almost seems like a matter of personal choice. Since the images are more or less immutable, you could consider that the URL for the image IS the model and the bitmap data that you're loading is part of the view. So in that case, your Image class might contain a URL that your ImageView would load.

If you felt like the bitmap data was really part of the model and you needed to be able to refresh the view when the bitmap data changes, you could load the bitmapdata in the Image class and pass it to the view when it's done loading.

I generally tend to use the prior example when coding something like this. The ImageView would have a Loader object which begins loading when the model is set. A loading message would be displayed during the load process and event listeners would determine when the Loader should be added to the stage.

Hope this helps!

Mims H. Wright