Easy access of datasource and rendering parameters from your controllers using Glass Mapper for Sitecore without GlassController

In a recent project I was working on, I found myself needing a way to be able to get at the rendering parameters and / or datasource of the rendering when using controller renderings. Below is a quick snippet to show one approach of how you can approach this problem.


   public class ControllerSitecoreContext : SitecoreContext, IControllerSitecoreContext
    {
        private IGlassHtml glassHtml;

        public ControllerSitecoreContext(IGlassHtml glassHtml)
        {
            this.glassHtml = glassHtml;
        }

        public T GetRenderingParameters() where T : class
        {
            string parameters = RenderingContext.CurrentOrNull.Rendering["Parameters"];
            if (String.IsNullOrEmpty(parameters))
            {
                return default(T);
            }

            return glassHtml.GetRenderingParameters(parameters);
        }

        public T GetDataSource() where T : class
        {
            string dataSource = RenderingContext.CurrentOrNull.Rendering.DataSource;

            if (String.IsNullOrEmpty(dataSource))
            {
                return default(T);
            }

            Guid dataSourceId;

            if (dataSource.StartsWith("query:"))
            {
                string query = dataSource.Remove(0, 6);
                var item = PageContext.Current.Item.Axes.SelectSingleItem(query);
                if (item != null)
                    return item.GlassCast();
                else
                    return default(T);
            }
            else
            {
                return Guid.TryParse(dataSource, out dataSourceId)

                    ? GetItem(dataSourceId)

                    : GetItem(dataSource);
            }
        }

    }


    public interface IControllerSitecoreContext : ISitecoreContext
    {
        T GetDataSource() where T : class;

        T GetRenderingParameters() where T : class;
    }

It’s worth noting that by inheriting from the regular SitecoreContext provided by Glass Mapper, you can treat and use this in the same way as the regular ISitecoreContext in Glass Mapper for Sitecore. In use you can simply then inject your IControllerSitecoreContext as a dependency – something like this

    public class MyController : Controller
    {
        private IControllerSitecoreContext sitecoreContext;

        public MyController(IControllerSitecoreContext sitecoreContext)
        {
            this.sitecoreContext = sitecoreContext;
        }
    }

Hope this helps Nat.

Advertisements

4 thoughts on “Easy access of datasource and rendering parameters from your controllers using Glass Mapper for Sitecore without GlassController

  1. I know we’re having this conversation on Twitter, but if you don’t require multiple inheritance then you can inherit from Glass.Mapper.Sc.Web.Mvc.GlassController which give you access to GetRenderingItem (the datasource), GetControllerItem (datasource or current item) and GetRenderingItem (rendering parameters).

    The GlassController also gives you access to SitecoreContext, so in the above example you should be able to remove that inheritance, not requiring multiple inheritance unless you have a more complex usage you have where chaining to GlassController is not going to be possible.

    • I missed the conversation on Twitter, but I like this topic….
      How can you mock the GetRenderingParameters if you inherit your controller from GlassController?
      I like the approach or having a base controller, but I think that’s harder to mock? maybe I am saying something stupid, would not make life easier to add the GetRenderingParameters somewhere else that’s easier to mock?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s