Search code examples
asp.net-mvclinq-to-sqldomain-driven-designlazy-loadingiqueryable

How should I lazy-generate a thumbnail image?


For background, I have a Data layer and Service layer loosely based on Rob Conery's Storefront model and like Rob's, many of my domain objects and chained with LazyList<>'s and LazyItem<>'s to make use of the deferred execution that Linq2Sql provides given that my Lazy* types utilise IQueryable<T> rather than this awesome delegate approach.

So I have an object graph like this (basically, each Activity should have a photo gallery of many images- thumbnails and full size photos):

latest3Activities[0].Gallery.Images.Inner[1].FullImage

The Gallery type has an Images property of LazyList<PhotoGalleryImage> and so the IList<PhotoGalleryImage> from that LazyList is the Inner you see. Each PhotoGalleryImage item has a FullImage property and a Thumbnail property, both of type Image.

The idea is that the full rez uploaded photo is stored in the PhotoGalleryImage.FullImage property and initially, the Thumbnail property is Null.

What I'm after is this: when the Thumbnail property is accessed for the 1st time, if it is Null I want my Service layer to generate the Thumb, persist it to the DB, then return the Image instance which is the smaller photo. I have all the code to create the thumbnail from the full size image, so that's not the question here.

What I can't figure out is how to catch the first access of the Thumbnail property (in my IQueryable<> architecture context) and then have the Service layer do the resizing and not the Repositories (DAL). I strongly feel that the Service (business) layer should be responsible for this functionality decision but I don't see how to make it work.

Currently I'm thinking the mapping from my domain classes in the repositories to the Linq2Sql classes would be a good place to identify this 'first access' I refer to, but I don't then see how a lower tier can then call up into the Service layer and perform the shrink (or even if it can, that it should).

Maybe my design constrains me to have the Repos do the conversion. Maybe I shouldn't want the Service layer to perform this logic at all. Maybe my design is just so horrid that I really shouldn't be facing this mess at all. What approach can I use?


Solution

  • The point of lazy init is to defer allocating resources since you don't know when or if you need them.

    In your case, this doesn't really make sense: As soon as the image is uploaded, the server will need the thumbnail -- the user wants to see the new image immediately, right? Therefore deferring the creation of the thumbnail has no positive ROI.