2012-01-17 2 views
0

경로를 결합하여 단일 매개 변수로 사용할 수있는 쉬운 "기본 제공 방식"이 있습니까?Spring.Net 설정에서 경로를 결합하여 속성 또는 생성자 인수로 사용하는 방법?

<property name="CombinedPath"> 
    <object type="SpringExt.SpringPathCombiner, SpringExt"> 
    <constructor-arg name="path1"> 
     <object factory-method="GetBasePath" factory-object="MyConfig" /> 
    </constructor-arg> 
    <constructor-arg name="path2" value="Temp" /> 
    </object> 
</property> 

이 함께 "온도"로 방법 GetBasePath의 반환 값의 조합에 결합 경로를 설정합니다, 예를 들면 :

나는 다음을 수행 SpringPathCombiner라는 자신의 구현을 사용하여 C : \ MyBasePath \ Temp.

public class SpringPathCombiner 
{ 
    private readonly string path; 

    public SpringPathCombiner(string path1, string path2) 
    { 
     path = Path.Combine(path1, path2); 
    } 

    public static implicit operator string(SpringPathCombiner combiner) 
    { 
     return combiner.path; 
    } 

    public override string ToString() 
    { 
     return path; 
    } 
} 

하지만 코드 중복을 가지고 싶지 않아 나는 방법에 의해 봄 그 자체로 가져온 것들로 작업의이 종류가 할 생각합니다 :

SpringPathCombiner 클래스 자체는 매우 간단합니다 . 누구나 자신의 구현없이이 작업을 수행하는 방법을 알고 있습니다. Path.Combine을 직접 사용 하시겠습니까?

답변

2

표현식을 사용해 보셨습니까? 예를 들어 :

<object id="MyObject" type="q8892913.MyClass, q8892913"> 
    <property name="Path" expression="T(System.IO.Path).Combine('c:\dev', 'Temp')" /> 
</object> 

당신은 쉽게도 다른 개체의 속성을 사용할 수 있습니다 :

<object id="MyObject" type="q8892913.MyClass, q8892913"> 
    <property name="Path" expression="T(System.IO.Path).Combine(@(MyConfig).Path, 'Temp')" /> 
</object> 

<object id="MyConfig" type="q8892913.MyClass, q8892913"> 
    <property name="Path" value="c:\dev" /> 
</object> 
+0

들으,이 트릭을 수행합니다. 하지만 적어도 문자열 (문자열과 같은 기본 유형의 경우)을 구성하는 데 더 읽기 쉬운 옵션이 있어야한다고 생각합니다. – Beachwalker

+0

문자열을 연결하는 경우 expression = " 'con과 같이 + [연산자] (http://www.springframework.net/doc-latest/reference/html/expressions.html#expressions-math)를 사용할 수 있습니다. '+'고양이 ''. – Marijn

+1

좋은 점은이 경우 (경로)에 대해 Path.Combine을 선호하는데 변수가 후행 '\'로 끝나도 제어 할 수 없기 때문입니다. – Beachwalker

관련 문제