Glass.Mapper.Sc What and Why
Glass.Mapper.Sc is a object relational mapper (ORM) for the Sitecore platform. Glass Mapper allows you to map data from your Sitecore database directly into strongly typed C# models.
The framework was created with the following goals:
- A developer must be able to hand write a plain old c# object and for this to be mapped.
- It must support the Experience Editor.
- It must be a read and write framework.
- It must be extensible.
- It must map all of the basic attributes of a Sitecore item.
- It must be able to describe the complex item relationships in Sitecore.
- It must be very simple to setup and intuitive to use.
Glass Mapper removes the need for developers to spend their time writing boilerplate code to convert data from the string values stored by Sitecore to .NET types. A Sitecore developer's life is made easier because they can focus on solving business problems and not data conversion. This also keeps the code cleaner and more maintainable.
Glass Mapper is also not just about mapping simple field values to properties, it also provides developers the ability to create complex object graphs that describe the complex business relationships between items. Glass.Mapper.Sc can map simple parent / child relationships and more complex relationships that require pulling items from any location in the Sitecore database. With each relationship you can also define rules about how that relationship should be mapped to filter out items that should not be part of your particular object graph.
Glass provides all this without sacrificing the key features that make Sitecore such a great platform. Your strongly typed models can work with the Experience Editor allowing you to make use of Editable fields without having to convert back to the original Sitecore item. You can also cache models, read and write data, use strongly typed Rendering Parameters and create Edit Frames.
The code below is an example of the code you would need to create using the Sitecore API and Glass API to filter a collection of items.
Using the Sitecore API.
Using the Glass.Mapper.SC API.
The example shows how simple and easy to read your business logic becomes. This allows developers to write code in a more intuitive manner making it easier to maintain and support over the entire life of the application.
Additionally models can be reused by other parts of the application. If the model needs to change, for example a property gets deleted, then the code will break when a developer tries to compile. No more hunting down field names based on magic strings.
Compile time checks mean that these common mistakes will be easily caught:
- Removing a field and forgetting to update references.
- Trying to convert between two different data types.
- Referencing properties and fields that don't exist.
By taking advantage of the compiler developers save time by not having to track down bugs at runtime.
Your code can be unit tested! Your interactions with Sitecore are via services that come with an interface, and all models are either plain old C# objects or interfaces. The service interfaces and plain old C# object models allow a developer to quickly write unit tests. You can choose any unit testing and mocking to use with Glass.Mapper.Sc, there are no restrictions.