Using Factories will greatly reduce the amount of maintenance necessary to maintain the Logging 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 Logging Layer class by simply swapping the respective factories. (See Factory Pattern for more details.)

The first step when considering designing a factory for a class is to ask if we actually need a factory. If the class has no logging needs, then we will not need a factory for it. An example for this situation is if the class only contains primitive data fields. If the class does have logging, then it does need a factory. In most cases the Simple Factory described below or the or the Initializing Factory will suffice. If we have a case where the class does not have logging in it, but contains members that do have logging, then we will need to use a Meta-Factory

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

Logging factories differ from the Functional factories in three ways: 1) they create Logging Layer objects, 2) they hold the Logging connection object that will be passed into each of the Logging Layer objects it creates, and 3) They hold the Context object that is passed into each of the Logging Layer objects.

While the first difference is not surprising, the latter two are very important to remember, since it means the factory is maintaining an internal state. We cannot treat the Logging factory as a singleton, since the state is directly related to the specific instantiation of service call we are logging.

Simple Case Factories

In the most simple case, the factory will pass through constructor arguments to the class being instantiated. 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 Logging factory will inherit and override the methods. When the Logging Factory is used, it generates a Logging version of the Functional class. namespace LoggingLayer.Factories { public class SimpleClassFactory : FunctionalLayer.Factories.SimpleClassFactory { private ILogger _logger; private LoggingContext _context; public SimpleClassFactory(ILogger logger, LoggingContext context) { _logger = logger; _context = context; } public override SimpleClass Construct() { return new LoggingLayer.Entities.SimpleClass(_context, _context); } public override SimpleClass ConstructFromDataStream(DataStream stream) { return new LoggingLayer.Entities.SimpleClass(stream, _context, _context); } } }

Next: Factories, Initializing

Copyright © 2017-2018 Adin H. Baber, all rights reserved.