InsertClass Merge Strategy

The InsertClass Merge Strategy is useful when you want to insert your template output into a previously existing class in the output file. At first this may sound very similar to the Insert Region Merge Strategy, and indeed it did start out that way; however, this Merge Strategy has many additional settings and features that separate it from it's other fellow Merge Strategies, and that make it a very robust and powerful tool.

Configuration Options

I think the best way to describe this Merge Strategy is through example; but before we can do that, we must first go over it's configuration options.

LanguageString, RequiredOnly Support C# and VB.

ClassName

String, RequiredName of the class to insert into.
PreserveClassAttributesBoolean, defaults to FalseWhether or not the merge should preserve the existing classes attributes. By default, the merge tries to replace the entire existing class, which includes the attributes on the top of the class; this option leaves the attributes from the top of the original class.
OnlyInsertMatchingClassBoolean, defaults to FalseInsert the whole template output or just the matching class.
MergeImportsBoolean, defaults to FalseMerge the import/using statements of the existing file and generated output.
NotFoundActionEnum, defaults to None

What to do if the class is not found in the existing file. There are three options:

None: Don't merge anything, just leave the existing file as is.

InsertAtBottom: Append the output of the template to the bottom of existing file.

InsertInParent. Insert the output of the template at the bottom of a speficied parent section (specified by the NotFoundParent property).

NotFoundParentString, no default

If you specified InsertInParent for the NotFoundAction configuration, you must specify a name for the parent region. This can be the name of a Region or a Class.

Example Configuration...

Language: C#
ClassName: "Pet"
PreserveClassAttributes: True
OnlyInsertMatchingClass: True
MergeImports: True

Existing File

using System;
using System.ComponentModel.DataAnnotations;
namespace Petshop
{
    [ScaffoldTable(true)]
    public class Pet
    {
        public int Age { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }
}

The Generated Output

using System;
using System.Text;
namespace Petshop
{
    public class Pet
    {
        public string FirstName { get; set; }

        public string LastName { get; set; }

        public string FullName
        {
            get { return String.Format("{0} {1}", FirstName, LastName); }

        }
    }
}

Insert Class Merge Strategy Result!

using System;
using System.ComponentModel.DataAnnotations;
using System.Text;
namespace Petshop
{
    [ScaffoldTable(true)]
    public class Pet
    {
        public string FirstName { get; set; }

        public string LastName { get; set; }

        public string FullName
        {
            get { return String.Format("{0} {1}", FirstName, LastName); }

        }
    }
}

As you can see in the merged result above, the class attribute was preserved and the Age property was removed.