New Snippet New Snippet Recent Snippets Recent Snippets My Snippets My Snippets Web Code Search Snippets Search
Sign inor Register
Language: C#

MEF DictionaryCatalog

77 Views
Copy Code Show/Hide Line Numbers
   1:  public class DictionaryCatalog<T> : ComposablePartCatalog
   2:  {
   3:      public DictionaryCatalog(IDictionary<string, T> dict)
   4:      {
   5:          InitializeParts(dict);
   6:      }
   7:   
   8:      private void InitializeParts(IDictionary<string, T> dict)
   9:      {
  10:          var partDefinitions = new List<ComposablePartDefinition>();
  11:   
  12:          foreach (KeyValuePair<string, T> item in dict)
  13:          {
  14:              var partDef = new DictionaryPartDefinition(item.Key, item.Value);
  15:              partDefinitions.Add(partDef);
  16:          }
  17:   
  18:          _partDefinitions = partDefinitions.AsQueryable<ComposablePartDefinition>();
  19:      }
  20:   
  21:      private IQueryable<ComposablePartDefinition> _partDefinitions;
  22:      public override IQueryable<ComposablePartDefinition> Parts
  23:      {
  24:          get { return _partDefinitions; }
  25:      }
  26:   
  27:      private class DictionaryPartDefinition : ComposablePartDefinition
  28:      {
  29:          private T _value;
  30:          public DictionaryPartDefinition(string contractName, T value)
  31:          {
  32:              _value = value;
  33:              _exportDefs.Add(new ExportDefinition(contractName, null));
  34:          }
  35:   
  36:          public override ComposablePart CreatePart()
  37:          {
  38:              return new DictionaryPart(_exportDefs[0], _value);
  39:          }   
  40:   
  41:          private List<ExportDefinition> _exportDefs = new List<ExportDefinition>();
  42:          public override IEnumerable<ExportDefinition> ExportDefinitions
  43:          {
  44:              get { return _exportDefs; }
  45:          }
  46:   
  47:          public override IEnumerable<ImportDefinition> ImportDefinitions
  48:          {
  49:              get { return Enumerable.Empty<ImportDefinition>(); }
  50:          }
  51:      }
  52:   
  53:      private class DictionaryPart : ComposablePart
  54:      {
  55:          private ExportDefinition _exportDef;
  56:          public DictionaryPart(ExportDefinition exportDef, T value)
  57:          {
  58:              _exportDef = exportDef;
  59:              _value = value;
  60:   
  61:              _exportDefs.Add(exportDef);
  62:          }
  63:   
  64:          private List<ExportDefinition> _exportDefs = new List<ExportDefinition>();
  65:          public override IEnumerable<ExportDefinition> ExportDefinitions
  66:          {
  67:              get { return _exportDefs; }
  68:          }
  69:   
  70:          private T _value;
  71:          public override object GetExportedValue(ExportDefinition definition)
  72:          {
  73:              if (definition.ContractName == _exportDef.ContractName)
  74:              {
  75:                  return _value;
  76:              }
  77:   
  78:              // throw an exception?
  79:              return null;
  80:          }
  81:   
  82:          public override IEnumerable<ImportDefinition> ImportDefinitions
  83:          {
  84:              get { return Enumerable.Empty<ImportDefinition>(); }
  85:          }
  86:   
  87:          public override void SetImport(ImportDefinition definition, IEnumerable<Export> exports)
  88:          {
  89:              throw new System.NotImplementedException();
  90:          }
  91:      }
  92:   
  93:  }
  94:   
by obiwanjacobi
  April 11, 2010 @ 11:30pm
Tags:

Add a comment


Report Abuse
brought to you by:
West Wind Techologies


If you find this site useful and use it frequently please consider making a donation to support this free service.
Donate