【问题标题】:Sitecore Glass data model inheritenceSitecore Glass 数据模型继承
【发布时间】:2014-09-06 18:42:09
【问题描述】:

我在具有基本数据模板结构的 Sitecore 实例上使用 Glass Mapper

  • 基地
    • BaseWithList
    • BaseWithExtraContent
      • BaseWithExtraContentAndCallToActionLink

我也在我的项目中添加了模型类来遵循这种结构。我的类名与我的模板名匹配。

[SitecoreType(TemplateId = "{5D19BD92-799E-4DC1-9A4E-1DDE3AD68DAD}", AutoMap = true)]
public class Base
{
   public virtual string Title {get;set;}
   public virtual string Content {get;set;}
}

[SitecoreType(TemplateId = "{0491E3D6-EBAA-4E21-B255-80F0607B176D}", AutoMap = true)]
public class BaseWithExtraContent : Base
{
    public virtual string ExtraContent {get;set;}
}

[SitecoreType(TemplateId = "{95563412-7A08-46A3-98CB-ABC4796D57D4}", AutoMap = true)]
public class BaseWithExtraContentAndCallToActionLink : BaseWithExtraContent
{
    public virtual string CallToActionLink {get;set;}
}

这些数据模型是从另一个具有基本类型列表的类中使用的,我希望能够在此处存储任何派生类型,因此我添加了tutorial中详细介绍的属性

[SitecoreType(AutoMap = true)]
public class HomePage
{
    [SitecoreChildren(InferType = true)]
    [SitecoreField(FieldName = "Widgets")]
    public virtual IEnumerable<Base> Widgets { get; set; }
}

根据tutorial,这应该可以工作。然而,小部件列表只包含基本类型的类。

然后我发现后来的tutorial 说,如果您将模型分离到与安装 Glass 的组件不同的组件中,则必须添加一个 AttributeConfigurationLoader 指向您的模型所在的组件。基础和派生类型都在同一个程序集中,所以我不确定这是否能解决问题,但我还是尝试了。

我的自定义加载器配置如下所示:

public static  class GlassMapperScCustom
{
    public static void CastleConfig(IWindsorContainer container)
    {
        var config = new Config {UseWindsorContructor = true};

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

    public static IConfigurationLoader[] GlassLoaders()
    {

        var attributes = new AttributeConfigurationLoader("Project.Data");

        return new IConfigurationLoader[] {attributes};
    }
    public static void PostLoad(){
        //Remove the comments to activate CodeFist
        /* CODE FIRST START
        var dbs = Sitecore.Configuration.Factory.GetDatabases();
        foreach (var db in dbs)
        {
            var provider = db.GetDataProviders().FirstOrDefault(x => x is GlassDataProvider) as GlassDataProvider;
            if (provider != null)
            {
                using (new SecurityDisabler())
                {
                    provider.Initialise(db);
                }
            }
        }
         * CODE FIRST END
         */
    }
}

在执行自定义加载程序配置时,我现在得到一个“发现模糊匹配”异常。我检查了该程序集中的类中是否设置了任何其他非 Glass 属性,但没有。

有什么想法吗?我猜有 2 个问题。

  1. 为什么使用推断类型属性不能加载正确的类型而只加载基本类型?
  2. 为什么当我尝试通过添加自定义属性加载器来解决此问题时,会出现异常?

【问题讨论】:

  • 你有堆栈跟踪吗?
  • 玻璃自定义配置类代码如下: var attributes = new AttributeConfigurationLoader("x.Data");返回新的 IConfigurationLoader[] {属性};
  • 堆栈跟踪太长,无法完整发布:[AmbiguousMatchException: Ambiguous match found.] System.RuntimeType.GetPropertyImpl(String name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] 修饰符)+13922767 System.Type.GetProperty(字符串名称)+65 Glass.Mapper.Utilities.GetProperty(类型类型,字符串名称)+330 Glass.Mapper.Utilities.GetAllProperties(类型类型)+710 Glass.Mapper .Configuration.Attributes.d__2.MoveNext() +169
  • Glass.Mapper.Configuration.Attributes.AttributeTypeLoader.LoadType(类型类型)+641 Glass.Mapper.Configuration.Attributes.AttributeTypeLoader.Load() +100 Glass.Mapper.Configuration.Attributes.AttributeConfigurationLoader。 LoadFromAssembly(Assembly assembly) +391 Glass.Mapper.Configuration.Attributes.AttributeConfigurationLoader.b__0(String assemblyName) +94 System.Linq.WhereSelectArrayIterator2.MoveNext() +82 System.Linq.Enumerable.Aggregate(IEnumerable1 source, Func`3 func) +123
  • 如果您有任何带有索引器的类,则可能会发生此问题。

标签: sitecore sitecore7 glass-mapper


【解决方案1】:

Widgets 属性有两个属性 - 它要么映射到项目的子元素,要么映射到字段,不能两者兼而有之。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多