0

I'm putting a project together with Spring.NET and Caliburn v2. I have some objects that I'm trying to instantiate, but not sure how to go about it.

I have been using Caliburn's IoC aspect annotations (Singleton and PerRequest) to get objects into the Spring context. The problem with this is that I have two objects, A and B, where Object B is a subclass of Object A (meaning B is also an A). This means that if I register both, Spring complains of ambiguity when an object of type A is requested. To get around this, I could stop using Caliburn's IoC aspects to register the objects and instead register them in the Spring context XML files. That way I can specify a named object in the Spring context file to use in the constructor of object C, which needs an object of type B injected.

However, this creates a new problem. Object B needs the Caliburn window manager to be injected (which is not available to the Spring container at the time when the objects listed in the context XML files are instantiated, but only later, after Caliburn has loaded and added its own objects to the Spring container).

I could simply remove the inheritance and let some code duplication occur between objects A and B, but then what would be the point of doing OO programming? Otherwise I guess I'm looking for a way to specify objects in Spring.NET context XML, but keep them from being resolved until after Caliburn has loaded.

Any ideas?

4

2 に答える 2

1

私はCaliburnに精通していませんが、インスタンス化を遅らせたい場合は、次のようにxml内のオブジェクトをlazy-initとしてマークできます。<object id="foo" type="..." lazy-init="true"/>

このようにして、最初にリクエストしたときにインスタンス化されます

于 2011-04-11T21:23:56.140 に答える
0

caliburnに依存するSpringコンテキストXMLファイルの個別のリストを維持することで、これを解決することができました。アプリケーションのブートストラッパーのオーバーライドされたDisplayRootView()メソッドの先頭に次のコードを追加して、これらをApplicationContextオブジェクトにロードしました。

var objectDefinitionReader = new XmlObjectDefinitionReader(applicationContext);
objectDefinitionReader.LoadObjectDefinitions(GetCaliburnDependentContextFiles());
applicationContext.Refresh();
于 2011-04-11T23:56:45.357 に答える