Skip to content
Lars-Erik Aabech edited this page Oct 20, 2013 · 10 revisions

Recommended uSync setup:

<usync read="true" write="false" attach="true" .../>

Only use write="true" when you don't want to use code-first. It will overwrite your code before reading it!

DO NOT CODE FIRST BEFORE FIRST RUN OF USYNC! YOU'LL LOSE YOUR CODE!

DO NOT WRITE LOGIC IN THE CLASSES, ONLY PROPERTIES! YOU'LL LOSE YOUR CODE!

Classes are partial for this reason

CodeGen configuration

Unless you installed via the package repository, add a file called CodeGen.config to your configuration folder. Otherwise it's already there, but you might want to include it in your project.

The full configuration looks like this:

<CodeGenerator OverwriteReadOnly="false"
  GeneratorFactory="Umbraco.CodeGen.Generators.DefaultCodeGeneratorFactory, Umbraco.CodeGen"
  ParserFactory="Umbraco.CodeGen.Parsers.DefaultParserFactory, Umbraco.CodeGen"
  >
  <DocumentTypes
    ModelPath="~/Models/DocumentTypes"
    Namespace="MyWeb.Models"
    BaseClass="SomeBaseClass"
    GenerateClasses="true"
    GenerateXml="true" />
  <MediaTypes
    ModelPath="~/Models/MediaTypes"
    Namespace="MyWeb.Models"
    BaseClass="SomeBaseClass"
    GenerateClasses="true"
    GenerateXml="true"
    RemovePrefix="" />
  <TypeMappings Default="String">
    <TypeMapping Description="True/false"
                 DataTypeId="38B352C1-E9F8-4FD8-9324-9A2EAB06D97A" 
                 Type="Boolean" />
    <TypeMapping Description="Numeric"
                 DataTypeId="1413AFCB-D19A-4173-8E9A-68288D2A73B8" 
                 Type="Int32" />
    <TypeMapping Description="Upload"
                 DataTypeId="5032A6E6-69E3-491D-BB28-CD31CD11086C" 
                 Type="Int32" />
    <TypeMapping Description="Date Picker with time"
                 DataTypeId="B6FB1622-AFA5-4BBF-A3CC-D9672A442222" 
                 Type="DateTime" />
    <TypeMapping Description="Approved Color"
                 DataTypeId="F8D60F68-EC59-4974-B43B-C46EB5677985" 
                 Type="System.Drawing.Color" />
    <TypeMapping Description="Folder Browser"
                 DataTypeId="CCCD4AE9-F399-4ED2-8038-2E88D19E810C" 
                 Type="Object" />
    <TypeMapping Description="Date Picker"
                 DataTypeId="23E93522-3200-44E2-9F29-E61A6FCBB79A" 
                 Type="DateTime" />
    <TypeMapping Description="Content Picker"
                 DataTypeId="158AA029-24ED-4948-939E-C3DA209E5FBA" 
                 Type="Int32" />
    <TypeMapping Description="Media Picker"
                 DataTypeId="EAD69342-F06D-4253-83AC-28000225583B" 
                 Type="Int32" />
    <TypeMapping Description="Macro Container"
                 DataTypeId="474FCFF8-9D2D-11DE-ABC6-AD7A56D89593" 
                 Type="Object" />
  </TypeMappings>
</CodeGenerator>

###CodeGenerator element

OverwriteReadOnly

For everyone lucky enough to have a SCS locking the files.

GeneratorFactory and ParserFactory

The ones in the sample config above are the defaults. They can be omitted.

You can create your own factories, generating whichever composition of generators (and preferably matching parsers) you want. See this page for a sample, and check out the unit-tests and source to see how you can "roll your own".

DocumentTypes and MediaTypes elements

ModelPath

If you have ReSharper, you may set the model folders as Namespace Provider=False. Otherwise, make a folder that has the namespace structure you want.

If GenerateXml is set to true, the code generation will attemt to create content type XML for ALL classes found in the specified folder.

Tip: don't put your base class in there

BaseClass

A class in the same namespace as the generated/code-first classes with at least the following structure:

public class DocumentTypeBase
{
    protected IPublishedContent Content; // Can be field or property, must be named Content

    protected DocumentTypeBase(IPublishedContent content) // Must implement ctor with one IPublishedContent arg
    {
        Content = content;
    }
}

The class itself can be named whatever.

It should however not be in the synced folders, keep it one step up, for instance under Models.

*Tip: You can make your own property bodies relatively easy, thereby controlling how your base class should look. Take a look at the source. :) *

Namespace

Preferably the correct namespace for your model folder. :)

GenerateClasses

Whether to generate class for document types when saved. (Works with uSync attach=true or uSync write=true)

GenerateXml

Whether to generate XML for document types when Umbraco starts. (Works with uSync read=true)

TypeMappings

Pretty self explanatory if you should use this tool at all. ;) For some reason the CSharpCodeDomProvider adds @ to intristic type aliases. (string, int etc.) Use the class names of the types to avoid it. DataTypeId = the editor ID.

Now, read all about the usage.