Reputation: 522
I'm developing an application with XPO/XAF and I need to manage photos together with some additional information.
This is the simplified (pseudo) code I use for my Photo entity:
public class Photo
{
public Photo()
{
CreationDate = DateTime.UtcNow;
}
private Image imageData;
public Image Data
{
get { return imageData; }
set
{
imageData = value;
// Current Impl: Use static code to create thumbnail
Thumbnail = ImageService.CreateThumbnail(value);
}
}
public Image Thumbnail { get; private set; }
public Guid Id { get; }
public DateTime CreationDate { get; private set; }
public string Description { get; set; }
}
The Photo entity is used in various other entities, e.g.
public class Inspection
{
public Photo Photo { get; set; }
}
public class User
{
public Photo Photo { get; set; }
}
Whenever the Data of the Photo is changed, the thumbnail should also be updated. My posted solution works, but it's quite ugly, isn't it?
And it is also not possible to implement a new requirement: It should be possible to specify (application-wide) the quality of the thumbnails based on the usage (e.g. higher quality for Inspection thumbnails, lower quality for User thumbnails).
In XAF I would implement this requirement with a ViewControllerViewController, reacting to changes of a Photo (taking the current 'owning view' into account). But this solution has some drawbacks:
1) Thumbnail property setter cannot be any longer private.
2) It's quite hard to (unit-)test, because additional code is required to setup the ViewController infrastructure.
3) A ViewController is only active if there is a view. But it's also possible to edit photos from a custom OData-Service. Of course I can/should move ImageProcessing code from the ViewController to an utility class/method, but I have to remember to call this code when using my OData Controllers.
4) When viewing/editing Photos from a generic listview, the ViewController does not know which quality setting to use (because right now, there is only a generic Photo class and no back-reference to the 'owner' exists). Of course it's possible to inherit various Photo classes (UserPhoto/InspectionPhoto/...), but does this make sense?
I think generating and persisting thumbnails is quite a common task, therefore I'm really interested your ideas. I also like concepts/ideas of DDD/rich-domain model, therefore I would like to know whether it's possible to adopt such concepts to my situation
Upvotes: 0
Views: 627
Reputation: 11326
You are asking some rather subjective questions about design and there is no 'correct' answer but here are some thoughts.
Is the Thumbnail
property necessary in the business object since it's only used during the view? Pulling the logic into a ViewController
makes sense.
It is quite easy to unit test XAF Controllers
and there are several examples in the DevExpress support centre and the documentation (S32594, How to test an action). Also the eXpand framework has some good example code for setting up XAF framework tests.
Another option would be to implement a custom editor for the thumbnail. You can even add it to the layout dynamically without a persistent property in the business object. There's a lot of options on this page about implementing custom ViewItems.
Yet another approach would be to modifying the layout at runtime to add a non-persistent thumbnail. There is an example in the documentation here which adds an non-persistent image control to a detail view.
You might also be interested in this support centre issue Q512788.
Upvotes: 1