【发布时间】:2015-05-21 13:58:52
【问题描述】:
我正在使用带有 Autofac 的 Glass Mapper v4,但无法弄清楚如何使其与 Language Item Fall back 模块一起使用。有创建实现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 不参与获取项目值,我还连接了 Language Item Fallback 并按预期工作。我理解为什么 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;
}
现在它调用FallbackCheckTask 的Execute 方法,仅当有该项目的版本时,如果没有版本则不调用该方法。此外,如果我启用此任务,无论我做什么,我的测试查询项总是返回为 NULL:
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}");
总而言之,我现在比以前好一点,但是当任务类注册时,所有项目的查询都返回为空,无论它们是否有版本。如前所述,感谢您提供任何帮助。
【问题讨论】:
-
远射-您是否禁用了版本计数?
-
是的,我已禁用版本计数。谢谢。
-
您应该将“编辑”作为答案。 It's perfectly acceptable to answer your own question
标签: sitecore autofac glass-mapper