Glass Mapper V4语言项目后备,如何使其与Autofac一起使用?

时间:2015-05-21 13:58:52

标签: sitecore autofac glass-mapper

我正在使用带有Autofac的Glass Mapper v4,并且无法弄清楚如何使它与Language Item Fallback模块一起使用。有一些创建实现IObjectConstructionTask的类的示例(见下文)

public class FallbackCheckTask : IObjectConstructionTask
{
    public void Execute(ObjectConstructionArgs args)
    {
        if (args.Result == null)
        {
            var scContext = args.AbstractTypeCreationContext as SitecoreTypeCreationContext;

            // if the item itself is null, regardless of version, abort
            if (scContext.Item == null)
            {
                args.AbortPipeline();
                return;
            }

            // we could be trying to convert rendering parameters to a glass model, and if so, just return.
            if (String.Compare(scContext.Item.Paths.FullPath, "[orphan]/renderingParameters", true) == 0)
            {
                return;
            }

            // the default glassmapper code would simply abort pipeline if the context items version count for the current langauge was 0
            // but this does not take item fallback into account
            // added here a check on the fallback extension method GetFallbackItem, recursively (for chained fallback)
            // and then if that fallback item is null or it's version count is 0 (and only then) would you go ahead and abort the pipeline
            if (scContext.Item.Versions.Count == 0)
            {
                var fallBackItem = CheckRecursivelyForFallbackItem(scContext.Item);
                if (fallBackItem == null)
                    args.AbortPipeline();
                else if (fallBackItem.Versions.Count == 0)
                    args.AbortPipeline();
                return;
            }
        }
    }

    // in the case of chained fallback, eg fr-CA -> en-CA -> en
    // could be that the middle languages don't have versions either, but DO have a fallback item
    // therefore, must check back further until either a version is found, or there are no more fallback items
    private Item CheckRecursivelyForFallbackItem(Item thisItem)
    {
        var fallBackItem = thisItem.GetFallbackItem();
        if (fallBackItem != null)
        {
            if (fallBackItem.Versions.Count == 0)
                fallBackItem = CheckRecursivelyForFallbackItem(fallBackItem);
        }
        return fallBackItem;
    }
}

然后你注册(与温莎城堡)

public static void CastleConfig(IWindsorContainer container){
            var config = new Config();

            container.Register(
               Component.For<IObjectConstructionTask>().ImplementedBy<FallbackCheckTask>().LifestylePerWebRequest()
              );
          //  config.EnableCaching = false;

            container.Install(new SitecoreInstaller(config));
        }

我正在使用Autofac,并且不知道如何执行与上述相同的操作,并确保以正确的顺序执行。我正在以典型的方式注册我的类型(见下文)但它似乎没有挂钩我的FallbackCheckTask类。

 public static void Register()
    {
        var builder = new ContainerBuilder();
        builder.RegisterControllers(typeof(MvcApplication).Assembly);

        // register our types
        builder.RegisterType<FallbackCheckTask>().As<IObjectConstructionTask>().InstancePerLifetimeScope();

        // build and set the resolver
        var container = builder.Build();
        DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
    }

如果玻璃不参与获取项目值,我也会将语言项目后备连接起来并按预期工作。我理解为什么Glass没有开箱即用的数据映射,我似乎无法让修复工作。有什么想法吗?

编辑2015-05-21 19:00

我编辑GlassMapperScCustom.cs如下:

public static IDependencyResolver CreateResolver(){
        var config = new Glass.Mapper.Sc.Config();
        var resolver = new DependencyResolver(config);

        resolver.ObjectConstructionFactory.Add(() => new FallbackCheckTask());

        return resolver;
    }

现在它仅在有项目版本的情况下调用Execute的{​​{1}}方法,如果没有版本则不调用该方法。此外,无论我做什么,如果我启用此任务,我的测试查询项总是返回为NULL:

FallbackCheckTask

总而言之,我现在好一点,然后我就是以前但是当任务类被注册时,无论是否有版本,所有项目的查询都会返回null。如前所述,任何帮助都表示赞赏。

1 个答案:

答案 0 :(得分:2)

我知道您提到过您使用的是VersionCountDisabler,但它看起来像这样:

using(new VersionCountDisabler()){

        var test = SitecoreContext.QuerySingle<Item>("{7A6D933A-127B-4C08-B073-7C39F16EBD06}");
        var test1 = SitecoreContext.Query<Item>("{7A6D933A-127B-4C08-B073-7C39F16EBD06}").ToList();
        var test2 = SitecoreContext.GetCurrentItem<Item>();
        var test3 = SitecoreContext.GetItem<Item>("{7A6D933A-127B-4C08-B073-7C39F16EBD06}");

}

或者你是否以其他方式禁用它?

我还注意到您的备用代码似乎没有更新scContent.Item属性。我认为您需要将其更新为以下内容:

public class FallbackCheckTask : IObjectConstructionTask
{
public void Execute(ObjectConstructionArgs args)
{
    if (args.Result == null)
    {
        var scContext = args.AbstractTypeCreationContext as SitecoreTypeCreationContext;

        // if the item itself is null, regardless of version, abort
        if (scContext.Item == null)
        {
            args.AbortPipeline();
            return;
        }

        // we could be trying to convert rendering parameters to a glass model, and if so, just return.
        if (String.Compare(scContext.Item.Paths.FullPath, "[orphan]/renderingParameters", true) == 0)
        {
            return;
        }

        // the default glassmapper code would simply abort pipeline if the context items version count for the current langauge was 0
        // but this does not take item fallback into account
        // added here a check on the fallback extension method GetFallbackItem, recursively (for chained fallback)
        // and then if that fallback item is null or it's version count is 0 (and only then) would you go ahead and abort the pipeline
        if (scContext.Item.Versions.Count == 0)
        {
            var fallBackItem = CheckRecursivelyForFallbackItem(scContext.Item);
            if (fallBackItem == null)
                args.AbortPipeline();
            else if (fallBackItem.Versions.Count == 0)
                args.AbortPipeline();

            //don't just return but update the scContext.Item to the fallback item
            scContext.Item = fallbackItem;
        }
    }
}

// in the case of chained fallback, eg fr-CA -> en-CA -> en
// could be that the middle languages don't have versions either, but DO have a fallback item
// therefore, must check back further until either a version is found, or there are no more fallback items
private Item CheckRecursivelyForFallbackItem(Item thisItem)
{
    var fallBackItem = thisItem.GetFallbackItem();
    if (fallBackItem != null)
    {
        if (fallBackItem.Versions.Count == 0)
            fallBackItem = CheckRecursivelyForFallbackItem(fallBackItem);
    }
    return fallBackItem;
}
}
相关问题