As mentioned in the part 1 of this series, we will be creating a site using the Glass.Mapper(a.k.a. Glass).
With the use of Glass you can create templates with Code-First models.
So before we start developing our site, we need to install a few NuGet packages.
Install the following packages in the web project (included the versions which worked for me):
- BoC.InversionOfControl.Unity (18.104.22.168)
- BoC.Sitecore.Mvc (22.214.171.124)
- BoC.Persistence.SitecoreGlass (126.96.36.199)
Maybe you already noticed, but these packages have dependencies to other packages. When you install these packages it automatically installed the following packages:
- BoC (188.8.131.52)
- BoC.Web.Mvc (184.108.40.206)
- BoC.Logging.Sitecore (220.127.116.11)
- BoC.Glass.Mapper.Sc (4.0.0)
- BoC.Glass.Mapper.Sc.ContentSearch.LuceneProvider (4.0.0)
Most of these BoC packages are essentially a shell around the Glass.Mapper module. They add some extra functionalities and bug fixes. But others also bring full functionalities you can use while developing an MVC/Sitecore website. For more info about each of these packages, see the package descriptions.
The Glass.Mapper itself is explained on http://glass.lu/Mapper/Sc.aspx. You can also find some nice tutorials there. We will also handle and explain some of the functionalities while creating the MusicStore website.
Enabling Glass.Mapper for Sitecore
Sitecore consists of pipelines to which you can hook onto. You can path these pipelines, through configuration, to execute a bit of code before, after or instead of a pipeline.
To enable usage of Glass and BoC for Sitecore, we need to add some custom configuration to let Glass and BoC hook into those pipelines.
As you might have already noticed, installing the packages also added some configuration files.
These config files add pipelines from the packages to Sitecore. Take the Sitecore.Glass.config for example, all it does is add two pipelines from the eFocus.Sitecore.Glass package.
<configuration xmlns:patch="> <sitecore> <pipelines> <initialize> <processor type="eFocus.Sitecore.Glass.Pipelines.Initialize.RegisterAppDomainGlassConfigLoader, eFocus.Sitecore.Glass" /> <processor type="eFocus.Sitecore.Glass.Pipelines.Initialize.InitializeGlass, eFocus.Sitecore.Glass" /> </initialize> </pipelines> </sitecore> </configuration>
But to enable edit the extension of the Sitecore.GlassCodeFirst.config file in the eFocus folder. Just remove the .exclude from the filename and Sitecore should include it in the configuration.
Next to this config file there is a Glass.Mapper.Sc.CodeFirst.config.example file in the BoC folder. This is almost identical to the one we just included, so we can delete this file.
Last but not least we have to enable the Glass CodeFirst option. Add a new config file to the Include folder with the following content:
After these steps, check if Sitecore is still functioning. It is highly possible that you’ll get some assembly reference errors. You can fix those by redirecting older versions of the assembly to the version of the dll in the bin folder of the website root. You can do this in the web.config. Example:
TIP: You can use Conditional Configs from Chris van de Steeg to transform your Web.config rather then changing it yourself: http://www.chrisvandesteeg.nl/2014/10/06/sitecore-conditional-configs-deploy-just-1-package/
Now we have a fully functioning Sitecore site running with Glass.Mapper support.