简体   繁体   English

自动接线特性注入

[英]Auto-wiring property injection

I am trying to use property injection for StructureMap in a plug-in style system. 我正在尝试在插件样式系统中为StructureMap使用属性注入。

ObjectFactory.Initialize(o => o.Scan(s =>
{
    s.AssembliesFromPath("path_to_assemblies");
    s.WithDefaultConventions();
});

Here is a sample implementation (purposely did not go deeper in defining T and TU as they are just simple classes) 这是一个示例实现(故意简化了T和TU的定义,因为它们只是简单的类)

public interface IBarService
{
    void Baz();
}

public class BarService : IBarService
{
    public void Baz() { /* implementation */ }
}


public abstract class PluginBase<T, TU> : IPlugin
    where T : AClass, new()
    where TU : BClass
{
     protected PluginBase()
     {
         //some init code in the constructor
     }
}

//Foo is created at run-time via Activator.CreateInstance()
public class FooPlugin : PluginBase<AClass, BClass>
{
   [SetterProperty]
   public IBarService BarService { get; set; }

   public void Fooey()
   {
       BarService.Baz();
   }
}

I want to auto-wire the property dependencies on all of these plug-ins that are created at run-time. 我想自动关联在运行时创建的所有这些插件的属性依赖关系。 I thought I'd start out with property injection and then move things to the constructor if the need arose. 我以为我将从属性注入开始,然后在需要时将其移至构造函数。

If I do: 如果我做:

var obj = ObjectFactory.GetInstance<IBarService>();

anywhere in an instance of FooPlugin , I get the correct implementation and all is well. FooPlugin实例中的任何地方,我都能获得正确的实现,一切都很好。

If I do: 如果我做:

this.BarService.Baz();

I get a null reference exception because no instance of IBarService was set. 我得到一个空引用异常,因为未设置IBarService实例。

After I create my plug-in, if I do this: 创建插件后,如果执行以下操作:

ObjectFactory.BuildUp(pluginObject) . ObjectFactory.BuildUp(pluginObject)

All is well and FooPlugin has the correct implementation of IBarService . 一切都很好, FooPlugin具有的正确实施IBarService

Is there any way to simply allow me to decorate my plugin properties with [SetterProperty] and have StructureMap automatically inject those when the Plugin is created without having to call ObjectFactory.BuildUp(pluginObject) ? 有什么方法可以让我简单地用[SetterProperty]装饰我的插件属性,并在创建插件时让StructureMap自动注入那些插件属性,而不必调用ObjectFactory.BuildUp(pluginObject)

How are you creating your plugin instance in the cases where it doesn't work? 在无效的情况下,如何创建插件实例? Are you just doing new FooPlugin() ? 您只是在做new FooPlugin()吗?

If you do var plugin = ObjectFactory.GetInstance<PluginBase<AClass, BClass>>(); 如果您执行var plugin = ObjectFactory.GetInstance<PluginBase<AClass, BClass>>(); , do you get the plugin with all the correct dependencies resolved? ,您是否已解决所有正确的依赖关系的插件?

If you are instantiating the object with "new", the container has no way to know that the instance needs dependencies resolved--it has no knowledge of that instance. 如果使用“ new”实例化对象,则容器将无法知道实例需要解决依赖关系-它不知道该实例。 Instances that need injected dependencies must be "managed by the container" so to speak. 可以说,需要注入依赖项的实例必须由容器“管理”。

You'll save yourself a lot of trouble if you just go the opposite route and inject all dependencies in the contstructor(s). 如果您只走相反的路线,并将所有依赖项注入到构造函数中,将为您省去很多麻烦。 This way there is a single place where you need to check if your objects are initialized properly... 这样,您可以在一个地方检查对象是否正确初始化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM