سؤال

I have downloaded Sitecore 7 Autohaus demo for learning purpose. I notice that in Autohaus code, there is a model - Car. I would like to know how does sitecore know how to map between Car model (code) and CarModel template (sitecore template).

هل كانت مفيدة؟

المحلول

There are a few more steps between the car template and the car object model.

One of the main features of Sitecore 7 is the embedded search capability. When items, created from the car template, are saved, that information is stored in a search index (Lucene or Solr)

The Car model is not mapped directly from the template or the database item but from the search document created.

When you use the LINQ layer e.g.

var index = ContentSearchManager.GetIndex("sitecore_master_index");
using (var context = index.CreateSearchContext())
{
    var query= context.GetQueryable<Car>()
                .Where(item => item.Seats == 2);
}

Sitecore will execute a search and then take the 'Car' object and populate/hydrate it with the information from the search results by use of Sitecore's DocumentMapper.

This will populate public properties and also indexers of the Car object. The DocumentMapper takes care of casting to and from object types for you (such as DateTime / int etc).

The DocumentMapper will try and map properties with matching field names but you can place attributes on the object properties to help Sitecore map specifically to your objects.

This example tells Sitecore to map the field 'modelkey' to the property ModelId.

[IndexField("modelkey")]
public string ModelId { get; set; }

You can see the LINQ queries used in Autohaus on most of the pages and should be a great resource for learning how Sitecore 7 works.

More info about various parts of Sitecore 7 can be found here: http://www.sitecore.net/Community/Technical-Blogs/Sitecore-7-Development-Team.aspx

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top