3

I want to create a custom attribute using Glass Mapper for getting the Sitecore URL, because it is not possible to lazy load a property with SitecoreInfo(SitecoreInfoType.Url) and we have some performance issues loading URL of mapped items, where the URL will never be used.

Here is what I've got so far:

The Configuration

public class SitecoreUrlConfiguration : AbstractPropertyConfiguration
{
    public SitecoreInfoUrlOptions UrlOptions { get; set; }

    public bool IsLazy { get; set; }
}

The Attribute

public class SitecoreUrlAttribute : AbstractPropertyAttribute
{
    public SitecoreUrlAttribute()
    {
        this.IsLazy = true;
        this.UrlOptions = SitecoreInfoUrlOptions.Default;
    }

    /// <summary>
    /// Gets or sets a value indicating whether is lazy.
    /// </summary>
    public bool IsLazy { get; set; }

    public SitecoreInfoUrlOptions UrlOptions { get; set; }

    public override AbstractPropertyConfiguration Configure(PropertyInfo propertyInfo)
    {
        var config = new SitecoreUrlConfiguration();
        this.Configure(propertyInfo, config);
        return config;
    }

    public void Configure(PropertyInfo propertyInfo, SitecoreUrlConfiguration config)
    {
        config.UrlOptions = this.UrlOptions;
        config.IsLazy = this.IsLazy;

        base.Configure(propertyInfo, config);
    }
}

The Mapper

public class SitecoreUrlMapper : AbstractDataMapper
{
    public override object MapToProperty(AbstractDataMappingContext mappingContext)
    {
        var context = mappingContext as SitecoreDataMappingContext;
        if (context == null)
        {
            throw new MapperException("Mapping Context is null");
        }

        var item = context.Item;
        var scConfig = this.Configuration as SitecoreUrlConfiguration;

        if (scConfig == null)
        {
            throw new MapperException("SitecoreUrlConfiguration is null");
        }

        var urlOptions = Utilities.CreateUrlOptions(scConfig.UrlOptions);

        urlOptions.Language = null;
        // now, what?
    }
}

So far, so good. But how can I lazy load the URL in the mapper? Does anyone have an idea?

Yan Sklyarenko
  • 31,557
  • 24
  • 104
  • 139
Tineler
  • 253
  • 3
  • 17

2 Answers2

4

The only way I actually see is to map a Lazy<T> and add a new property to the class which returns the value of this when accessing it. So in you mapper, where you put // now what? I would return the lazy string:

return new Lazy<string>(() => LinkManager.GetItemUrl(item, urlOptions));

Then in your model, put these two properties:

[SitecoreUrl]
public Lazy<string> LazyUrl { private get; set; }

[SitecoreIgnore]
public virtual string Url
{
    get
    {
        return this.LazyUrl.Value;
    }
}
Kevin Brechbühl
  • 4,717
  • 3
  • 24
  • 47
0

You can achieve pretty similar to this with a bit of creativity and the new Delegate functionality

In the fluent configuration map the type like so:

SitecoreType<IWhatever> sitecoreType = new SitecoreType<IWhatever>();
sitecoreType.Delegate(y => y.Url).GetValue(GetLazyUrl);

private LazyString GetLazyUrl(SitecoreDataMappingContext arg)
{
    var item = context.Item;

    return new LazyString(
        () => 
        {
           // the necessary actions to get the url
        });
}

public class LazyString : Lazy<string>
{
    public LazyString(Func<string> valueFactory) : base(valueFactory)
    {
    }

    public override string ToString()
    {
        return Value;
    }

    public static implicit operator string(LazyString lazyString)
    {
        return lazyString.Value;
    }
}

It's not a string, but for the purposes of many applications, will behave like one.

Nat
  • 19
  • 3