CodePaste Logo
New Snippet New Recent Snippets Recent Snippets My Snippets My Snippets My Favorites Favorites Web Code Search Snippets Search
Sign inor Register
Language: csharp

Configuration Export Provider

1761 Views   
using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel.Composition; using System.ComponentModel.Composition.Hosting; using System.ComponentModel.Composition.Primitives; using System.Linq; using System.Text; public class ConfigurationExportProvider : ExportProvider { private ILookup<string, object> _valueLookup; public ConfigurationExportProvider(ILookup<string,object> valueLookup) { _valueLookup = valueLookup; } protected override IEnumerable<Export> GetExportsCore(ImportDefinition importDefinition, AtomicComposition composition) { var contractName = importDefinition.ContractName; if (contractName == null) return Enumerable.Empty<Export>(); if (!_valueLookup.Contains(contractName)) return Enumerable.Empty<Export>(); return new Collection<Export>(){ new Export(contractName, () => {return _valueLookup[contractName].FirstOrDefault();}) }; } } //IValueLookup implementation for talking to a dictionary public class ValueLookup : ILookup<string,object> { public ValueLookup(IDictionary<string,object> configValues) { _configValues = configValues; } private IDictionary<string,object> _configValues; public object GetValue(string key) { return _configValues[key]; } public bool ContainsKey(string key) { return _configValues.ContainsKey(key); } public IEnumerator<IGrouping<string, object>> GetEnumerator() { var groupBy = _configValues.GroupBy(item => item.Key, item => item.Value).AsEnumerable(); return groupBy.GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); } public bool Contains(string key) { return _configValues.ContainsKey(key); } public int Count { get { return _configValues.Count; } } public IEnumerable<object> this[string key] { get { return new List<object> {_configValues[key]}; } } }
by Glenn Block
  April 11, 2010 @ 11:47pm
Description:
Export Provider which will talk to a configuration store. Simply requires passing an ILookup implementation.
by Glenn Block    April 12, 2010 @ 2:15am

Marc Jacobi has created a nice port of this that uses an extension method to simplify usage. Can be found here: http://codepaste.net/4yxtrz

Add a comment


Report Abuse
brought to you by:
West Wind Techologies