Using custom ImageFile class in CMS v11.9.4

Vote:
 

Hey

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
    {
        [CultureSpecific]
        public virtual string AltText { get; set; }
    }

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

        [CultureSpecific]
        [Display(           
            GroupName = GroupNames.CampaignHeroGroupNames.Product,
            Order = 20)]
        [UIHint(UIHint.Image)]
        [AllowedTypes(typeof(ImageFile))]
        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
Vote:
 

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
Vote:
 

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
Vote:
 

in order to have reference to media you have to have property of type ContentReference - https://world.episerver.com/documentation/developer-guides/CMS/Content/Properties/built-in-property-types/

#198886
Nov 08, 2018 9:24
Vote:
 

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
Vote:
 

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
Vote:
 

Thank you

#198913
Nov 08, 2018 13:58
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.