Unity InjectionProperty 生成 null 属性

发布于 2024-11-14 14:19:59 字数 3199 浏览 1 评论 0原文

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

非常感谢任何帮助。

我在互联网上找了很多途径来确定如何使用 Unity 进行属性注入,但最终还是得到了一个应该注入的带有 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 即使我试图使用它,我也可能把自己搞砸了。

The various posts here do not show why every time I attempt to use the InjectionProperty() construct in Unity 2.0 (April) it never populates the properties in my resolved instance. They are always null. I can see in the debugger that the object is created, but the first reference to the property that is supposed to be injected is always a null reference exception. There must be something very fundamentally wrong in what I am doing.

Any help is greatly appreciated.

I have traversed many paths on the Internet to determine how to use Unity to do Property Injection, and I still end up with an instantiated object with a null property that is supposed to be injected. So it dies.

There are several issues:

1) Where are these PropertyInjector objects visible in the debugger? No amount of mining has revealed these, so I cannot determine if they are 'ready to inject'.

2) The object with the property does indeed instantiate via Resolve but it never gets a property value (the property is an ILog object).

I am at my wit's end, which admittedly might be a short piece of rope, but what the heck is going on with this? Any insights.

Here's the code:

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

}

I do see in several locations that marking a property with
[Dependency]
seems to be required, and other places where it says those markings will be overridden by the use of the InjectionProperty object in code. Discussion is ambiguous.

I am greatly afeared that Unity = DisUnity
and I may have screwed myself even trying to use it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文