0

I was wondering if there is any kind of lazy loading for a single attribute to increase the loading speed. I'm working with entity framwork 6.1.3 and the following model:

public class Photo : HoHoRecord
{
    public string Description { get; set; }

    public int? GalleryId { get; set; }
    public virtual Gallery Gallery { get; set; }

    public byte[] Thumbnail { get; set; }
    public byte[] Content { get; set; }

    public virtual BitmapImage ThumbnailBitmapImage
    {
        get
        {
            return ImageByteConverter.GetBitmapFromContentByteArray(Thumbnail);
        }
    }

    public virtual BitmapImage ContentBitmapImage
    {
        get
        {
            return ImageByteConverter.GetBitmapFromContentByteArray(Content);
        }
    }
}

If I access the list of Photos in another model, I would prefer, that the attribute Photo.Content is not download yet. It should be downloaded only, when I access the property Photo.ContentBitmapImage.

This way I would be able increase the main loading speed of the whole liste in a good way.

Is there any way to achive this without creating an additional model class to store the byte array?

Thansk and regards

Markus

2
  • how big are you images Commented Feb 17, 2018 at 5:28
  • @MichaelRandall The image content is around 3 to 4 MB in my tests Commented Feb 17, 2018 at 16:19

1 Answer 1

1

The answer is no.

However there is a couple of things you could consider.

  • Projecting to an intermediate class so it doesn't drag the images over all the time. This will save load times. However you lose the ability to use it as an attached entity and so forth.

  • Vertically partitioning the table so your images are not part of the entity it self and in another table/entity. Or creating a dedicated images table, both these will give you true lazy loadable images.

  • The other thing to consider here is, that there are times when images are best file backed and not part of a Database. There are pros and cons and reams of threads written about this topic, and it comes down to your needs and how big the images are

In regards to the first point, you could easily make an extensions/methods to fetch the image when needed

Sign up to request clarification or add additional context in comments.

2 Comments

And if you use method to load the image, it can return Stream instead of byte[], avoiding an unnecessary copy of the photo bits in memory.
First of all thanks for the clear and easy answer "No", and for the hints for other solutions. I choosen the way "vertically partitioning" - this was my first idea when I thought about the situation, too. But I wanted to make sure there is no data anotation trick or so, before changing the models. Thanks for your answer. By the way, I know there are many pros and cons about storing the images (and other files) directly in the database - but in my current project I need to use the database way. But thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.