Wiki

Clone wiki

TraceabilityV2 / TraceCodeGenLib / FactoryGenerator

Home

TraceCodeGenerator Lib

#FactoryGenerator

The FactoryGenerator is used to generate factory classes for a functional class. For each public constructor on the functional class, the factory will have a public virtual Construct() method created. Any parameters, including default values, out, or ref, in the functional class's constructors will appear in the corresponding Construct() method.

The default name of the factory will be the name of the class being constructed with "Factory" added as a suffix.

The Factory Generator implements all parts of IClassGenerator

###public FactoryGenerator(Type classType, string name_space, string factoryName = null) Constructs a FactoryGenerator that can be used to generate factories for functional classes.

####classType This is the class the generated factory will produce.

####name_space This parameter provides the namespace the generated factory will inhabit.

####factoryName Optional: if this is used, it will override the class name in the generated code, as well as the filename used to write the code to disk.

####skipMap Currently has no effect as the skipMap does not impact constructors.

###public FactoryGenerator(string typeName, string name_space, string factoryName = null, SkipMap skipMap = null) Constructs a FactoryGenerator that can be used to generate factories for functional classes.

####typeName The qualified name of the type the factory will produce.

####name_space This parameter provides the namespace the generated factory will inhabit.

####factoryName Optional: if this is used, it will override the class name in the generated code, as well as the filename used to write the code to disk.

####skipMap Currently has no effect as the skipMap does not impact constructors.

##Example:

namespace Functional.Entities
{
    public class Example
    {
        public Example() { ... }
        public Example(int i) { ... }
        public Example(string s) { ... }

        public virtual string Data { get; set; }

        public virtual void Method1()
        { ... }

        public virtual string Method2(string s)
        { ... }
    }
}

We create our code generator and write the results to a file:

var codeGen = new FactoryGenerator(typeof(Example), "Functional.Factories");
var fileWriter = new FileWriter();
fileWriter.CreateFile(codeGen);

The following code will be written to {SolutionDir}/Functional/Factories/ExampleFactory.cs

namespace Functional.Factories
{
    public class ExampleFactory
    {
        public virtual Example Construct()
        {
            return new Example();
        }

        public virtual Example Construct(int i)
        {
            return new Example(i);
        }

        public virtual Example Construct(string s)
        {
            return new Example(s);
        }
    }
}

Updated