From: <jer...@us...> - 2009-12-29 14:40:53
|
Revision: 317 http://structuremap.svn.sourceforge.net/structuremap/?rev=317&view=rev Author: jeremydmiller Date: 2009-12-29 14:40:45 +0000 (Tue, 29 Dec 2009) Log Message: ----------- last minute fixes to scanning found in regression test runs Modified Paths: -------------- trunk/Source/StructureMap/Container.cs trunk/Source/StructureMap/Graph/ITypeScanner.cs trunk/Source/StructureMap/Graph/PluginFamily.cs trunk/Source/StructureMap/IContainer.cs trunk/Source/StructureMap/InitializationExpression.cs trunk/Source/StructureMap/PipelineGraph.cs trunk/Source/StructureMap/TypeExtensions.cs trunk/Source/StructureMap/Util/Cache.cs Modified: trunk/Source/StructureMap/Container.cs =================================================================== --- trunk/Source/StructureMap/Container.cs 2009-12-29 04:51:11 UTC (rev 316) +++ trunk/Source/StructureMap/Container.cs 2009-12-29 14:40:45 UTC (rev 317) @@ -482,6 +482,11 @@ Configure(x => x.For<PLUGINTYPE>().Use(instance)); } + public void Inject<PLUGINTYPE>(string name, PLUGINTYPE value) + { + Configure(x => x.For<PLUGINTYPE>().Use(value).Named(name)); + } + /// <summary> /// Injects the given object into a Container as the default for the designated /// pluginType. Mostly used for temporarily setting up return values of the Container Modified: trunk/Source/StructureMap/Graph/ITypeScanner.cs =================================================================== --- trunk/Source/StructureMap/Graph/ITypeScanner.cs 2009-12-29 04:51:11 UTC (rev 316) +++ trunk/Source/StructureMap/Graph/ITypeScanner.cs 2009-12-29 14:40:45 UTC (rev 317) @@ -24,7 +24,7 @@ Type pluginType = FindPluginType(type); if (pluginType != null && Constructor.HasConstructors(type)) { - registry.For(pluginType).Add(type); + registry.AddType(pluginType, type); } } Modified: trunk/Source/StructureMap/Graph/PluginFamily.cs =================================================================== --- trunk/Source/StructureMap/Graph/PluginFamily.cs 2009-12-29 04:51:11 UTC (rev 316) +++ trunk/Source/StructureMap/Graph/PluginFamily.cs 2009-12-29 14:40:45 UTC (rev 317) @@ -34,14 +34,6 @@ PluginFamilyAttribute.ConfigureFamily(this); - //if (IsConcrete(pluginType)) - //{ - // Plugin plugin = PluginCache.GetPlugin(pluginType); - // if (plugin.CanBeCreated()) - // { - // AddPlugin(pluginType, Plugin.DEFAULT); - // } - //} } public PluginGraph Parent { get { return _parent; } set { _parent = value; } } @@ -362,7 +354,6 @@ /// <summary> /// The InstanceKey of the default instance of the PluginFamily /// </summary> - [Obsolete] public string DefaultInstanceKey { get { return _defaultKey; } set { _defaultKey = value ?? string.Empty; } } #endregion Modified: trunk/Source/StructureMap/IContainer.cs =================================================================== --- trunk/Source/StructureMap/IContainer.cs 2009-12-29 04:51:11 UTC (rev 316) +++ trunk/Source/StructureMap/IContainer.cs 2009-12-29 14:40:45 UTC (rev 317) @@ -126,6 +126,9 @@ /// <param name="instance"></param> void Inject<PLUGINTYPE>(PLUGINTYPE instance); + void Inject<PLUGINTYPE>(string name, PLUGINTYPE value); + + /// <summary> /// Injects the given object into a Container as the default for the designated /// pluginType. Mostly used for temporarily setting up return values of the Container Modified: trunk/Source/StructureMap/InitializationExpression.cs =================================================================== --- trunk/Source/StructureMap/InitializationExpression.cs 2009-12-29 04:51:11 UTC (rev 316) +++ trunk/Source/StructureMap/InitializationExpression.cs 2009-12-29 14:40:45 UTC (rev 317) @@ -7,7 +7,7 @@ namespace StructureMap { - public interface IInitializationExpression + public interface IInitializationExpression : IRegistry { /// <summary> /// If true, makes the existence of the StructureMap.config mandatory. @@ -61,118 +61,6 @@ /// <param name="registry"></param> void AddRegistry(Registry registry); - - /// <summary> - /// Expression Builder used to define policies for a PluginType including - /// Scoping, the Default Instance, and interception. BuildInstancesOf() - /// and ForRequestedType() are synonyms - /// </summary> - /// <typeparam name="PLUGINTYPE"></typeparam> - /// <returns></returns> - CreatePluginFamilyExpression<PLUGINTYPE> BuildInstancesOf<PLUGINTYPE>(); - - /// <summary> - /// Expression Builder used to define policies for a PluginType including - /// Scoping, the Default Instance, and interception. BuildInstancesOf() - /// and ForRequestedType() are synonyms - /// </summary> - /// <typeparam name="PLUGINTYPE"></typeparam> - /// <returns></returns> - CreatePluginFamilyExpression<PLUGINTYPE> ForRequestedType<PLUGINTYPE>(); - - /// <summary> - /// Expression Builder used to define policies for a PluginType including - /// Scoping, the Default Instance, and interception. This method is specifically - /// meant for registering open generic types - /// </summary> - /// <returns></returns> - GenericFamilyExpression ForRequestedType(Type pluginType); - - /// <summary> - /// This method is a shortcut for specifying the default constructor and - /// setter arguments for a ConcreteType. ForConcreteType is shorthand for: - /// ForRequestedType[T]().TheDefault.Is.OfConcreteType[T].************** - /// when the PluginType and ConcreteType are the same Type - /// </summary> - /// <typeparam name="T"></typeparam> - /// <returns></returns> - Registry.BuildWithExpression<T> ForConcreteType<T>(); - - /// <summary> - /// Adds an additional, non-Default Instance to the PluginType T. - /// </summary> - /// <typeparam name="T"></typeparam> - /// <returns></returns> - IsExpression<T> InstanceOf<T>(); - - /// <summary> - /// Adds an additional, non-Default Instance to the designated pluginType - /// This method is mostly meant for open generic types - /// </summary> - /// <param name="pluginType"></param> - /// <returns></returns> - GenericIsExpression InstanceOf(Type pluginType); - - /// <summary> - /// Expression Builder to define the defaults for a named Profile. Each call - /// to CreateProfile is additive. - /// </summary> - /// <param name="profileName"></param> - /// <returns></returns> - ProfileExpression Profile(string profileName); - - /// <summary> - /// An alternative way to use CreateProfile that uses ProfileExpression - /// as a Nested Closure. This usage will result in cleaner code for - /// multiple declarations - /// </summary> - /// <param name="profileName"></param> - /// <param name="action"></param> - void Profile(string profileName, Action<ProfileExpression> action); - - /// <summary> - /// Registers a new TypeInterceptor object with the Container - /// </summary> - /// <param name="interceptor"></param> - void RegisterInterceptor(TypeInterceptor interceptor); - - /// <summary> - /// Allows you to define a TypeInterceptor inline with Lambdas or anonymous delegates - /// </summary> - /// <param name="match"></param> - /// <returns></returns> - /// <example> - /// IfTypeMatches( ... ).InterceptWith( o => new ObjectWrapper(o) ); - /// </example> - MatchedTypeInterceptor IfTypeMatches(Predicate<Type> match); - - /// <summary> - /// Expresses a single "Scanning" action - /// </summary> - /// <param name="action"></param> - /// <example> - /// Scan(x => { - /// x.Assembly("Foo.Services"); - /// x.WithDefaultConventions(); - /// }); - /// </example> - void Scan(Action<IAssemblyScanner> action); - - /// <summary> - /// Directs StructureMap to always inject dependencies into any and all public Setter properties - /// of the type PLUGINTYPE. - /// </summary> - /// <typeparam name="PLUGINTYPE"></typeparam> - /// <returns></returns> - CreatePluginFamilyExpression<PLUGINTYPE> FillAllPropertiesOfType<PLUGINTYPE>(); - - /// <summary> - /// Creates automatic "policies" for which public setters are considered mandatory - /// properties by StructureMap that will be "setter injected" as part of the - /// construction process. - /// </summary> - /// <param name="action"></param> - void SetAllProperties(Action<SetterConvention> action); } public class InitializationExpression : ConfigurationExpression, IInitializationExpression Modified: trunk/Source/StructureMap/PipelineGraph.cs =================================================================== --- trunk/Source/StructureMap/PipelineGraph.cs 2009-12-29 04:51:11 UTC (rev 316) +++ trunk/Source/StructureMap/PipelineGraph.cs 2009-12-29 14:40:45 UTC (rev 317) @@ -234,7 +234,10 @@ public void EachInstance(Action<Type, Instance> action) { - _factories.Values.Each(f => { f.AllInstances.Each(i => action(f.PluginType, i)); }); + _factories.Values.ToArray().Each(f => + { + f.AllInstances.ToArray().Each(i => action(f.PluginType, i)); + }); } public IObjectCache FindCache(Type pluginType) Modified: trunk/Source/StructureMap/TypeExtensions.cs =================================================================== --- trunk/Source/StructureMap/TypeExtensions.cs 2009-12-29 04:51:11 UTC (rev 316) +++ trunk/Source/StructureMap/TypeExtensions.cs 2009-12-29 14:40:45 UTC (rev 317) @@ -5,7 +5,7 @@ namespace StructureMap { - public static class BasicExtensions + internal static class BasicExtensions { public static string ToName(this ILifecycle lifecycle) { @@ -30,7 +30,7 @@ } } - public static T As<T>(this object target) where T : class + internal static T As<T>(this object target) where T : class { return target as T; } Modified: trunk/Source/StructureMap/Util/Cache.cs =================================================================== --- trunk/Source/StructureMap/Util/Cache.cs 2009-12-29 04:51:11 UTC (rev 316) +++ trunk/Source/StructureMap/Util/Cache.cs 2009-12-29 14:40:45 UTC (rev 317) @@ -1,127 +1,147 @@ using System; +using System.Collections; using System.Collections.Generic; -using System.Threading; +using StructureMap.Graph; +using StructureMap.Pipeline; namespace StructureMap.Util { - [Serializable] - public class Cache<TKey, TValue> where TValue : class + public class Cache<KEY, VALUE> : IEnumerable<VALUE> where VALUE : class { - private readonly ReaderWriterLockSlim _lock = new ReaderWriterLockSlim(LockRecursionPolicy.SupportsRecursion); - private readonly IDictionary<TKey, TValue> _values; - private Func<TValue, TKey> _getKey = delegate { throw new NotImplementedException(); }; + private readonly object _locker = new object(); + private readonly IDictionary<KEY, VALUE> _values; - private Action<TValue> _onAddition = x => { }; + private Func<VALUE, KEY> _getKey = delegate { throw new NotImplementedException(); }; - private Func<TKey, TValue> _onMissing = delegate(TKey key) + private Func<KEY, VALUE> _onMissing = delegate(KEY key) { string message = string.Format("Key '{0}' could not be found", key); throw new KeyNotFoundException(message); }; public Cache() - : this(new Dictionary<TKey, TValue>()) + : this(new Dictionary<KEY, VALUE>()) { } - public Cache(Func<TKey, TValue> onMissing) - : this(new Dictionary<TKey, TValue>(), onMissing) + public Cache(Func<KEY, VALUE> onMissing) + : this(new Dictionary<KEY, VALUE>(), onMissing) { } - public Cache(IDictionary<TKey, TValue> dictionary, Func<TKey, TValue> onMissing) + public Cache(IDictionary<KEY, VALUE> dictionary, Func<KEY, VALUE> onMissing) : this(dictionary) { _onMissing = onMissing; } - public Cache(IDictionary<TKey, TValue> dictionary) + public Cache(IDictionary<KEY, VALUE> dictionary) { _values = dictionary; } - public Func<TKey, TValue> OnMissing { set { _onMissing = value; } } + public Func<KEY, VALUE> OnMissing + { + set { _onMissing = value; } + } - public Action<TValue> OnAddition { set { _onAddition = value; } } + public Func<VALUE, KEY> GetKey + { + get { return _getKey; } + set { _getKey = value; } + } - public Func<TValue, TKey> GetKey { get { return _getKey; } set { _getKey = value; } } - public int Count { - get - { - using (ReadLock()) - return _values.Count; - } + get { return _values.Count; } } - public TValue First + public VALUE First { get { - using (ReadLock()) - foreach (var pair in _values) - { - return pair.Value; - } + foreach (var pair in _values) + { + return pair.Value; + } return null; } } - public TValue this[TKey key] + public VALUE this[KEY key] { get { - using (ReadLockToken @lock = ReadLock()) + if (!_values.ContainsKey(key)) { - if (!_values.ContainsKey(key)) + lock (_locker) { - @lock.Upgrade(); if (!_values.ContainsKey(key)) { - TValue value = _onMissing(key); - _values.Add(key, value); + VALUE value = _onMissing(key); + //Check to make sure that the onMissing didn't cache this already + if(!_values.ContainsKey(key)) + _values.Add(key, value); } } - - return _values[key]; } + + return _values[key]; } set { - _onAddition(value); - - using (WriteLockToken @lock = WriteLock()) + if (_values.ContainsKey(key)) { - if (_values.ContainsKey(key)) - { - _values[key] = value; - } - else - { - _values.Add(key, value); - } + _values[key] = value; } + else + { + _values.Add(key, value); + } } } - public void Fill(TKey key, TValue value) + #region IEnumerable<VALUE> Members + + IEnumerator IEnumerable.GetEnumerator() { - using (WriteLock()) + return ((IEnumerable<VALUE>)this).GetEnumerator(); + } + + public IEnumerator<VALUE> GetEnumerator() + { + return _values.Values.GetEnumerator(); + } + + #endregion + + public void Fill(KEY key, VALUE value) + { + if (_values.ContainsKey(key)) { - if (_values.ContainsKey(key)) - { - return; - } + return; + } - _values.Add(key, value); + _values.Add(key, value); + } + + public bool TryRetrieve(KEY key, out VALUE value) + { + value = default(VALUE); + + if (_values.ContainsKey(key)) + { + value = _values[key]; + return true; } + + return false; } - public void Each(Action<TValue> action) + public void Each(Action<VALUE> action) { - using (ReadLock()) + lock (_locker) { foreach (var pair in _values) { @@ -130,159 +150,76 @@ } } - public void Each(Action<TKey, TValue> action) + public void Each(Action<KEY, VALUE> action) { - using (ReadLock()) + foreach (var pair in _values) { - foreach (var pair in _values) - { - action(pair.Key, pair.Value); - } + action(pair.Key, pair.Value); } } - public bool Has(TKey key) + public bool Has(KEY key) { - using (ReadLock()) - return _values.ContainsKey(key); + return _values.ContainsKey(key); } - public bool Exists(Predicate<TValue> predicate) + public bool Exists(Predicate<VALUE> predicate) { bool returnValue = false; - Each(delegate(TValue value) { returnValue |= predicate(value); }); + Each(delegate(VALUE value) { returnValue |= predicate(value); }); return returnValue; } - public TValue Find(Predicate<TValue> predicate) + public VALUE Find(Predicate<VALUE> predicate) { - using (ReadLock()) - foreach (var pair in _values) + foreach (var pair in _values) + { + if (predicate(pair.Value)) { - if (predicate(pair.Value)) - { - return pair.Value; - } + return pair.Value; } + } return null; } - public TKey Find(TValue value) + public VALUE[] GetAll() { - using (ReadLock()) - foreach (var pair in _values) - { - if (pair.Value == value) - { - return pair.Key; - } - } + var returnValue = new VALUE[Count]; + _values.Values.CopyTo(returnValue, 0); - return default(TKey); - } - - public TValue[] GetAll() - { - var returnValue = new TValue[Count]; - using (ReadLock()) - _values.Values.CopyTo(returnValue, 0); - return returnValue; } - public void Remove(TKey key) + public void Remove(KEY key) { - using (ReadLock()) - if (_values.ContainsKey(key)) - { - _values.Remove(key); - } + if (_values.ContainsKey(key)) + { + _values.Remove(key); + } } public void Clear() { - using (WriteLock()) - _values.Clear(); + _values.Clear(); } - public void WithValue(TKey key, Action<TValue> callback) + public Cache<KEY, VALUE> Clone() { - using (ReadLock()) - _values.TryGet(key, callback); - } + var clone = new Cache<KEY, VALUE>(_onMissing); + _values.Each(pair => clone[pair.Key] = pair.Value); - private ReadLockToken ReadLock() - { - return new ReadLockToken(_lock); + return clone; } - private WriteLockToken WriteLock() + public void WithValue(KEY key, Action<VALUE> action) { - return new WriteLockToken(_lock); - } - - public Cache<TKey, TValue> Clone() - { - var cache = new Cache<TKey, TValue>(); - cache._onMissing = _onMissing; - - Each((key, value) => cache[key] = value); - - return cache; - } - - #region Nested type: ReadLockToken - - private class ReadLockToken : IDisposable - { - private readonly ReaderWriterLockSlim _lock; - private bool upgraded; - - public ReadLockToken(ReaderWriterLockSlim @lock) + if (_values.ContainsKey(key)) { - _lock = @lock; - _lock.EnterReadLock(); + action(this[key]); } - - public void Dispose() - { - if (upgraded) - _lock.ExitWriteLock(); - else - _lock.ExitReadLock(); - } - - public void Upgrade() - { - _lock.ExitReadLock(); - _lock.EnterWriteLock(); - upgraded = true; - } } - - #endregion - - #region Nested type: WriteLockToken - - private class WriteLockToken : IDisposable - { - private readonly ReaderWriterLockSlim _lock; - - public WriteLockToken(ReaderWriterLockSlim @lock) - { - _lock = @lock; - _lock.EnterWriteLock(); - } - - public void Dispose() - { - _lock.EnterWriteLock(); - } - } - - #endregion } -} \ No newline at end of file +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |