Skip to content

Fluent configuration

Pavel Novikov edited this page Jan 12, 2016 · 20 revisions

Fluent configuration is Reinforced.Typings configuration specified as sequence of calls to Reinforced.Typings.Fluent.ConfigurationBuilder class. It is somehow more comfortable than attributes configuration in some cases. Anyway fluent configuration does not restrict attributes configuration somehow. So attributes are safe to be used together with fluent configuration.

Important! If attributes configuration contrary to the fluent configuration then RT will act according to fluent configuration.

Table of contents

Starting with fluent configuration

To enable fluent configuration for Reinforced.Typings follow 2 simple steps:

  1. Create static class with static method consuming instance of Reinforced.Typings.Fluent.ConfigurationBuilder as follows:

    using Reinforced.Typings.Fluent;
    
    namespace YourProjectNamespace {
        public static class ReinforcedTypingsConfiguration
        {
            public static void Configure(ConfigurationBuilder builder)
            {
                // fluent configuration goes here
            }
        }    
    }
  2. Go to Reinforced.Typings.settings.xml and set there RtConfigurationMethod parameter to full-qualified name of Configure method as follows:

    ...
        <RtConfigurationMethod>YourProjectNamespace.ReinforcedTypingsConfiguration.Configure</RtConfigurationMethod>
    ...
  3. Rebuild your solution. Now your TypeScript code will be generated according to calls made to ConfigurationBuilder.

Configuration builder methods - single and plural

Configuration builder contains 6 main methods:

  • ExportAsInterface/ExportAsInterfaces
  • ExportAsClass/ExportAsClasses
  • ExportAsEnum/ExportAsEnums

Lets call:

  • ExportAsInterface, ExportAsClass, ExportAsEnum - "Single" configuration builder methods
  • ExportAsInterfaces, ExportAsClasses, ExportAsEnums - "Plural" configuration builder methods

"Single" versions of these methods requires to specify exported type as follows:

public static void Configure(ConfigurationBuilder builder)
{
    builder.ExportAsInterface<IMyInterface>();
}

"Single" versions returns instance of interface/class/enum generic configuration builder. To configure exporting details for specified type you should simply continue call chain like this:

public static void Configure(ConfigurationBuilder builder)
{
    builder.ExportAsInterface<IMyInterface>()
            .WithPublicMethods()        // extension methods calls
            .WithPublicProperties()     // to interface configuration builder`
            .OverrideName("IMyCustomInterface");
}

Also it is safe to perform multiple "single" method calls:

public static void Configure(ConfigurationBuilder builder)
{
    builder.ExportAsInterface<IMyInterface>()
            .WithProperty(c => c.MyProperty, p => p.CamelCase());
            
    builder.ExportAsInterface<IMyInterface>()
            .WithProperty(c => c.AnotherProperty, p => p.Type<Func<int,bool>>()); // no crime!
}

"Plural" versions of these methods consumes 2 parameters:

  • set (IEnumerable) of types to be exported
  • action that should be performed on each type's configuration builder

Fluent configuration for "plural" methods looks like this:

public static void Configure(ConfigurationBuilder builder)
{
    builder.ExportAsInterfaces(
        new Type[] {    typeof(IMyInterface), 
                        typeof(IAnotherInterface), 
                        typeof(MyClass)
                   },
        conf => conf.WithPublicMethods().WithPublicProperties();
    );
    // this will export IMyInterface, IAnotherInterface, MyClass
    // as TS interfaces including all public methods and all public properties            
}

Note that in "single" fluent methods returns generic builders but "plural" dont. So you can use strongly typed calls e.g. to specify configuration for particular properties when you are using "single" method. Due to C# type system this approach is not applicable for "plural" methods:

public static void Configure(ConfigurationBuilder builder)
{
    // look, I can do this for .ExportAsInterface<T>!
    builder.ExportAsInterface<IMyInterface>()
            .WithProperty(c => c.MyProperty, p => p.CamelCase()); 
       
    builder.ExportAsInterfaces(
        new Type[] { typeof(IMyInterface) },
        conf => conf
            .WithPublicMethods()
            .WithPublicProperties()
            // but cannot for .ExportAsInterfaces :(
            // .WithProperty(c => c.MyProperty, p => p.CamelCase()) 
            
            // but still can do like this! :)
            .WithProperties(
                prop => prop.Name == "MyProperty",
                conf => conf.CamelCase())
            ;
    );           
}

Classes, interfaces and enums are exported differently. For further information see which fluent methods present for interfaces, classes and enums.

Additional ConfigurationBuilder methods

Besides methods mentioned above ConfigurationBuilder has 2 more:

AddReference

Arguments: string reference: path for ///<reference ...> directive being added to each exported file

Path to file that will be written in ///<reference path="..."> directive that will be placed on top of every generated file (analogue for [TsReference] attribute)

TryLookupDocumentationForAssembly

Arguments:

  • Assembly assmbly: Assembly which documentation should be included
  • string documentationFileName = null: Override XMLDOC file name if differs (please include .xml extension)

Tries to find documentation .xml file for specified assembly and take it in account when generating documentaion. See details on JSDOC support page

Fluent methods for types

### Class

### Enum

### Interface

Fluent methods for members

### Method

### Property

### Enum value

### Method parameter

Clone this wiki locally