Using Multiple Configurations

Kam Figy edited this page Jan 16, 2015 · 2 revisions

Starting with version 8, Synthesis allows for multiple generation configurations. Multiple configurations are useful if you want to have multiple models that are isolated from each other (such as for a multi-site solution). The first configuration, Default Configuration, is registered in the Synthesis.config file.

Additional configurations can be registered similarly to areas in MVC: you create a class that derives from ConfigurationProviderConfiguration (or IProviderConfiguration if feeling brave) and configure the providers as you wish. You can set what templates are generated, namespaces, field mappings - anything in a provider.

Here's an example configuration registration:

public class SiteModelConfiguration : ConfigurationProviderConfiguration
{
	private SiteModelConfiguration() 
    {
        Name = "Site Model";
    }

    public static void Register()
	{
		var configuration = new SiteModelConfiguration();
		ProviderResolver.RegisterConfiguration(configuration);
	}

	protected override IGeneratorParametersProvider LoadGeneratorParametersProviderFromConfig()
	{
		var siteIdentifier = Name.Replace(" ", string.Empty);

		// we call base here so the parameters begin as a copy of what's in global config (e.g. base classes, etc)
		var parameters = (ConfigurationGeneratorParametersProvider)base.LoadGeneratorParametersProviderFromConfig();

		parameters.InterfaceNamespace = "Foo.Sites.{0}.Models.Generated".FormatWith(siteIdentifier);
		parameters.InterfaceOutputPath = "~/Areas/{0}/Models/Generated/_Interfaces.cs".FormatWith(siteIdentifier);
		parameters.ItemNamespace = "Foo.Sites.{0}.Models.Generated.Concrete".FormatWith(siteIdentifier);
		parameters.ItemOutputPath = "~/Areas/{0}/Models/Generated/_Concrete.cs".FormatWith(siteIdentifier);
		parameters.TemplatePathRoot = "/sitecore/templates/Sites/{0}".FormatWith(Name);

		return parameters;
	}

	protected override ITemplateInputProvider LoadTemplateInputProviderFromConfig()
	{
		var templates = new ConfigurationTemplateInputProvider();
		templates.AddTemplatePath("/sitecore/Templates/Sites/{0}".FormatWith(Name));
		templates.AddTemplateExclusion("/sitecore/Templates/Sites/{0}/Components".FormatWith(Name));
		templates.AddTemplateExclusion("/sitecore/Templates/Sites/{0}/Folders".FormatWith(Name));
		templates.AddFieldExclusion("__*");

		return templates;
	}
}

Then the configuration must be registered with Synthesis. The easiest way to do this is by using WebActivator to register it on application start:

[assembly: PreApplicationStartMethod(typeof(SiteModelConfiguration), "Register")]

Now when you regenerate, both configurations are generated separately.

Note: You should not generate the same template in two different configurations. This can result in unexpected typing issues, as the mapper will choose to give you one of the classes only. To get around this, use Friend Metadata.

Friend Metadata

Sometimes you may wish to separate models, but have a core model of shared templates. Normally this would cause a conflict, as having overlapping templates in separate configurations is a Bad Thing.

Enter Friend Metadata. This is a way you can inform a configuration about template interfaces that already exist that it may reference during generation - like a core model. For example:

  • MyProject.Kernel.Model.IPageItem represents a common page base template in a multisite solution
  • MyProject.AwesomeSite.Model.IAwesomePage is a site-specific template that inherits from the shared Page template.
  • You don't want an IPageItem in your AwesomeSite model, as that would mean two types represented the same template.
  • So, you add the metadata from the Kernel model to the AwesomeSite configuration - and then the IAwesomePage : MyProject.Kernel.Model.IPageItem

To use Friend Metadata, you generate the metadata for the configuration you want to friend, then add it to your configuration parameters like so when registering it:

        var globalMetadata = ProviderResolver.GetConfiguration("Default Configuration");

		Assert.IsNotNull(globalMetadata, "Configuration Default Configuration did not exist!");

		parameters.AddFriendMetadata(globalMetadata.CreateMetadataGenerator().GenerateMetadata());

When using Friend Metadata, make sure that the model with friends has a reference to the assembly containing the friend interfaces!

You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.