Understanding the Source Code


CSCodeGeneration.csproj contains the code you'll be most interested in. The assembly generated by this project allows the T4 templates to easily access Commerce Server and returns simplified representations of Commerce Server metadata entities which can be used to generate source code using T4. The OOB solution uses T4 to generate C# but you could use this assembly to generate JavaScript, HTML, or whatever you want based on Commerce entities and Request Templates.

The easiest way to learn how to use and extend this library might be to dig into the T4 templates and push some things around but I'll highlight a few things you might find useful.

If you want to generate code based on Commerce Entities your entry point is CommerceServerContrib.CodeGeneration.EntityGenerator. This class provides two constructors - one that takes just a configuration file path and one that takes a configuration file path, channel, user locale and user UI locale. The additional properties in the second overload allow you to pass properties which will be used to create a CommerceRequestContext which the generator runs so you can generate entities based on metadata defined in an extended channel for instance. Use GetClassDefinitions() to get a List<ClassDefinition> of enities. There is a helper method in CodeGenerationIncludes.tt which you can use to get the config file of the current project.

Behind the scenes CommerceServerContrib.CodeGeneration.EntityGenerator uses FullConfigurationSwapper() to inject your project's configuration into the app domain of T4 and then uses CommerceServerMetadataProcessor to get the list of ClassDefinitions. CommerceServerMetadataProcessor has a number of virtual methods you can override if you want to change how metadata is queried, ClassDefinitions are generated or how the code generation configuration is interpreted.

If you want to generate code for Request Templates your entry point is CommerceServerContrib.CodeGeneration.RequestTemplateGenerator. Like EntityGenerator, RequestTemplateGenerator has two contructors - one that just takes a path to you configuration file and one that takes channel, user locale and user UI locale so you can override the channel. The OOB T4 templates use GetRequestTemplates() which returns a List<RequestTemplateGroup>.

Request Templates, defined in RequestTemplateDefinitions.xml or a similar file in your Foundation service, have a group attribute. This group typically corresponds to the Commerce Server sub-system the template primarily uses but it can also be a custom term if you want to seperate the RTs (say, to cache them seperately or because you've introduced a 3rd party integration accessable through Commerce Server Foundation) from the existing sub-systems. GetRequestTemplates() returns template defintions by these groups so that the T4 template can put all templates of the same group in the same static class. This makes the generated methods more discoverable as the group becomes a token in the namespace, CommerceServerContrib.MVCSiteTemplate.RequestTemplates.Catalog vs CommerceServerContrib.MVCSiteTemplate.RequestTemplates.Orders for example.

If you want lower level access to the Request Template definitions returned by the CommerceServerContrib.CodeGeneration you can use CommerceServerContrib.CodeGeneration.CommerceServerRequestTemplateGenerator directly. CommerceServerRequestTemplateGenerator has a number of virtual methods you can extend to change how templates are queried and how property definitions are built.


Contains a few test for the Foundation and Metadata access of CommerceServerContrib.CodeGeneration.


Contains a few test for the FullConfigurationSwapper which loads the configuration file from one application into another at runtime.


A Foundation service that supports MVCSample (more on this later) and also holds the files that are packaged to create the Foundation and Two-Tier Nuget packages.


Sample MVC Site for working with the T4 templates in development. This is only uses to development of the T4 files and is not meant to be used as a starting point for Commerce Server websites. You'd be better off starting with the MVCSiteTemplate or your own project with the Commerce Server Nuget packages added than this project. It also holds the extra files that are packaged as part of the Presentation, Two-Tier and Code Generation Nuget packages.

Last edited Nov 14, 2012 at 2:36 PM by bthubbard, version 5


No comments yet.