我一直在玩这个问题一段时间,我得出这样的结论,即代码从一开始就结构很差。如果您在其中有基本级别的实现BaseClass
,则应该通过使用IBaseClass
. 这样,每件事都有一个标准化的解决方法。
在您的示例中,您.Title
拥有specialItem
. 我认为这对于您的所有 MEF 导出类都很常见。因此,这应该在IBaseClass
. 然后,您可以使用合同名称或通过向导出添加ExportMetaData
属性并使用延迟加载来分离您的合同。
IBase类:
/// <summary>
/// Provides mechanisms for working with BaseClasses.
/// </summary>
public interface IBaseClass
{
/// <summary>
/// Gets the title of the MEF Exported class.
/// </summary>
/// <value>
/// The title.
/// </value>
string Title { get; }
}
基类:
/// <summary>
/// Acts as a base implementation for all derived classes.
/// </summary>
public abstract class BaseClass : IBaseClass {
/// <summary>
/// Initialises a new instance of the <see cref="BaseClass"/> class.
/// </summary>
/// <param name="title">The title.</param>
protected BaseClass(string title)
{
// Set the title.
Title = title;
}
#region Implementation of IBaseClass
/// <summary>
/// Gets the title of the MEF Exported class.
/// </summary>
/// <value>
/// The title.
/// </value>
public string Title { get; private set; }
#endregion
}
然后从那里派生你的类......
派生类:
[Export("DerivedClasses", typeof(BaseClass))]
public class DerivedClassA : BaseClass
{
public DerivedClassA()
: this("DerivedClassA")
{
/* IoC Friendly Constructor */
}
/// <summary>
/// Initialises a new instance of the <see cref="BaseClass"/> class.
/// </summary>
/// <param name="title">The title.</param>
public DerivedClassA(string title) : base(title)
{
/* Construction Logic */
}
}
请注意,我已在导出中添加了合同名称。您的特殊类扩展了您的功能BaseClass
,并且该功能应该被划分到另一个接口中。
IDerivedSpecialBaseClass:
/// <summary>
/// Provides mechanisms for working with special BaseClasses.
/// </summary>
public interface IDerivedSpecialBaseClass : IBaseClass
{
string SpecialProperty { get; }
}
此接口继承自,IBaseClass
因为它派生自同一个根。您的具体实现与上面相同,但实现IDerivedSpecialBaseClass
而不仅仅是IBaseClass
.
DerivedSpecialBaseClass:
/// <summary>
/// Acts as a base implementation for all derived special classes.
/// </summary>
public abstract class DerivedSpecialBaseClass : BaseClass
{
protected DerivedSpecialBaseClass()
: this("DerivedSpecialBaseClass")
{
/* IoC Friendly Constructor */
}
/// <summary>
/// Initialises a new instance of the <see cref="BaseClass"/> class.
/// </summary>
/// <param name="title">The title.</param>
protected DerivedSpecialBaseClass(string title) : base(title)
{
/* Constructor Logic */
}
#region Implementation of IDerivedSpecialBaseClass
/// <summary>
/// Gets the special property.
/// </summary>
/// <value>
/// The special property.
/// </value>
public abstract string SpecialProperty { get; }
#endregion
}
具体实现:
[Export("DerivedSpecialClasses", typeof(DerivedSpecialBaseClass))]
public class DerivedSpecialA : DerivedSpecialBaseClass
{
/// <summary>
/// Initialises a new instance of the <see cref="DerivedSpecialA"/> class.
/// </summary>
public DerivedSpecialA()
: this("DerivedSpecialA")
{
/* IoC Friendly Constructor */
}
/// <summary>
/// Initialises a new instance of the <see cref="DerivedSpecialA"/> class.
/// </summary>
/// <param name="title">The title.</param>
public DerivedSpecialA(string title)
: base(title)
{
/* Construction Logic */
}
#region Implementation of IDerivedSpecialBaseClass
/// <summary>
/// Gets the special property.
/// </summary>
/// <value>
/// The special property.
/// </value>
public override string SpecialProperty
{
get { return @"Hello, from DerivedSpecialA"; }
}
#endregion
}
注意使用的不同合约名称,以及导出的不同类型。如果你必须使用泛型,你可以以同样的方式添加第三个接口。
界面:
/// <summary>
/// Provides mechanisms for doing stuff with things.
/// </summary>
/// <typeparam name="T">The type of things to do stuff with.</typeparam>
public interface IDerivedSpecialBaseClass<T> : IDerivedSpecialBaseClass
where T : struct
{
/// <summary>
/// Does stuff with things.
/// </summary>
/// <param name="thing">The thing.</param>
void DoStuffWith(T thing);
}
原始
/// <summary>
/// Acts as a base class for all generic special classes.
/// </summary>
/// <typeparam name="T"></typeparam>
public abstract class DerivedGenericBaseClass<T> : DerivedSpecialBaseClass, IDerivedSpecialBaseClass<T>
where T : struct
{
#region Implementation of IDerivedGenericBaseClass<T>
/// <summary>
/// Does stuff with things.
/// </summary>
/// <param name="thing">The thing.</param>
public abstract void DoStuffWith(T thing);
#endregion
}
具体的:
[Export("DerivedSpecialGenericClasses", typeof(DerivedGenericBaseClass<>))]
public class DerivedSpecialGenericA<T> : DerivedGenericBaseClass<T>
where T : struct
{
#region Overrides of DerivedSpecialBaseClass
/// <summary>
/// Does stuff and things.
/// </summary>
/// <param name="thing">The thing.</param>
public override void DoStuffWith(T thing)
{
Console.WriteLine("Doing Stuff and Things with " + thing.GetType().Name);
}
/// <summary>
/// Gets the special property.
/// </summary>
/// <value>
/// The special property.
/// </value>
public override string SpecialProperty
{
get { return @"Hello, from DerivedSpecialGenericA"; }
}
#endregion
}
现在,在 MEF 中处理这些的最简单方法是创建一个 Adapter 类来处理组合。
适配器类
/// <summary>
/// Adapter class for MEF Imported contracts, deriving from BaseClass.
/// </summary>
public class Classes
{
/// <summary>
/// Gets or sets the derived classes.
/// </summary>
/// <value>
/// The derived classes.
/// </value>
/// <remarks>
/// This list will be populated via MEF.
/// </remarks>
[ImportMany("DerivedClasses", typeof(BaseClass))]
private IEnumerable<IBaseClass> DerivedClasses { get; set; }
/// <summary>
/// Gets or sets the derived special classes.
/// </summary>
/// <value>
/// The derived special classes.
/// </value>
/// <remarks>
/// This list will be populated via MEF.
/// </remarks>
[ImportMany("DerivedSpecialClasses", typeof(DerivedSpecialBaseClass))]
private IEnumerable<IDerivedSpecialBaseClass> DerivedSpecialClasses { get; set; }
/// <summary>
/// Gets or sets the derived special generic classes.
/// </summary>
/// <value>
/// The derived special generic classes.
/// </value>
/// <remarks>
/// This list will be populated via MEF.
/// </remarks>
[ImportMany("DerivedSpecialGenericClasses", typeof(DerivedGenericBaseClass<>))]
private IEnumerable<IDerivedSpecialBaseClass> DerivedSpecialGenericClasses { get; set; }
/// <summary>
/// Initialises a new instance of the <see cref="Classes"/> class.
/// </summary>
public Classes()
{
// NOTE: This is a generic application catalogue, for demonstration purposes.
// It is likely you'd rather use a Directory or Assembly catalogue
// instead, to make the application more scalable, in line with the MEF
// ideology.
var catalogue = new ApplicationCatalog();
var container = new CompositionContainer(catalogue);
container.ComposeParts(this);
}
/// <summary>
/// Processes the classes.
/// </summary>
public void ProcessClasses()
{
foreach (var item in DerivedClasses)
{
DoSomethingInteresting(item);
}
foreach (var item in DerivedSpecialClasses)
{
DoSomethingInteresting(item);
}
foreach (var item in DerivedSpecialGenericClasses)
{
DoSomethingInteresting(item);
}
}
private void DoSomethingInteresting(IBaseClass specialItem)
{
Console.WriteLine("Something interesting has been done with: " + specialItem.Title);
}
}
请注意,您不再需要检查哪种类型是哪种类型,因为它们已被分成三个单独的列表,使用 MEF 合同名称将它们过滤到正确的列表中。您不应该在适配器中引用具体的实现,因为您在运行时不知道它们是否存在。如果他们不这样做,您的应用程序就会失败。
相反,我建议ShouldProcess
在接口中添加一个标志IBaseClass
,并且只处理那些设置为 true 的标志。
foreach (var item in DerivedSpecialGenericClasses.Where(p => p.ShouldProcess == true))
{
DoSomethingInteresting(item);
}
类似的东西。考虑分离你的担忧。MEF 不知道什么流经它,所以您的应用程序也不知道。让它成为焦点,让插件告诉你的应用程序他们需要什么。在可能的最低级别实施这些检查,以便可以批量处理它们。
如果您不完全了解程序流程、设计模式和可扩展架构,使用 MEF 可能会很复杂。例如,这可以通过延迟加载来实现,将处理检查添加为ExportMetadata
. 它不会增加太多开销,但可能会更复杂,如果您没有一口气完全吞下您的插件。当从其他可扩展性较低的组合引擎移植到 MEF 时,通常需要从头开始重建插件结构;为了使它流畅,优雅。