如何使用自定义属性延迟加载属性

时间:2014-10-24 08:57:07

标签: c# sitecore glass-mapper

我想使用Glass Mapper创建一个自定义属性来获取Sitecore URL,因为无法使用SitecoreInfo(SitecoreInfoType.Url)延迟加载属性,并且我们在加载映射项的URL时遇到一些性能问题,其中URL永远不会被使用。

这是我到目前为止所得到的:

配置

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

    public bool IsLazy { get; set; }
}

属性

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);
    }
}

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?
    }
}

到目前为止,这么好。但是,我如何在映射器中延迟加载URL?有没有人有想法?

2 个答案:

答案 0 :(得分:4)

我实际看到的唯一方法是映射Lazy<T>并向该类添加一个新属性,该类在访问它时返回this的值。所以在你的映射器中,放置// now what?的地方我将返回惰性字符串:

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

然后在你的模型中,放上这两个属性:

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

[SitecoreIgnore]
public virtual string Url
{
    get
    {
        return this.LazyUrl.Value;
    }
}

答案 1 :(得分:0)

您可以通过一些创造力和新的代表功能

来实现与此类似的功能

在流畅的配置图中,类似的类型如下:

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;
    }
}

它不是一个字符串,但出于许多应用程序的目的,它将表现得像一个。