如何在 Spring.net 中注入 Predicate 和 Func
我想使用 spring 在 xml 配置中创建一个带有构造函数的对象,其中包含谓词和 func 对象。 Predicate 和 Func 参数应该指向另一个配置对象的方法。使用 Spring.net 这怎么可能呢?我无法在文档中找到解决方案或提示...
示例构造函数是:
MyClass(Predicate<TInput> condition, Func<TInput, TOutput> result)
I want to create an object with a constructor containing predicate and func objects in the xml config using spring. The Predicate and the Func arguments should point to a method of another configured object. How is this possible using Spring.net? I was not able to find a solution or hint in the documentation...
A sample constructor would be:
MyClass(Predicate<TInput> condition, Func<TInput, TOutput> result)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
还可以使用 Spring.net 中的 DelegateFactoryObject 来创建委托(Action、Func、Predicate 只是特殊的委托):
因此您不必再被迫创建一个构造,例如上面提到的 MySpringConfigurationDelegateObjectContainer 来通过工厂方法转发委托。
It is also possible to use the DelegateFactoryObject within Spring.net to create delegates (Action, Func, Predicate are only special delegates):
So you're not forced to create a construct such the MySpringConfigurationDelegateObjectContainer mentioned above to forward the delegates through factory methods anymore.
我想说你必须提取谓词和函数并将它们放在接口后面。然后你可以在你的构造函数中使用这个接口。如果您大多数时候使用构造函数注入,则将依赖项指定为接口或类型。示例中的构造函数使用 2 个实例变量(在本例中指向一个方法)。
您可以创建这样的接口:
并使用此接口作为构造函数参数,这会为您提供与让“其他配置对象”实现此接口完全相同的结果。
I would say you have to extract the predicate and the function and put them behind an interface. Then you can use this interface in your constructor. If your using constructor injection most of the times you specify the dependencies as interfaces or types. The constructor in your example uses 2 instance variables (in this case pointing to a method).
You could create an interface like this:
And use this interface as constructor param, which gives you the exact same result as you can have your 'other configured object' implement this interface.
我使用受 thekip 建议启发的中间“工厂”解决了这个问题,但在构造函数中保留了需要谓词和 func 的对象(上面示例中的 MyClass)。该解决方案将在 Spring 中处理委托的“问题”保留在 MyClass 的实现之外(与 thekip 的建议相反,但感谢您的回答)。
这是我配置这种情况的方法(在 Spring.Net 中设置委托,使用基于对象的工厂模式)。
MyDelegator 类是供谓词/函数使用的示例实现
(这里的对象,但可以是任何其他适合谓词/函数参数的东西):
...然后你需要一个带有谓词和/或函数的对象的容器(可以
也可以在单独的类中)……
这可以在 Spring.Net xml 中以这种方式进行配置
现在您可以在配置中的任何位置使用谓词/函数的对象
(无需更改需要谓词/函数的类)
就是这样。有什么建议可以改进这个解决方案吗?也许,Spring.net 已经为此提供了通用的解决方案......
I solved the problem using an intermediate "factory" inspired by the suggestion of thekip but leaving the object requiring the predicate and func in the constructor (MyClass in the example above) untouched. This solution keeps the "problem" of handling delegates in spring outside of the implementation of MyClass (in opposite of the suggestion of thekip, but thanks for your answer).
Here is my way to configure such a situation (setting delegates in Spring.Net, using object based factory pattern).
The MyDelegator class a sample implementation for predicate/func to use
(object here, but could be anything else fitting to the predicate/func params):
... then you need a container for the object with the predicate and or func (could
be in seperate classes, too) ...
... and this can be configured in Spring.Net xml this way
And now you can use this anywhere in your config for objects with predicate/func
(no need to change the class requiring the predicate/func)
That's it. Any suggestions to improve this solution? Maybe, Spring.net already has a generic solution for this...