Sitecore Glass Mapper: Attributes on Interfaces or Specific Classes?

We are new to Glass Mapper and would like to use it in our Sitecore project. When we looked at the lessons, we noticed that there were no deep examples of how to set up the deep inheritance that Sitecore allows. While browsing the web, we noticed that people who work with placing attributes on interfaces, and on the other hand, place attributes on specific classes there. None of these examples explains their good reasons for this, but leaves us with the question: which is the right use and what is the influence of one or the other?

Consider the following:

Template: Content (which is a field section template that adds 2 simple fields: Title, Body) This template is directly and indirectly inherited by many of our templates.

Now in one of our sublayers we use only this section, and this is rather more general control, so we need to do: GetCurrentItem<Content>or GetCurrentItem<IContent>.

Personally, I find it GetCurrentItem<IContent>more intuitive, as it seems to me that he asks: "Give me the current element if it supports the content section, where the other is more similar." Give me the current element if it is a content section "(which is technically impossible, since content elements are never created)

+5
source share
2 answers

Glass Mapper . -, Glass Mapper - . , Glass Mapper .

.

Glass.Sitecore.Mapper mapper , Castle Dynamic Proxies , .

, .

- . , SitecoreService Glass Mapper . . TemplateId SitecoreClass . .

public interface ISitecoreItem {

    Guid ID{ get; }

    Language Language{ get; }

    int Version { get; }

    string Url { get; }
}

[SitecoreClass]
public partial interface IHeader : MyProject.Content.ISitecoreItem 
{

    Link LogoLink  {get; set;}

    Image Logo  {get; set;}

}



    [SitecoreClass(TemplateId="87d5b6c1-a084-4738-be11-b4e6fe07d894")]
    public partial class Header  : IHeader 
    {
        [SitecoreId]
        public virtual Guid ID{ get; private set;}

        [SitecoreInfo(SitecoreInfoType.Language)]
        public virtual Language Language{ get; private set; }

        [SitecoreInfo(SitecoreInfoType.Version)]
        public virtual int Version { get; private set; }

        [SitecoreInfo(SitecoreInfoType.Url)]
        public virtual string Url { get; private set; }

        [SitecoreField(FieldName = "Logo Link" )]
        public virtual Link LogoLink  {get; set;}

        [SitecoreField(FieldName = "Logo" )]
        public virtual Image Logo  {get; set;}


    }

var service = new SitecoreService(Sitecore.Context.Database);
var header = service.CreateClass<IHeader>(false /* don't lazy load */, true /* infer type */, headerItem);
+9

Sitecore , , . , Sitecore. ,

public interface IMyPageTemplate : IBaseTemplate1, IBaseTemplate2 {

}

, . , (, ) - . , , , IContent, , , .

- GetCurrentItem<IContent>(). , - -, ( , ).

+1

All Articles