Marketing

Quick intro to Glass Sitecore Mapper

Apr 25, 2013
Pablo Bustamante

Glass Sitecore Mapper is an open source solution that maps “Sitecore items directly into an object model, this allows you to model your entire Sitecore solution in code[1].” It is an object mapping framework whose main features are: creating an object model directly from Sitecore Items, allowing them to be easily mocked/unit tested, and allowing code first templates to be created.

The installation of this framework can be easily done with NuGet, or by just downloading the code and dependencies, compiling them and including them in your project.

Using the framework to turn your Sitecore templates into the model is done by creating classes like this:

  [SitecoreClass]

  public class Example

  {

      [SitecoreField]

      public virtual string ExampleField1 { get; set; }

      [SitecoreField]

      public virtual string ExampleField2 { get; set; }

     

  }

It is important to note that all properties should be marked as virtual to allow proxy items. The property must also have the correct type (string, in this case).

After completing this you will need to include the following lines of code in the project Global.ascx:

  protected void Application_Start(object sender, EventArgs e)

  {

      AttributeConfigurationLoader loader = new AttributeConfigurationLoader(

         new string[] { "Example.Application.Domain, Example.Application" }

         );

      Persistence.Context  context = new Context(loader, null);

  }

Finally you can access your items through the model using these lines:

  ISitecoreContext context = new SitecoreContext();

  Example item = context.GetCurrentItem<Example>();

  item.ExampleField1

There is more that can be done with Glass Sitecore Mapper than explained in this quick intro. To learn more about this mapping framework, its features, and to download it yourself, visit: http://www.glass.lu/.

__________________________________________

[1]
http://www.glass.lu/

[Framework image courtesy of Elsie esq. via Flickr Creative Commons]