Sometimes I’m amazed with what some of our developers do. I’ve seen so many examples of code just thrown together. I’ve seen that code produce infinite loops, slow down load time by a factor of 10, and take down servers. So when I see an example of someone thinking through how to efficiently create code, I’m amazed. Anyway, Parshva Vora, a Sitecore developer here at Perficient, just blogged on how you can use mapping to create agile code and more efficiently get at the latest content. I’ll use some of his own words
Let’s talk about motivation first. I was dealing with a problem involving aggregate object where information needed to be retrieved seamlessly from various and diverse data sources including search index without spreading data access code into my models and views. Does’t it sound like classic case of data mapping problem? In fact, Sitecore 7 content search APIs similar approach where indexed content is filled into specified .Net object(typically into SearchResultItem or its derivative). And mapping is directed through IndexField, IgnoreIndexField and few other attributes. Now it begs an obvious question, why would I use Glass.Mapper? Consider following scenario:
Say you are dealing with an item with large number of fields and not all of them are being indexed(indexing comes at a cost so you like to index select item fields, for instance fields that appear in search results). And while processing search results, some of those non-indexed fields need to be referenced. One way is to make sure that the ID field gets indexed and then using ID, you retrieve related item from database. However the process could be non-transparent and messy, particularly if field is complex or nested field type and there are several fields. So instead of you pulling information from database with Sitecore APIs, Glass.Mapper can do it for you, with the help of mapping attributes. Consider following model for an article where its number, title and summary are retrieved from index and content and images are retrieved from database.
Parshva goes on to get into how it works and to show the benefits of the mapping approach. It’s worth reading the whole article if you have similar needs.