ここでのさまざまな投稿は、Unity 2.0(4月)でInjectionProperty()コンストラクトを使用しようとするたびに、解決されたインスタンスのプロパティにデータが入力されない理由を示していません。それらは常にnullです。デバッガーでオブジェクトが作成されていることがわかりますが、挿入されるはずのプロパティへの最初の参照は常にnull参照例外です。私がしていることには、根本的に何かが間違っているに違いありません。
どんな助けでも大歓迎です。
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
...
...
}
[Dependency]でプロパティをマークする
必要があるように思われる場所や、コードでのInjectionPropertyオブジェクトの使用によってそれらのマークが上書きされると記載されている場所がいくつかあります。議論はあいまいです。
私はUnity=DisUnityを非常 に恐れており、それを使おうとしても自分自身を台無しにしたかもしれません。