2010-09-24 16 views
5

मैं एमईएफ को उन सभी हिस्सों को पुन: संकलित करने की कोशिश कर रहा हूं, जिन्हें मैं निर्यात करता हूं जब मैं निर्यात किया गया एक उदाहरण अद्यतन करता हूं। अनिवार्य रूप से मैं अपने सभी हिस्सों को एमईएफ अपडेट करना चाहता हूं जो कनेक्शन बदलते समय कनेक्शन स्ट्रिंग कॉन्फ़िगरेशन मान आयात करते हैं। सब कुछ उस बिंदु तक अच्छा दिखता है जहां मैं उदाहरण बदलना चाहता हूं। यदि मैं अद्यतन मूल्य के साथ कंपोज़पार्ट्स का प्रयास करता हूं तो यह कंटेनर में दूसरा भाग उदाहरण जोड़ता है, और फिर मेरे आयात अपडेट होते हैं, लेकिन शून्य के लिए।जब मैं भाग बदलता हूं तो मैं पुन: संयोजन करने के लिए एमईएफ कैसे प्राप्त करूं?

क्या कोई यह बता सकता है कि मैं कहां गलत हूं? या मुझे इस फैशन में एमईएफ का उपयोग करने की कोशिश भी करनी चाहिए?

मैं एमईएफ पूर्वावलोकन 9 और लक्ष्यीकरण नेट फ्रेमवर्क 3.5, और डब्ल्यूपीएफ का उपयोग कर रहा हूं।

using System; 
using System.Collections.Generic; 
using System.ComponentModel.Composition; 
using System.ComponentModel.Composition.Hosting; 
using System.Linq; 
using System.Text; 
using Shouldly; 

namespace ConsoleApplication4 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      MainClass main = new MainClass(); 
      main.RunTest(); 
     } 
    } 

    public class MainClass 
    { 
     [ImportMany] 
     public IEnumerable<Settings> Settings { get; set; } 

     public void RunTest() 
     { 
      AggregateCatalog catalog = new AggregateCatalog(); 
      catalog.Catalogs.Add(new AssemblyCatalog(typeof(Settings).Assembly)); 

      CompositionContainer container = new CompositionContainer(catalog); 

      container.SatisfyImportsOnce(this); 

      Config cfg = new Config 
      { 
       Settings = new Settings { ConnectionString = "Value1" }, 
      }; 

      // result is returned with a null settings value 
      UsesSettings result = container.GetExportedValue<UsesSettings>(); 

      // this recomposes everything with the new value, result changes to have settings of Value1 
      container.ComposeParts(cfg); 

      // this line results in my import many enumerable returning 2 parts the Value1 setting and null 
      container.SatisfyImportsOnce(this); 

      result.TheSettings.ConnectionString.ShouldBe("Value1"); 

      cfg.Settings = new Settings { ConnectionString = "Value2" }; 

      // how do I tell the container to recompose now I have changed the config object, 
      // or how do I replace the part value with the new value? 

      // this line causes the result.Settings to return null 
      container.ComposeParts(cfg); 

      // this updates the ImportMany to 3 values, Value1, Value2 and null 
      container.SatisfyImportsOnce(this); 
     } 
    } 

    public class Settings 
    { 
     public string ConnectionString = "default value"; 
    } 

    public class Config 
    { 
     [Export(typeof(Settings))] 
     public Settings Settings { get; set; } 
    } 

    [Export(typeof(UsesSettings))] 
    public class UsesSettings 
    { 
     [Import(typeof(Settings), AllowRecomposition = true)] 
     public Settings TheSettings { get; set; } 
    } 
} 

उत्तर

6

आपके पास परिदृश्य के लिए कुछ चीजें हैं जो आप पूरा करने की कोशिश कर रहे हैं।

पहला: यदि आप किसी विशेष निर्यात को जोड़ना/निकालना/बदलना चाहते हैं तो यह कैटलॉग में ही नहीं होना चाहिए, इसलिए आपको अपनी कॉन्फ़िगरेशन क्लास को हटा देना चाहिए जिसमें निर्यात सेटिंग्स संपत्ति है। यह CatalogExportProvider द्वारा बनाया जा रहा है और यही कारण है कि आप अपने संग्रह में शून्य मान देख रहे हैं।

public class Program 
{ 
    [ImportMany(AllowRecomposition=true)] 
    public IEnumerable<Settings> Settings { get; set; } 

    public void RunTest() 
    { 
     AggregateCatalog catalog = new AggregateCatalog(); 
     catalog.Catalogs.Add(new AssemblyCatalog(typeof(Settings).Assembly)); 

     CompositionContainer container = new CompositionContainer(catalog); 

     // Settings should have 0 values. 
     container.ComposeParts(this); 
     Contract.Assert(this.Settings.Count() == 0); 

     CompositionBatch batch = new CompositionBatch(); 

     // Store the settingsPart for later removal... 
     ComposablePart settingsPart = 
      batch.AddExportedValue(new Settings { ConnectionString = "Value1" }); 

     container.Compose(batch); 

     // Settings should have "Value1" 
     UsesSettings result = container.GetExportedValue<UsesSettings>(); 
     Contract.Assert(result.TheSettings.ConnectionString == "Value1"); 

     // Settings should have 1 value which is "Value1"; 
     Contract.Assert(this.Settings.Count() == 1); 
     Contract.Assert(this.Settings.First().ConnectionString == "Value1"); 

     // Remove the old settings and replace it with a new one. 
     batch = new CompositionBatch(); 
     batch.RemovePart(settingsPart); 
     batch.AddExportedValue(new Settings { ConnectionString = "Value2" }); 
     container.Compose(batch); 

     // result.Settings should have "Value2" now. 
     Contract.Assert(result.TheSettings.ConnectionString == "Value2"); 

     // Settings should have 1 value which is "Value2" 
     Contract.Assert(this.Settings.Count() == 1); 
     Contract.Assert(this.Settings.First().ConnectionString == "Value2"); 
    } 
} 
public class Settings 
{ 
    public string ConnectionString = "default value"; 
} 

[Export(typeof(UsesSettings))] 
public class UsesSettings 
{ 
    [Import(typeof(Settings), AllowRecomposition = true)] 
    public Settings TheSettings { get; set; } 
} 
+0

धन्यवाद वेस:

निम्नलिखित कोशिश करो! मैंने अपने कोड को संगत भाग पर पकड़ने के लिए बदल दिया जिसने मुझे अधिक से अधिक उदाहरण जोड़ने के बजाय, कंटेनर में भाग को अपडेट करने की अनुमति दी, अब बहुत अच्छा काम करता है। – Matt

संबंधित मुद्दे

 संबंधित मुद्दे