Unity InjectionProperty生成null属性

时间:2011-06-09 16:58:35

标签: dependency-injection unity-container mvvm-light property-injection

这里的各个帖子没有说明为什么每次我尝试在Unity 2.0(4月)中使用InjectionProperty()构造时,它都不会在我已解析的实例中填充属性。它们总是空的。我可以在调试器中看到创建了对象,但是对应该注入的属性的第一个引用始终是空引用异常。在我正在做的事情中肯定存在一些非常根本的错误。

非常感谢任何帮助。

我已经遍历了Internet上的许多路径以确定如何使用Unity来执行Property Injection,我仍然最终得到一个具有null属性的实例化对象,该属性应该被注入。所以它死了。

有几个问题:

1)这些PropertyInjector对象在调试器中何处可见?没有多少采矿揭示了这些,所以我无法确定它们是否“准备注射”。

2)具有该属性的对象确实通过Resolve实例化,但它永远不会获得属性值(该属性是ILog对象)。

我在我的智慧结束,这无疑可能只是一根短绳,但这是怎么回事呢?任何见解。

以下是代码:

// (Unity 2.1, May something or other drop, so I think this is the latest )  

public class RuntimeFilesRepository:IFilesRepository
{
...
...
...

// A customized version of the standard Log4Net ILog

public ILog Logger {get;set;} 
...
...

 public RuntimeFilesRepository()
{
...
... 

// INJECTION NEEDS TO HAPPEN BEFOE THIS, BUT NEVER DOES, SO THIS IS A NULL OBJECT 

Logger.Debug("I do like my CaesarSalad with the extra chicken"); 

}


// and the registration looks like this:**

public void WireUp()
{

...
...

// a container

        ParentContainer = new UnityContainer();

// this thing is really helpful!!! 

//  [https://github.com/dbuksbaum/unity.extensions][1] 

        ParentContainer.AddNewExtension<**TypeTrackingExtension**>();

// and the Logger type

        ParentContainer.RegisterType<ILog, Log4NetLog>("Logger",
                                                 new InjectionFactory(
                                                     factory => LogManager.GetLogger("Visual Element Migrator")));

// and an instance of an ILog.  It can be referred to as 'LoggingService'
// to use as a resolved parameter to inject   


 ILog Logger = ParentContainer.Resolve<ILog>("Logger");
        ParentContainer.RegisterInstance("LoggingService", Logger, new LifeTimeManager()); 


...  
...  


//various Logger log statements from the resolved ILog object work here as we plod along, by the way  

...  
...  

// then the next statement works, type is registered, shows up in the debugger,    
// but where the heck are the injection properties???     

    DataServicesContainer.RegisterType<IFilesRepository,RuntimeFilesRepository>(new InjectionProperty("Logger", Logger));  

// I have tried 3 different variants of the above InjectionProperty() to no avail.  

// runtime files repo, want a singleton
// allow Unity to resolve the  RUN TIME files repositoryand hold onto reference

// DOES NOT WORK.  Apparently instantiates RuntimeFilesRepository,  but does not inject the ILog to the Logger property


            var filesRepo = DataServicesContainer.Resolve<RuntimeFilesRepository>();


// *never gets here where I try to register the object so it can be REUSED in other contexts....*

            DataServicesContainer.RegisterInstance<IFilesRepository>("FilesRepositoryDataService", filesRepo,  new LifeTimeManager()); // to inject, singleton

...  
...  
// lots more of the same sort of class register and instantiate stuff
...  
...  

}

我确实在几个标有财产的地方看到了  的 [依赖]
似乎是必需的,并且在代码中使用 InjectionProperty 对象将覆盖其他表示这些标记的地方。讨论含糊不清。

我非常感到Unity = DisUnity 而且我甚至可能在试图使用它时搞砸了自己。

0 个答案:

没有答案