Check glass type V3 at runtime

Using Glass Mapper V3, can I check if the Sitecore element supports a specific Glass Mapper class / interface?

Given these classes

[SitecoreType]
public partial interface IPage : IGlassBase
{
  // ... some properties here ...
}

[SitecoreType]
public partial interface IRateableItem : IGlassBase
{
  // ... some properties here ...
}

I would like to do something like this

var context = SitecoreContext();
var item = context.GetCurrentItem<IRateableItem>();
if (item != null)
  // it an item that is composed of the Rateable Item template

Unfortunately, if I do this, I get an element of return type IRateableItem, regardless of whether the current element of this template is composed or not.

+4
source share
3 answers

Dan

Another solution would be to create a custom task that runs in the ObjectConstruction pipeline.

Something like that:

public class LimitByTemplateTask : IObjectConstructionTask
{
    private static readonly Type _templateCheck = typeof (ITemplateCheck);

    public void Execute(ObjectConstructionArgs args)
    {
        if (args.Result != null)
            return;

        if ( _templateCheck.IsAssignableFrom(args.AbstractTypeCreationContext.RequestedType))
        {
            var scContext = args.AbstractTypeCreationContext as SitecoreTypeCreationContext;
            var config = args.Configuration as SitecoreTypeConfiguration;

            var template = scContext.SitecoreService.Database.GetTemplate(scContext.Item.TemplateID);

            //check to see if any base template matched the template for the requested type
            if (template.BaseTemplates.All(x => x.ID != config.TemplateId) && scContext.Item.TemplateID != config.TemplateId)
            {
                args.AbortPipeline();
            }
        }
    }
}


public interface ITemplateCheck{}

IRateableItem inteface, , ITemplateCheck:

[SitecoreType(TemplateId = "CF9B175D-872E-439A-B358-37A01155EEB1")]
public interface IRateableItem: ITemplateCheck, IGlassBase{}

, Castle IOC GlassMapperScCustom:

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

        container.Register(
            Component.For<IObjectConstructionTask>().ImplementedBy<LimitByTemplateTask>(),
            );
        container.Install(new SitecoreInstaller(config));
    }

, , - .

+4

. :

TemplateId SitecoreType :

[SitecoreType(TemplateId = "{your-template-id}")]

GetCurrentItem < > () InferType = true:

 var context = SitecoreContext();
 var item = context.GetCurrentItem<IGlassBase>(InferType: true);
 if (item is IRateableItem)
  {
    var rateableItem = item as IRateableItem;
    // do more...
  }

TemplateID InferType: true, Glass , IGlassBase TemplateID.

, .

+2

I used this code to determine if an Item can be loaded as a glass model of a certain type. I used your type IRateableItem as an example:

public void Main()
{
    var item = Sitecore.Context.Item;

    if (item.TemplateID.Equals(GetSitecoreTypeTemplateId<IRateableItem>()))
    {
        // item is of the IRateableItem type
    }
}

private ID GetSitecoreTypeTemplateId<T>() where T : class
{
    // Get the GlassMapper context
    var context = GetGlassContext();

    // Retrieve the SitecoreTypeConfiguration for type T
    var sitecoreClass = context[typeof(T)] as SitecoreTypeConfiguration;

    return sitecoreClass.TemplateId;
}

private SitecoreService GetSitecoreService()
{
    return new SitecoreService(global::Sitecore.Context.Database);
}

private Glass.Mapper.Context GetGlassContext()
{
    return GetSitecoreService().GlassContext;
}

EDIT:
Add this extension method so that you can determine if the template inherits from a specific base template.

public static bool InheritsFrom(this TemplateItem templateItem, ID templateId)
{
    if (templateItem.ID == templateId)
    {
        return true;
    }

    foreach (var template in templateItem.BaseTemplates)
    {
        if (template.ID == templateId)
        {
            return true;
        }

        if (template.InheritsFrom(templateId))
        {
            return true;
        }
    }

    return false;
}

So now you can do this:

if (item.Template.InheritsFrom(GetSitecoreTypeTemplateId<IRateableItem>()))
{
  // item is of type IRateableItem
}
+2
source

All Articles