2

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

任何帮助是极大的赞赏。

我在互联网上遍历了很多路径,以确定如何使用 Unity 进行属性注入,但我最终还是得到了一个实例化的对象,该对象带有一个应该被注入的空属性。所以它死了。

有几个问题:

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
...  
...  

}

我确实在几个位置看到似乎需要使用 [Dependency]
标记属性 ,而在其他地方表明这些标记将被代码中的InjectionProperty对象覆盖。讨论模棱两可。

我非常担心 Unity = DisUnity ,我什至可能会尝试使用它。

4

0 回答 0