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 more details.)

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. Most classes may use the Simple Factory discussed below, or the Initializing Factory discussed later. A class that contains other classes is a good candidate for the Meta-Factory.

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.

Simple Case Factories

In the most simple case, the factory will pass through constructor arguments to the class being instantiated. Note: due to the simplicity of this factory, it could be effectively treated as a singleton. However, you do not want to treat it as a true singleton, since that would cause problems in Traceable version. namespace FunctionalLayer.Factories { public class SimpleClassFactory { public virtual SimpleClass Construct() { return new SimpleClass(); } public virtual SimpleClass ConstructFromDataStream(DataStream stream) { return new SimpleClass(stream); } } }

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. namespace TraceableLayer.Factories { public class SimpleClassFactory : FunctionalLayer.Factories.SimpleClassFactory { private Tracer _tracer; public SimpleClassFactory(Tracer tracer) { _tracer = tracer; } public override SimpleClass Construct() { return new TraceableLayer.Entities.SimpleClass(_tracer); } public override SimpleClass ConstructFromDataStream(DataStream stream) { return new TraceableLayer.Entities.SimpleClass(stream, _tracer); } } }

Next: Factories, Initializing