2008-01-07 18:04:02 +00:00
|
|
|
|
#region CPL License
|
|
|
|
|
/*
|
|
|
|
|
Nuclex Framework
|
2012-02-29 16:27:43 +00:00
|
|
|
|
Copyright (C) 2002-2012 Nuclex Development Labs
|
2008-01-07 18:04:02 +00:00
|
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
|
modify it under the terms of the IBM Common Public License as
|
|
|
|
|
published by the IBM Corporation; either version 1.0 of the
|
|
|
|
|
License, or (at your option) any later version.
|
|
|
|
|
|
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
IBM Common Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the IBM Common Public
|
|
|
|
|
License along with this library
|
|
|
|
|
*/
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
|
|
|
|
namespace Nuclex.Support.Plugins {
|
|
|
|
|
|
|
|
|
|
/// <summary>Employer to create factories of suiting types found in plugins</summary>
|
2012-03-08 11:05:20 +00:00
|
|
|
|
/// <typeparam name="TProduct">
|
2008-11-26 19:15:36 +00:00
|
|
|
|
/// Interface or base class that the types need to implement
|
|
|
|
|
/// </typeparam>
|
2008-01-07 18:04:02 +00:00
|
|
|
|
/// <remarks>
|
|
|
|
|
/// <para>
|
|
|
|
|
/// This employer will not directly instanciate any compatible types found in
|
|
|
|
|
/// plugin assemblies, but generated runtime-factories of these types, enabling the
|
|
|
|
|
/// user to decide when and how many instances of a type will be created.
|
|
|
|
|
/// </para>
|
|
|
|
|
/// <para>
|
|
|
|
|
/// This approach has the advantage that it enables even assemblies that were not
|
|
|
|
|
/// intended to be plugins can be loaded as plugins, without risking an instanciation
|
|
|
|
|
/// or complex and possibly heavy-weight types. The disadvantage is that the
|
|
|
|
|
/// runtime-factory can not provide decent informationa about the plugin type like
|
|
|
|
|
/// a human-readable name, capabilities or an icon.
|
|
|
|
|
/// </para>
|
|
|
|
|
/// </remarks>
|
2012-03-08 11:05:20 +00:00
|
|
|
|
public class FactoryEmployer<TProduct> : Employer where TProduct : class {
|
2008-01-07 18:04:02 +00:00
|
|
|
|
|
2008-11-26 19:15:36 +00:00
|
|
|
|
#region class ConcreteFactory
|
2008-01-07 18:04:02 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>Concrete factory for the types in a plugin assembly</summary>
|
2012-03-08 11:05:20 +00:00
|
|
|
|
private class ConcreteFactory : IAbstractFactory<TProduct>, IAbstractFactory {
|
2008-01-07 18:04:02 +00:00
|
|
|
|
|
2009-05-27 19:51:54 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Initializes a factory and configures it for the specified product
|
|
|
|
|
/// </summary>
|
2008-01-07 18:04:02 +00:00
|
|
|
|
/// <param name="type">Type of which the factory creates instances</param>
|
2008-11-26 19:15:36 +00:00
|
|
|
|
public ConcreteFactory(Type type) {
|
2008-01-07 18:04:02 +00:00
|
|
|
|
this.concreteType = type;
|
|
|
|
|
}
|
|
|
|
|
|
2009-05-27 19:51:54 +00:00
|
|
|
|
/// <summary>Create a new instance of the type the factory is configured to</summary>
|
2008-01-07 18:04:02 +00:00
|
|
|
|
/// <returns>The newly created instance</returns>
|
2012-03-08 11:05:20 +00:00
|
|
|
|
public TProduct CreateInstance() {
|
|
|
|
|
return (TProduct)Activator.CreateInstance(this.concreteType);
|
2008-01-07 18:04:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
2009-05-27 19:51:54 +00:00
|
|
|
|
/// <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);
|
|
|
|
|
}
|
|
|
|
|
|
2008-01-07 18:04:02 +00:00
|
|
|
|
/// <summary>Concrete product which the factory instance creates</summary>
|
|
|
|
|
private Type concreteType;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion // class Factory
|
|
|
|
|
|
|
|
|
|
/// <summary>Initializes a new FactoryEmployer</summary>
|
|
|
|
|
public FactoryEmployer() {
|
2012-03-08 11:05:20 +00:00
|
|
|
|
this.employedFactories = new List<IAbstractFactory<TProduct>>();
|
2008-01-07 18:04:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>List of all factories that the instance employer has created</summary>
|
2012-03-08 11:05:20 +00:00
|
|
|
|
public List<IAbstractFactory<TProduct>> Factories {
|
2008-01-07 18:04:02 +00:00
|
|
|
|
get { return this.employedFactories; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Determines whether the type suites the employer's requirements</summary>
|
|
|
|
|
/// <param name="type">Type which will be assessed</param>
|
|
|
|
|
/// <returns>True if the type can be employed</returns>
|
|
|
|
|
public override bool CanEmploy(Type type) {
|
|
|
|
|
return
|
2012-03-08 11:05:20 +00:00
|
|
|
|
type.HasDefaultConstructor() &&
|
|
|
|
|
typeof(TProduct).IsAssignableFrom(type) &&
|
2009-09-01 19:11:19 +00:00
|
|
|
|
!type.ContainsGenericParameters;
|
2008-01-07 18:04:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>Employs the specified plugin type</summary>
|
|
|
|
|
/// <param name="type">Type to be employed</param>
|
|
|
|
|
public override void Employ(Type type) {
|
2012-03-08 11:05:20 +00:00
|
|
|
|
if(!type.HasDefaultConstructor()) {
|
2008-11-28 19:34:43 +00:00
|
|
|
|
throw new MissingMethodException(
|
|
|
|
|
"Cannot employ type because it does not have a public default constructor"
|
|
|
|
|
);
|
|
|
|
|
}
|
2012-03-08 11:05:20 +00:00
|
|
|
|
if(!typeof(TProduct).IsAssignableFrom(type)) {
|
2008-11-28 19:34:43 +00:00
|
|
|
|
throw new InvalidCastException(
|
|
|
|
|
"Cannot employ type because it cannot be cast to the factory's product type"
|
|
|
|
|
);
|
|
|
|
|
}
|
2009-09-01 19:11:19 +00:00
|
|
|
|
if(type.ContainsGenericParameters) {
|
|
|
|
|
throw new ArgumentException(
|
|
|
|
|
"Cannot employ type because it requires generic parameters", "type"
|
|
|
|
|
);
|
|
|
|
|
}
|
2008-11-28 19:34:43 +00:00
|
|
|
|
|
2008-11-26 19:15:36 +00:00
|
|
|
|
this.employedFactories.Add(new ConcreteFactory(type));
|
2008-01-07 18:04:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>All factories that the instance employer has created</summary>
|
2012-03-08 11:05:20 +00:00
|
|
|
|
private List<IAbstractFactory<TProduct>> employedFactories;
|
2008-01-07 18:04:02 +00:00
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace Nuclex.Support.Plugins
|