0

Say I have the following class, which maps to a database table:

public class Person 
{
public Guid Id {get; set; }
public string Name {get; set; }
public DateTime DateOfBirth {get; set; }
public string ImageURL {get; set; }
public string ImageBytes {get; set; }

//Methods go here

}

Notice there are two fields. The image URL is stored in the database and the ImageBytes is not. The ImageURL tells the application how to access the image (it is a web api url).

Were do I put the code that gets the Image URL and then gets the image? I believe I have two options:

1) Application Service

2) Query handler that gets the image from the database

3) Access the web service in the razor view

The query handler sounds the cleaner solution, however should a query handler access a web api?

Note that I understand a query handler to mean a class that reads (rather than writes) to the database.

  • 1
    Why do you want the binary (image bytes) in memory? – Laiv Apr 17 at 17:51
  • @Laiv, I only want to display the image on the webpage. – w0051977 Apr 17 at 17:54
  • 3
    Leave then the retrieve to the view. It only needs the URL – Laiv Apr 17 at 17:55
  • I'm inclined to agree with @Laiv on this one. Unless your application requires some special handling of the image, just let the view retrieve it directly from the URL. – Robert Harvey Apr 17 at 18:15
  • Option 2 is not an option. Even by your own definition its job is to just read the database. Even without that definition, it implies that the image data will always be available. So what happens when the database is available but the image service is offline? That decision is at the wrong level of analysis, maybe you do not care about the image, just its name and date of creation? It should just return the data sans image. What if you need to permutate the image (change its format)? well not returning the data at all means the image doesn't exist, but it cannot return all of the data... – Kain0_0 Apr 18 at 0:41
0

Normally, you can do this by separating your Entity Model and Domain Model. Entity Model doesn't have ImageBytes property but Domain Model has. When you are mapping Entity Model to Domain Model, you can load bytes in Application Service. But, really big but, if you load images from there and if you have n tier (I assume here is 2: UI and App) transferring those bytes from App to UI is really big performance issue. Thus, as @Laiv mention his comment, UI should be responsible to download images. Besides, do not force UI to load this images, maybe it only shares URL.

0

DDD is the right design pattern for adapting domain to new requiments. In your case it is very common requirement and not related DDD.

You should return imageUrl from servicelayer within your entity and if you are using a model then together with the model. And you will bind this model to your ui on the front-end layer. Your front-end component will download the image from the url.

You don't need to imageByte property in your entity for this

  • This only applies to cases where the end user (who you return the image URL to) has access rights to the external resource. If they do, then this is indeed the right approach. But there are cases where the end user doesn't have access and the web service does (by intentional design). In this case, it is the web service's duty to download the image data. – Flater Oct 16 at 9:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.