diff --git a/Source/Plugins/FactoryEmployer.Test.cs b/Source/Plugins/FactoryEmployer.Test.cs index bd5a26f..beb5135 100644 --- a/Source/Plugins/FactoryEmployer.Test.cs +++ b/Source/Plugins/FactoryEmployer.Test.cs @@ -69,6 +69,23 @@ namespace Nuclex.Support.Plugins { Assert.IsFalse(testEmployer.CanEmploy(typeof(Unrelated))); } + /// + /// Tests whether the factory employer can use the non-generic IAbstractFactory + /// interface instead of its generic variant + /// + [Test] + public void TestNonGenericCreateInstance() { + FactoryEmployer testEmployer = new FactoryEmployer(); + testEmployer.Employ(typeof(Derived)); + + Assert.That(testEmployer.Factories.Count, Is.AtLeast(1)); + + IAbstractFactory factory = testEmployer.Factories[0] as IAbstractFactory; + Assert.IsNotNull(factory); + + Assert.IsInstanceOf(factory.CreateInstance()); + } + /// /// Tests whether the factory employer throws an exception when it is asked to /// employ an abstract class @@ -105,7 +122,6 @@ namespace Nuclex.Support.Plugins { testEmployer.Employ(typeof(Derived)); Assert.AreEqual(1, testEmployer.Factories.Count); - Assert.AreEqual(typeof(Derived), testEmployer.Factories[0].ConcreteType); Assert.IsInstanceOf(testEmployer.Factories[0].CreateInstance()); } @@ -120,7 +136,6 @@ namespace Nuclex.Support.Plugins { testEmployer.Employ(typeof(Unrelated)); Assert.AreEqual(1, testEmployer.Factories.Count); - Assert.AreEqual(typeof(Unrelated), testEmployer.Factories[0].ConcreteType); Assert.IsInstanceOf(testEmployer.Factories[0].CreateInstance()); } diff --git a/Source/Plugins/FactoryEmployer.cs b/Source/Plugins/FactoryEmployer.cs index 2f14f4f..c316330 100644 --- a/Source/Plugins/FactoryEmployer.cs +++ b/Source/Plugins/FactoryEmployer.cs @@ -41,31 +41,33 @@ namespace Nuclex.Support.Plugins { /// a human-readable name, capabilities or an icon. /// /// - public class FactoryEmployer : Employer - where ProductType : class { + public class FactoryEmployer : Employer where ProductType : class { #region class ConcreteFactory /// Concrete factory for the types in a plugin assembly - private class ConcreteFactory : IAbstractFactory { + private class ConcreteFactory : IAbstractFactory, IAbstractFactory { - /// Initializes a factory and configures it for the specified product + /// + /// Initializes a factory and configures it for the specified product + /// /// Type of which the factory creates instances public ConcreteFactory(Type type) { this.concreteType = type; } - /// The concrete type as produced by the factory - public Type ConcreteType { - get { return this.concreteType; } - } - - /// Create a new instance of the type that the factory is configured to + /// Create a new instance of the type the factory is configured to /// The newly created instance public ProductType CreateInstance() { return (ProductType)Activator.CreateInstance(this.concreteType); } + /// Create a new instance of the type the factory is configured to + /// The newly created instance + object IAbstractFactory.CreateInstance() { + return Activator.CreateInstance(this.concreteType); + } + /// Concrete product which the factory instance creates private Type concreteType; diff --git a/Source/Plugins/IAbstractFactory.cs b/Source/Plugins/IAbstractFactory.cs index a8680c0..7b9f933 100644 --- a/Source/Plugins/IAbstractFactory.cs +++ b/Source/Plugins/IAbstractFactory.cs @@ -24,13 +24,25 @@ using System.Collections.Generic; namespace Nuclex.Support.Plugins { /// Abstract factory for a concrete type - /// Interface or base class of the product of the factory + public interface IAbstractFactory { + + /// + /// Creates a new instance of the type to which the factory is specialized + /// + /// The newly created instance + object CreateInstance(); + + } + + /// Abstract factory for a concrete type + /// + /// Interface or base class of the product of the factory + /// public interface IAbstractFactory { - /// The concrete type as implemented by the factory instance - Type ConcreteType { get; } - - /// Creates a new instance of the type to which the factory is specialized + /// + /// Creates a new instance of the type to which the factory is specialized + /// /// The newly created instance ProductType CreateInstance();