Removed ConcreteType property from IAbstractFactory<> interface (it was a really strange decision to add it in the first place, luckily it hasn't crept anywhere so far); provided a non-generic abstract factory interface; the factories returned by the factory employer now implement the non-generic factory interface

git-svn-id: file:///srv/devel/repo-conversion/nusu@139 d2e56fa2-650e-0410-a79f-9358c0239efd
This commit is contained in:
Markus Ewald 2009-05-27 19:51:54 +00:00
parent 41dcfa34d0
commit 4e2beb71b8
3 changed files with 46 additions and 17 deletions

View File

@ -69,6 +69,23 @@ namespace Nuclex.Support.Plugins {
Assert.IsFalse(testEmployer.CanEmploy(typeof(Unrelated)));
}
/// <summary>
/// Tests whether the factory employer can use the non-generic IAbstractFactory
/// interface instead of its generic variant
/// </summary>
[Test]
public void TestNonGenericCreateInstance() {
FactoryEmployer<Base> testEmployer = new FactoryEmployer<Base>();
testEmployer.Employ(typeof(Derived));
Assert.That(testEmployer.Factories.Count, Is.AtLeast(1));
IAbstractFactory factory = testEmployer.Factories[0] as IAbstractFactory;
Assert.IsNotNull(factory);
Assert.IsInstanceOf<Derived>(factory.CreateInstance());
}
/// <summary>
/// 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<Derived>(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<Unrelated>(testEmployer.Factories[0].CreateInstance());
}

View File

@ -41,31 +41,33 @@ namespace Nuclex.Support.Plugins {
/// a human-readable name, capabilities or an icon.
/// </para>
/// </remarks>
public class FactoryEmployer<ProductType> : Employer
where ProductType : class {
public class FactoryEmployer<ProductType> : Employer where ProductType : class {
#region class ConcreteFactory
/// <summary>Concrete factory for the types in a plugin assembly</summary>
private class ConcreteFactory : IAbstractFactory<ProductType> {
private class ConcreteFactory : IAbstractFactory<ProductType>, IAbstractFactory {
/// <summary>Initializes a factory and configures it for the specified product</summary>
/// <summary>
/// Initializes a factory and configures it for the specified product
/// </summary>
/// <param name="type">Type of which the factory creates instances</param>
public ConcreteFactory(Type type) {
this.concreteType = type;
}
/// <summary>The concrete type as produced by the factory</summary>
public Type ConcreteType {
get { return this.concreteType; }
}
/// <summary>Create a new instance of the type that the factory is configured to</summary>
/// <summary>Create a new instance of the type the factory is configured to</summary>
/// <returns>The newly created instance</returns>
public ProductType CreateInstance() {
return (ProductType)Activator.CreateInstance(this.concreteType);
}
/// <summary>Create a new instance of the type the factory is configured to</summary>
/// <returns>The newly created instance</returns>
object IAbstractFactory.CreateInstance() {
return Activator.CreateInstance(this.concreteType);
}
/// <summary>Concrete product which the factory instance creates</summary>
private Type concreteType;

View File

@ -24,13 +24,25 @@ using System.Collections.Generic;
namespace Nuclex.Support.Plugins {
/// <summary>Abstract factory for a concrete type</summary>
/// <typeparam name="ProductType">Interface or base class of the product of the factory</typeparam>
public interface IAbstractFactory {
/// <summary>
/// Creates a new instance of the type to which the factory is specialized
/// </summary>
/// <returns>The newly created instance</returns>
object CreateInstance();
}
/// <summary>Abstract factory for a concrete type</summary>
/// <typeparam name="ProductType">
/// Interface or base class of the product of the factory
/// </typeparam>
public interface IAbstractFactory<ProductType> {
/// <summary>The concrete type as implemented by the factory instance</summary>
Type ConcreteType { get; }
/// <summary>Creates a new instance of the type to which the factory is specialized</summary>
/// <summary>
/// Creates a new instance of the type to which the factory is specialized
/// </summary>
/// <returns>The newly created instance</returns>
ProductType CreateInstance();