How To Map Sitecore Objects into C# Objects using Synthesis

As a developer using C#, when we build websites we ideally want our website entities to be defined, or, at least mapped in code. This mapping allows us to create strongly typed models. Strongly typed models make the code more robust, allows unit testing of key aspects of the site. Some CMS’s like Episerver have a built-in mechanism to do this, however, out of the box Sitecore doesn’t provide any such ability.

Trying to manually map Sitecore entities to objects is very fragile and very boring. If someone changes a property name in Sitecore, your website could just suddenly break 🙁 For this reason, several developers have gone off and created their own solutions to solve the problem for the rest of us).

In today’s guide, I’m going to use a package called ‘Synthesis’ to do the work for us.

Installing Synthesis in Sitecore

Synthesis can be installed via NuGet, so the first thing you need to do is open Visual Studio, right-click on your solution and select ‘Manage NuGet Packages for Solution…’

Sitecore_Synthesis_1

Running Synthesis in Sitecore

After installing Synthesis, if you look in your website’s ‘App_Config’ folder, you should see one or more config files depending on which version you have installed. If you are using Synthesis 8.0 or below you should see a single file called ‘Synthesis.config’. This is the main file that we can use to configure Synthesis. If you are using 8.1 then you should see three config files, Synthesis.config, Synthesis.ControlPanel.config and Synthesis.Startup.config.

In a lot of Sitecore installs, teams have decided to stop working within the webroot. If you are new to this concept then I suggest you read ‘How To Architect Your Sitecore Website With MVC‘.

If you are working in this type of set-up, then the ‘Configuring Synthesis’ section will probably be very useful for you. After you have your website configured, all you need to do is open up a browser and type www.website.com/synthesis.aspx. If you are using an earlier version, then swap synthesis.aspx with synthesis.axd. If everything has run correctly, you should see a screen like this:

Sitecore_Synthesis_2

Clicking on the ‘Details’ screen, will show you detailed information about which of your templates have been mapped to objects and which ones still need generating. Clicking on the ‘Generate’ button will map your Sitecore items into C# objects!

If you do not see the ‘Generate’ button, then it is very likely that dynamic debug is not enabled. To enable dynamic debug compilation you need to set it in your web.config file.

Configuring Synthesis in Sitecore

In here you have the following settings:

Project Path : This should be the path to your project file that, when Synthesis is run, it will build the Models into.

<setting name="StartupRegenerateProjectPath" value="~/../Code/SitecoreProject.csproj" />

Item Namespace : The Namespace that the entites will be generated under.

<ItemNamespace>SitecoreProject.Models</ItemNamespace>

Interface Namespace : When Synthesis generates the entities, it will also generate associated C# interface class. Synthesis adds a signature to these interfaces. When a generate is run, the generated signature of all Sitecore templates within the database is compared to the signatures of the currently loaded interfaces.

<InterfaceNamespace>SitecoreProject.Interfaces</InterfaceNamespace>

Item OutputPath : The folder on your file system that Synthesis will generate the C# class.

<ItemOutputPath>~/Code/GeneratedModels.cs</ItemOutputPath>

Interface Output Path : The folder on your file system that Synthesis will generate the C# class.

<InterfaceOutputPath>~/Code/GeneratedInterfaces.cs</InterfaceOutputPath>

Use Template Path For Namespace : This forces the code generation to use the InterfaceNamespace and ItemNamespace properties.

<UseTemplatePathForNamespace>false</UseTemplatePathForNamespace>

Template Path Root : The path in Sitecore that you want to generate the Models from. I would be very careful not to include the system items, as this can cause a lot of Visual Studio warnings.

<TemplatePathRoot>/sitecore/templates/company</TemplatePathRoot>

Included Paths : One or more Template Specs to include templates from for generation/sync.
If no included paths are defined, nothing will be generated.

<includedPaths hint="list:AddTemplatePath">
<templates>/sitecore/Templates/Company</templates>
<fake-guid>{516F709D-C3E4-4652-868F-6C2AAD9921A1}</fake-guid>
<wildcard>/sitecore/templates/Sites/*/Pages</wildcard>
</includedPaths>

Exclude Paths : Excludes templates that match a Template Spec from generation/sync.

Conclusion

In today’s tutorial, I’ve covered how to generate C# classes from Sitecore objects using Synthesis. Synthesis is one of many ORM that can be used with Sitecore to improve developer productivity and I would strongly recommend you look into using one.

Synthesis is installable from Nuget and is pretty easy to configure and get started.

Jon D Jones

Software Architect, Programmer and Technologist Jon Jones is founder and CEO of London-based tech firm Digital Prompt. He has been working in the field for nearly a decade, specializing in new technologies and technical solution research in the web business. A passionate blogger by heart , speaker & consultant from England.. always on the hunt for the next challenge

More Posts

3 replies

Trackbacks & Pingbacks

  1. […] project used Synthesis as an ORM mapper, if you have never used Synthesis then I recommend reading, How To Map Sitecore Objects into C# Objects using Synthesis. Synthesis does a pretty good job of mapping Sitecore objects. It does exactly what you would want. […]

  2. […] I recently inherited a Sitecore project that used Synthesis to do the ORM. If you want to read more about Synthesis you can read more about it here, How To Map Sitecore Objects into C# Objects using Synthesis. […]

  3. […] I recently inherited a Sitecore project that used Synthesis to do the ORM. If you want to read more about Synthesis you can read more about it here, How To Map Sitecore Objects into C# Objects using Synthesis. […]

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *