3

我想使用 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);
    }
}

映射器

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?有人有想法吗?

4

2 回答 2

4

我实际看到的唯一方法是映射 aLazy<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;
    }
}
于 2014-10-30T06:15:45.790 回答
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;
    }
}

它不是一个字符串,但对于许多应用程序而言,它的行为就像一个字符串。

于 2014-12-28T09:40:14.200 回答