VirtualRadar.InterfaceFactory 3.0.0-beta01

Lightweight class factory

This is a prerelease version of VirtualRadar.InterfaceFactory.
Install-Package VirtualRadar.InterfaceFactory -Version 3.0.0-beta01
dotnet add package VirtualRadar.InterfaceFactory --version 3.0.0-beta01
<PackageReference Include="VirtualRadar.InterfaceFactory" Version="3.0.0-beta01" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add VirtualRadar.InterfaceFactory --version 3.0.0-beta01
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

InterfaceFactory

The interface factory is responsible for creating instances of interfaces.

ClassFactory

ClassFactory manages the registration of classes that implement interfaces.
Each library usually exposes a static class called Implementations that has a
method called Register. The program calls these Register methods early on in
its run. Each Register method is passed a ClassFactory to populate with
implementations, usually Factory.Singleton, and the Register method just
calls Register on the ClassFactory for each of the interfaces that it implements.

Implementations can be changed while the program is running. In VRS it is common
for plugins to override the default implementation of one or two interfaces with
their own implementations.

When the program wants to instantiate an interface it calls one of the Resolve
methods on ClassFactory.

The ClassFactory guarantees that it will always return the same instance for
interfaces that are tagged with the Singleton attribute. It will also do this
if an instance is registered with ClassFactory using RegisterInstance.

Any of the Resolve calls (except ResolveNewInstance) will honour the Singleton
attribute, but for the sake of self-documenting code it's preferrable to call
ResolveSingleton when resolving singleton interfaces. This method is just the
same as Resolve, the only difference is that it throws an exception if the interface
isn't marked as a Singleton.

Factory

Factory is a static class that exposes a property called Singleton, which is
the instance of ClassFactory that the application is expected to use.

Starting with version 3 Factory exposes all of the ClassFactory methods as statics
that pass the call through to Singleton - so instead of calling

Factory.Singleton.Resolve<IInterface>()

you can call

Factory.Resolve<IInterface>()

instead.

There are two methods, TakeSnapshot and RestoreSnapshot, that let you
copy a class factory and restore it respectively. These are only intended for use
in unit tests, they should not be called in production code.

Examples

The interface:

public interface IMyInterface
{
    string GenerateName();
}

The implementation:

// Note that the implementation is usually private to the library
class MyImplementation : IMyInterface
{
    public string GenerateName()
    {
        return "A Name";
    }
}

Registering the implementation - this need only be done once:

Factory.Singleton.Register<IMyInterface, MyImplementation>();

(or the new 3.0 shortcut):

Factory.Register<IMyInterface, MyImplementation>();

Creating a new instance of an implementation:

IMyInterface instance = Factory.Singleton.Resolve<IMyInterface>();
Console.WriteLine(instance.GenerateName());

(or the new 3.0 shortcut):

IMyInterface instance = Factory.Resolve<IMyInterface>();
Console.WriteLine(instance.GenerateName());

InterfaceFactory

The interface factory is responsible for creating instances of interfaces.

ClassFactory

ClassFactory manages the registration of classes that implement interfaces.
Each library usually exposes a static class called Implementations that has a
method called Register. The program calls these Register methods early on in
its run. Each Register method is passed a ClassFactory to populate with
implementations, usually Factory.Singleton, and the Register method just
calls Register on the ClassFactory for each of the interfaces that it implements.

Implementations can be changed while the program is running. In VRS it is common
for plugins to override the default implementation of one or two interfaces with
their own implementations.

When the program wants to instantiate an interface it calls one of the Resolve
methods on ClassFactory.

The ClassFactory guarantees that it will always return the same instance for
interfaces that are tagged with the Singleton attribute. It will also do this
if an instance is registered with ClassFactory using RegisterInstance.

Any of the Resolve calls (except ResolveNewInstance) will honour the Singleton
attribute, but for the sake of self-documenting code it's preferrable to call
ResolveSingleton when resolving singleton interfaces. This method is just the
same as Resolve, the only difference is that it throws an exception if the interface
isn't marked as a Singleton.

Factory

Factory is a static class that exposes a property called Singleton, which is
the instance of ClassFactory that the application is expected to use.

Starting with version 3 Factory exposes all of the ClassFactory methods as statics
that pass the call through to Singleton - so instead of calling

Factory.Singleton.Resolve<IInterface>()

you can call

Factory.Resolve<IInterface>()

instead.

There are two methods, TakeSnapshot and RestoreSnapshot, that let you
copy a class factory and restore it respectively. These are only intended for use
in unit tests, they should not be called in production code.

Examples

The interface:

public interface IMyInterface
{
    string GenerateName();
}

The implementation:

// Note that the implementation is usually private to the library
class MyImplementation : IMyInterface
{
    public string GenerateName()
    {
        return "A Name";
    }
}

Registering the implementation - this need only be done once:

Factory.Singleton.Register<IMyInterface, MyImplementation>();

(or the new 3.0 shortcut):

Factory.Register<IMyInterface, MyImplementation>();

Creating a new instance of an implementation:

IMyInterface instance = Factory.Singleton.Resolve<IMyInterface>();
Console.WriteLine(instance.GenerateName());

(or the new 3.0 shortcut):

IMyInterface instance = Factory.Resolve<IMyInterface>();
Console.WriteLine(instance.GenerateName());

Release Notes

Initial beta release.

Dependencies

This package has no dependencies.

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
3.0.0-beta01 89 2/16/2019
2.4.2 101 2/16/2019