0

The following code was working before the upgrade to the latest glass mapper. Since removing the GlassController inheritance the following no longer works. Is there something in the latest version that should be used instead?

var model = GetDataSourceItem<IContent_Item>();

1 Answer 1

3

With the Glass version 5+, you need to convert the controller base class from Glass Controller to Controller(.NET class). Then you need to use IMVContext to get items/context from Sitecore, I used IMvcContext when I am working with MVC. your code will look like below -

 IMvcContext mvcContext = new MvcContext();
 var model = mvcContext.GetDataSourceItem<IContent_Item>();

A better approach is to register all of these new Glass services with the Sitecore dependency injection container. Use the below code to register all Glass Mapper services with the Sitecore container -

public class GlassMapperConfigurator : IServicesConfigurator
    {
        public void Configure(IServiceCollection serviceCollection)
        {
            serviceCollection.AddSingleton<Func<Database, ISitecoreService>>(_ => CreateSitecoreService);
            serviceCollection.AddScoped(_ => CreateSitecoreContextService());
            serviceCollection.AddScoped(_ => CreateRequestContext());
            serviceCollection.AddScoped(_ => CreateGlassHtml());
            serviceCollection.AddScoped(_ => CreateMvcContext());
            serviceCollection.AddSingleton<Func<ISitecoreService>>(_ => Get<ISitecoreService>);
            serviceCollection.AddSingleton<Func<IRequestContext>>(_ => Get<IRequestContext>);
            serviceCollection.AddSingleton<Func<IGlassHtml>>(_ => Get<IGlassHtml>);
            serviceCollection.AddSingleton<Func<IMvcContext>>(_ => Get<IMvcContext>);

        }

        private static ISitecoreService CreateSitecoreService(Database database)
        {
            return new SitecoreService(database);
        }

        private static ISitecoreService CreateSitecoreContextService()
        {
            var sitecoreServiceThunk = Get<Func<Database, ISitecoreService>>();
            return sitecoreServiceThunk(Sitecore.Context.Database);
        }

        private static T Get<T>()
        {
            return ServiceLocator.ServiceProvider.GetService<T>();
        }

        private static IRequestContext CreateRequestContext()
        {
            return new RequestContext(Get<ISitecoreService>());
        }

        private static IGlassHtml CreateGlassHtml()
        {
            return new GlassHtml(Get<ISitecoreService>());
        }

        private static IMvcContext CreateMvcContext()
        {
            return new MvcContext(Get<ISitecoreService>(), Get<IGlassHtml>());
        }
    }

Then patch it like this -

<configuration>
  <sitecore>
    <services>
      <configurator type="YourAssembly.GlassMapperConfigurator, YourAssembly" />
    </services>
  </sitecore>
</configuration>

finally, your code will be like this -

public YourController : Controller
{
  private readonly IMvcContext _mvcContext;

  public YourController (IMvcContext mvcContext)
  {
    var _mvcContext = mvcContext ?? throw new ArgumentNullException(nameof(mvcContext));
  }
  
  public ActionResult YourAction()
  {
    var model = mvcContext.GetDataSourceItem<IContent_Item>();
    return View(model);
  }
}

Here you don't need to Function the IMvcContext and can use it directly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.