如何Spring.net注入predicate以及FUNCnet、Spring、FUNC、predicate

2023-09-03 01:10:11 作者:她是纯氧.

我想创建一个对象,使用Spring XML配置含构造函数predicate和FUNC对象。在predicate和函数功能参数应该指向其他配置对象的方法。这可能是如何使用Spring.net?我没能找到的文档中的解决方案或暗示......

一个样本的构造将是:

  MyClass的(predicate< TInput>的条件,Func键< TInput,TOutput>结果)
 

解决方案

我使用的是中间的工厂的灵感来自thekip的建议,但离开该对象的要求predicate和FUNC在构造函数(MyClass的解决了这个问题在上面的例子)不变。该解决方案保留了处理春季代表MyClass的实施外的问题(在thekip的建议相反,但感谢你的答案)。

Spring Cloud Netflix概览和架构设计

下面是我的方法来配置这样的情况(设置在Spring.Net代表,采用基于对象的工厂模式)。

该MyDelegator类的样本实现predicate / FUNC使用 (这里的对象,但也可以是其他任何配件到predicate / FUNC PARAMS):

 公共类MyDelegator
{
    //为predicate predicate< TInput>条件下,随着对象实现
    公布尔条件(对象输入)
    {
        ...
    }

    //为函数功能:LT; TInput,TOutput>结果,实现与目标
    公众评价对象(物体输入)
    {
        ...
    }
}
 

...那么你需要一个容器与predicate和或FUNC对象(可以 在单独的班,太)...

 公共类MySpringConfigurationDelegateObjectContainer
{
    私人只读predicate<对象>条件;
    私人只读Func键<对象,对象>评估;

    公共MySpringConfigurationDelegateObjectContainer(MyDelegator策略)
    {
        条件= strategy.Condition;
        评估= strategy.Evaluate;
    }

    公共predicate<对象> GetConditionMethod()
    {
        返回的条件;
    }

    公共函数功能:LT;对象,对象> GetEvaluateMethod()
    {
        返回评估;
    }
}
 

...,这可以在Spring.Net的xml这种方式被构造

 <! - 一个简单的容器类,而只有与方法的对象调用 - >
  <对象ID =集装箱式=MySpringConfigurationDelegateObjectContainer>
    <构造带参数的名称=myDelegateObject>
      <! - 把对象的委托/ FUNC / predicate在这里 - >
      <对象类型=MyDelegator/>
    < /构造带参数的>
  &所述; /对象>
 

现在你可以使用带有predicate / FUNC对象这在任何地方你的配置 (无需更改类要求predicate / FUNC)

 <对象ID =NeedA predicateAndFuncInConstructorTYPE =...>
    ...
    <构造带参数的名称=条件>
      <对象工厂方法=GetConditionMethod工厂对象=容器/>
    < /构造带参数的>
    <构造带参数的名称=结果>
      <对象工厂方法=GetEvaluateMethod工厂对象=容器/>
    < /构造带参数的>
    ...
  &所述; /对象>
 

就是这样。任何建议,以改善这个解决方案?也许,Spring.net已经有一个通用的解决方案,这...

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)

解决方案

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):

public class MyDelegator
{
    // for the predicate Predicate<TInput> condition, implemented with object
    public bool Condition(object input) 
    {
        ...
    }

    //for the Func<TInput, TOutput> result, implemented with object
    public object Evaluate(object input) 
    {
        ...
    }
}

... then you need a container for the object with the predicate and or func (could be in seperate classes, too) ...

public class MySpringConfigurationDelegateObjectContainer
{
    private readonly Predicate<object> condition;
    private readonly Func<object, object> evaluate;

    public MySpringConfigurationDelegateObjectContainer(MyDelegator strategy)
    {
        condition = strategy.Condition;
        evaluate = strategy.Evaluate;
    }

    public Predicate<object> GetConditionMethod()
    {
        return condition;
    }

    public Func<object, object> GetEvaluateMethod()
    {
        return evaluate;
    }
}

... and this can be configured in Spring.Net xml this way

  <!-- a simple container class, just has the object with the method to call -->
  <object id="Container" type="MySpringConfigurationDelegateObjectContainer">
    <constructor-arg name="myDelegateObject">
      <!-- put the object with the delegate/func/predicate here -->
      <object type="MyDelegator" />
    </constructor-arg>
  </object>

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)

  <object id="NeedAPredicateAndFuncInConstructor" type="...">
    ...
    <constructor-arg name="condition">
      <object factory-method="GetConditionMethod" factory-object="Container" />
    </constructor-arg>
    <constructor-arg name="result">
      <object factory-method="GetEvaluateMethod" factory-object="Container" />
    </constructor-arg>
    ...
  </object>

That's it. Any suggestions to improve this solution? Maybe, Spring.net already has a generic solution for this...