Wiki

Clone wiki

TraceabilityV2 / Integration / Factories

Home

Project Integration

#Factories Using Factories will greatly reduce the amount of maintenance necessary to maintain the Traceability layer as it relates to the Functional Layer. The basis of this idea is that anywhere you need to create a Functional Layer class, you may instead create a Traceable Layer class by simply swapping the respective factories. (See Factory Pattern

For the purposes of adding Traceability to a system, all classes will need factories to generate them. This is due to the fact that each class needs to be able to have its base version replaced with the corresponding Traceable version. By using factories everywhere, switching between the functional classes and the traceable classes becomes as simple as switching which factories we use.

The relationship of the factory classes in the Functional Layer and the Traceable Layer are the same as the other classes in the two layers; i.e. there is a one to one correspondence and the Traceable Layer factories are children of the Functional Layer factories. Once again, this allows a Traceable factory to passed into any location that accepts the corresponding Functional factory.

Traceable factories differ from the Functional factories in two ways: 1) they create Traceable Layer objects, and 2) they hold the Tracer object that will be passed into each of the Traceable Layer objects it creates.

While the first difference is not surprising, the second one is very important to remember. Since the Tracer is attached to the factory, this means the factory is maintaining an internal state. We cannot treat the Traceable factory as a singleton, since the state is directly related to the specific instantiation of the service call we are tracing.

For a working example of this: see SortingV2's System Service. The TraceModes section shows how to easily switch between no tracing and four different trace modes.

In most cases, the factory simply calls the constructor of the class. If there are constructors with parameters, then additional Construct() methods may be added to handle those situations as well.

Example from SortingV2 SimpleSortFactory

using Sorting.Algorithms;

namespace Sorting.Factories
{
    public class SimpleSortFactory
    {
        public virtual SimpleSort Construct()
        {
            return new SimpleSort();
        }
    }
}

The corresponding Traceable factory will inherit and override the methods. When the Traceable Factory is used, it generates a Traceable version of the functional class.

Example from SortingV2 TraceSimpleSortFactory

using Sorting.Algorithms;
using Sorting.Factories;
using Traceability;
using TraceSorting.Algorithms;

namespace TraceSorting.Factories
{
    public class TraceSimpleSortFactory : SimpleSortFactory
    {
        private readonly ITracer _tracer;

        public TraceSimpleSortFactory(ITracer tracer)
        {
            _tracer = tracer;
        }

        public override SimpleSort Construct()
        {
            return new TraceSimpleSort(_tracer);
        }
    }
}

Updated