using System; using System.Collections.Generic; using System.Reflection; using System.Text; using Castle.DynamicProxy; using Config.Net.Core; namespace Config.Net { public class ConfigurationBuilder where T : class { private readonly ProxyGenerator _generator = new ProxyGenerator(); private List _stores = new List(); private TimeSpan _cacheInterval = TimeSpan.Zero; private readonly List _customParsers = new List(); public ConfigurationBuilder() { TypeInfo ti = typeof(T).GetTypeInfo(); if (!ti.IsInterface) throw new ArgumentException($"{ti.FullName} must be an interface", ti.FullName); } /// /// Creates an instance of the configuration interface /// /// public T Build() { var valueHandler = new ValueHandler(_customParsers); var ioHandler = new IoHandler(_stores, valueHandler, _cacheInterval); T instance = _generator.CreateInterfaceProxyWithoutTarget(new InterfaceInterceptor(typeof(T), ioHandler)); return instance; } /// /// Set to anything different from to add caching for values. By default /// Config.Net doesn't cache any values /// /// /// public ConfigurationBuilder CacheFor(TimeSpan time) { _cacheInterval = time; return this; } public ConfigurationBuilder UseConfigStore(IConfigStore store) { _stores.Add(store); return this; } /// /// Adds a custom type parser /// public ConfigurationBuilder UseTypeParser(ITypeParser parser) { if (parser == null) { throw new ArgumentNullException(nameof(parser)); } _customParsers.Add(parser); return this; } } }