Using custom ImageFile class in CMS v11.9.4



We've implemented our own ImageFile class below:

    [ContentType(GUID = "868493e9-062f-49fd-98e0-6580dbf17ce7")]
    [MediaDescriptor(ExtensionString = "jpg,jpeg,png,gif")]
    public class ImageFile : ImageData
        public virtual string AltText { get; set; }

When I want some content to have an image property I do the following:

            GroupName = GroupNames.CampaignHeroGroupNames.Product,
            Order = 20)]
        public virtual ContentReference ProductImage { get; set; }

On a simple BlockData type, we don't have controller cause it is simple rendering, so it just goes straight to the view.

However now my ProductImage is a ContentReference in the view and of course not an ImageFile with the AltText property. As I see it I have two options:

  1. In the view, get the IContentLoader service and _contentLoader.Get(Model.ProductImage).
  2. Implement a controller and viewmodel for every simple block type which sole purpose is _contentLoader.Get(Model.ProductImage).

My question is; is there no other/better way to always access my ContentReferences for images as ImageFiles?

Thank you in advance!

#198746 Nov 05, 2018 17:31
  • valdis iljuconoks
    Member since: 2011

    yes that's one of the option. I would hide it behind some extension method and not access content loader via service locator anti-pattern. and btw, I would create an extension on IContentLoader interface, instead of ContentReference just to avoid service locator. and would override webpage base type (aside from aspnet MVC default supplied) to inject automatically required interfaces "directly" into a view.

    But... that's the whole new story around dependency injection topic.

    #198836 Nov 07, 2018 14:13

    What about a custom ImageFile property? I tried to create one, but I wasn't able to have it resemble af ContentRefence, it always ended up as a multiline text field.

    #198838 Nov 07, 2018 14:28
  • valdis iljuconoks
    Member since: 2011

    in order to have reference to media you have to have property of type ContentReference -

    #198886 Nov 08, 2018 9:24

    Yes, but I was hoping I could make a custom property that took an asset (my custom ImageFile content), but I guess I can't.

    #198907 Nov 08, 2018 11:58
  • valdis iljuconoks
    Member since: 2011

    what about this? I washed my hands after wrote this :)

    public ImageFile Image => ProductImage == ContentReference.EmptyReference ? null : ServiceLocator.Current.GetInstance<IContentRepository>().Get<ImageFile>(ProductImage);
    #198909 Nov 08, 2018 12:43

    Thank you

    #198913 Nov 08, 2018 13:58