Click or drag to resize
InterfaceFactory Namespace
The namespace for the class factory.
Classes
  ClassDescription
Public classClassFactoryException
The exception raised by class factories.
Public classFactory
A static class that exposes an instance of an IClassFactory that the application can use to register implementations of interfaces and instantiate those implementations later on.
Interfaces
  InterfaceDescription
Public interfaceIClassFactory
An interface for objects that implement class factory functionality.
Remarks

Virtual Radar Server has been written on the principle that the classes that make up the source are loosely coupled - instead of having direct references to each other they have references to interfaces and they do not know anything about the classes that implement those interfaces.

This library contains a singleton class factory that the executable and all of the libraries have access to. The class factory can be told which classes implement which interfaces, and it can also create instances of those classes for the application to use.

The singleton class factory is held by the public static class Factory. The property Singleton exposes the singleton IClassFactory class factory.

Virtual Radar Server originally used Microsoft's Unity library to register implementations of interfaces. Unity was replaced with this library after there was an issue with a particular object. This issue no longer exists so one day the implementation of IClassFactory may be changed to use Unity again. This should not affect any code that is currently using the class factory.

Examples

This declares an interface and a class that implements it. It shows how the implementation can be registered with the singleton class factory and how to request a new instance of the class that implements the interface.

Normally the interface would be in one library while the implementation (and the registration of the implementation) would be in another. The code that requests a new instance of the implementation could be anywhere within the application.

// The public interface that the application will use.
public interface IMyInterface
{
    void DoSomeWork();
}

// The private class that implements the interface. Nothing that uses this
// class will know anything about it other than it implements IMyInterface.
class PrivateClass : IMyInterface
{
    public void DoSomeWork() { MessageBox.Show("Doing some work"); }
}

// The call that will tell the singleton class factory that new instances
// of PrivateClass should be created when it is asked for instantiations of
// IMyInterface:
Factory.Singleton.Register<IMyInterface, PrivateClass>();

// The call that can be made anywhere within the application to get an
// implementation of IMyInterface. In this case the singleton class factory
// will create a new instance of PrivateClass and return it:
IMyInterface instance = Factory.Singleton.Resolve<IMyInterface>();
instance.DoSomeWork();