2009-09-15 37 views
6

tôi là chuyển đổi một ứng dụng Castle/đường ray thành một Unity/Asp.NET MVC một, Tôi đang mắc kẹt trong cố gắng để chuyển đổi cấu hình thành phần này:Unity: Xây dựng một từ điển

<component 
    id="ComponentBaseConfiguration" 
    service="MyFakeNamespace.BOL.IConfiguration, MyFakeAppDll" 
    type="MyFakeNamespace.BOL.ConfigurableConfiguration, MyFakeAppDll"> 
    <parameters> 
    <!-- Setting Configuration (Dictionary<string,string>)--> 
    <Config> 
     <dictionary> 
     <entry key="localHost">#{LocalHost}</entry>    
     <entry key="contentHost">#{ContentHost}</entry> 
     <entry key="virtualDir">#{VirtualDir}</entry>    
     </dictionary> 
    </Config> 
    </parameters> 

vẻ như Unity hỗ trợ mảng nhưng không điển, tôi muốn làm một cái gì đó như thế này:

<unity> 
<containers> 
    <container> 
     <types> 
      <type name="ComponentBaseConfiguration" type="MyFakeNamespace.BOL.IConfiguration, MyFakeAppDll" mapTo="MyFakeNamespace.BOL.ConfigurableConfiguration, MyFakeAppDll"> 
       <typeConfig extensionType="Microsoft.Practices.Unity.Configuration.TypeInjectionElement, Microsoft.Practices.Unity.Configuration"> 
        <property name="Config" propertyType="System.Collections.Generic.Dictionary`2[[System.String, mscorlib], [System.String, mscorlib]],mscorlib"> 
         <dictionary> 
          <entry key="localHost">127.0.0.1</keyedValue> 
          <entry key="contentHost">\\content</keyedValue> 
          <entry key="virtualDir">/</keyedValue> 
         </dictionary> 
        </property> 
       </typeConfig> 
      </type> 
     </types> 
    </container> 
</containers></unity> 

làm thế nào tôi có thể đạt được một cái gì đó như thế này?

+0

Tôi nhận được thông báo như thế này để hoạt động. xem - http://stackoverflow.com/questions/5597492/how-do-i-create-and-populate-a-dictionarystring-object-using-unitys-xml-confi/7901103#7901103 – ScArcher2

+0

Đây là giải pháp của tôi cho cũ hơn phiên bản - nó đòi hỏi một số thay đổi trong mã - khi tôi sẽ có thời gian có lẽ tôi sẽ cố gắng để thích ứng với vesion mới của Unity http://unity.codeplex.com/discussions/230927/ – AdaskoTheBeAsT

Trả lời

6

Tôi nghĩ bạn phải sử dụng phương thức để lưu trữ điều này. Nó không tốt đẹp nhưng một workaround.

Loại của bạn phải xác định phương thức Thêm (chuỗi khóa, giá trị chuỗi) mà vùng chứa thống nhất sử dụng để chèn các giá trị.

<method name="Add"> 
<param name="key" parameterType="string"> 
    <value value="localHost"/> 
</param> 
<param name="value" parameterType="string"> 
    <value value="127.0.0.1"/> 
</param> 
</method> 

Unity chắc chắn không hỗ trợ từ điển cho cấu hình vùng chứa. Xem Build Dictionaries using Unity container?

1

tôi phát hiện ra rằng Unity có lỗi khi xử lý Generics (http://unity.codeplex.com/Thread/View.aspx?ThreadId=30292), có một cách giải quyết khá xấu xí như thế này:

public class MyDictionary : Dictionary<string,string>{ 

    public MyDictionary() { 

    } 
} 

bây giờ trong tập tin cấu hình:

 <typeAlias alias="string" type="System.String, mscorlib" />    
     <typeAlias alias="Dictionary" type="MyFakeNamespace.MyDictionary, MyFakeAppDll" /> 

...

và sau đó sử dụng đề xuất của Jehof:

<type name="ConfigurationDictionary" type="Dictionary"> 
        <typeConfig extensionType="Microsoft.Practices.Unity.Configuration.TypeInjectionElement, Microsoft.Practices.Unity.Configuration"> 
         <method name="Add" key="0"> 
          <param name="key" parameterType="string"> 
           <value value="localHost"/> 
          </param> 
          <param name="value" parameterType="string"> 
           <value value="127.0.0.1"/> 
          </param> 
         </method> 
         <method name="Add" key="1"> 
          <param name="key" parameterType="string"> 
           <value value="contentHost"/> 
          </param> 
          <param name="value" parameterType="string"> 
           <value value="\\content"/> 
          </param> 
         </method> 
         <method name="Add" key="2"> 
          <param name="key" parameterType="string"> 
           <value value="virtualDir"/> 
          </param> 
          <param name="value" parameterType="string"> 
           <value value="/"/> 
          </param> 
         </method> 
        </typeConfig> 

       </type> 

thuộc tính khóa trong thẻ phương thức cần phải là duy nhất để gọi phương thức Thêm nhiều lần.

Sau đó, khi lỗi sẽ được giải quyết một chút thay đổi trong loạiAlias ​​cho phép chúng tôi đặt đúng loại, nhưng tôi nghĩ tôi sẽ để nguyên như vậy.

Các vấn đề liên quan