From: <jer...@us...> - 2008-04-25 17:26:17
|
Revision: 81 http://structuremap.svn.sourceforge.net/structuremap/?rev=81&view=rev Author: jeremydmiller Date: 2008-04-25 10:26:13 -0700 (Fri, 25 Apr 2008) Log Message: ----------- introducing the BuildPolicy refactoring Modified Paths: -------------- trunk/Source/StructureMap/Configuration/NormalGraphBuilder.cs trunk/Source/StructureMap/Configuration/ProfileAndMachineParser.cs trunk/Source/StructureMap/Emitting/InstanceBuilderAssembly.cs trunk/Source/StructureMap/Emitting/Parameters/ChildParameterEmitter.cs trunk/Source/StructureMap/Graph/InstanceDefaultManager.cs trunk/Source/StructureMap/Graph/InterceptionChain.cs trunk/Source/StructureMap/Graph/PluginFamily.cs trunk/Source/StructureMap/Graph/PluginFamilyCollection.cs trunk/Source/StructureMap/Graph/PluginGraph.cs trunk/Source/StructureMap/InstanceBuilder.cs trunk/Source/StructureMap/InstanceFactory.cs trunk/Source/StructureMap/InstanceMemento.cs trunk/Source/StructureMap/Interceptors/IInterceptorChainBuilder.cs trunk/Source/StructureMap/Interceptors/InterceptorChainBuilder.cs trunk/Source/StructureMap/Pipeline/ConfiguredInstance.cs trunk/Source/StructureMap/Pipeline/IConfiguredInstance.cs trunk/Source/StructureMap/Pipeline/Instance.cs trunk/Source/StructureMap/StructureMap.csproj trunk/Source/StructureMap.Testing/Configuration/NormalGraphBuilderTester.cs trunk/Source/StructureMap.Testing/Container/EmittingTester.cs trunk/Source/StructureMap.Testing/DataAccess/Debugging.cs trunk/Source/StructureMap.Testing/Pipeline/InstanceTester.cs trunk/Source/StructureMap.Testing/StructureMap.Testing.csproj trunk/Source/StructureMap.Testing.Widget/ArrayConstruction.cs trunk/Source/StructureMap.Testing.Widget/Decision.cs trunk/Source/StructureMap.Testing.Widget/Hierarchy.cs trunk/Source/StructureMap.Testing.Widget2/EnumerationCheck.cs trunk/Source/StructureMap.Testing.Widget5/BasicGridColumnInstanceBuilder.cs Added Paths: ----------- trunk/Source/StructureMap/Diagnostics/ trunk/Source/StructureMap/Diagnostics/Tokens.cs trunk/Source/StructureMap/InstanceFamily.cs trunk/Source/StructureMap/Pipeline/BuildStrategies.cs trunk/Source/StructureMap/Pipeline/HttpContextBuildPolicy.cs trunk/Source/StructureMap/Pipeline/ILocationPolicy.cs trunk/Source/StructureMap/Pipeline/Profile.cs trunk/Source/StructureMap/Pipeline/SingletonPolicy.cs trunk/Source/StructureMap/Pipeline/ThreadLocalStoragePolicy.cs trunk/Source/StructureMap.Testing/Pipeline/BuildStrategiesTester.cs trunk/Source/StructureMap.Testing/Pipeline/ThreadLocalStoragePolicyTester.cs trunk/Source/StructureMap.Testing.GenericWidgets/structuremap.snk Modified: trunk/Source/StructureMap/Configuration/NormalGraphBuilder.cs =================================================================== --- trunk/Source/StructureMap/Configuration/NormalGraphBuilder.cs 2008-04-12 13:45:08 UTC (rev 80) +++ trunk/Source/StructureMap/Configuration/NormalGraphBuilder.cs 2008-04-25 17:26:13 UTC (rev 81) @@ -83,7 +83,8 @@ if (profile == null) { - throw new StructureMapException(195, profileName, machineName); + _pluginGraph.Log.RegisterError(195, profileName, machineName); + return; } _machine = new MachineOverride(machineName, profile); @@ -155,13 +156,13 @@ public void AttachSource(TypePath pluginTypePath, MementoSource source) { - PluginFamily family = _pluginGraph.PluginFamilies[pluginTypePath]; + PluginFamily family = _pluginGraph.PluginFamilies[pluginTypePath.FindType()]; family.AddMementoSource(source); } public Plugin AddPlugin(TypePath pluginTypePath, TypePath pluginPath, string concreteKey) { - PluginFamily family = _pluginGraph.PluginFamilies[pluginTypePath]; + PluginFamily family = _pluginGraph.PluginFamilies[pluginTypePath.FindType()]; if (family == null) { string message = @@ -177,14 +178,14 @@ public SetterProperty AddSetter(TypePath pluginTypePath, string concreteKey, string setterName) { - PluginFamily family = _pluginGraph.PluginFamilies[pluginTypePath]; + PluginFamily family = _pluginGraph.PluginFamilies[pluginTypePath.FindType()]; Plugin plugin = family.Plugins[concreteKey]; return plugin.Setters.Add(setterName); } public virtual void AddInterceptor(TypePath pluginTypePath, InstanceMemento interceptorMemento) { - PluginFamily family = _pluginGraph.PluginFamilies[pluginTypePath]; + PluginFamily family = _pluginGraph.PluginFamilies[pluginTypePath.FindType()]; try { InstanceFactoryInterceptor interceptor = Modified: trunk/Source/StructureMap/Configuration/ProfileAndMachineParser.cs =================================================================== --- trunk/Source/StructureMap/Configuration/ProfileAndMachineParser.cs 2008-04-12 13:45:08 UTC (rev 80) +++ trunk/Source/StructureMap/Configuration/ProfileAndMachineParser.cs 2008-04-25 17:26:13 UTC (rev 81) @@ -1,3 +1,4 @@ +using System; using System.Xml; using StructureMap.Graph; using StructureMap.Source; @@ -4,6 +5,7 @@ namespace StructureMap.Configuration { + [Obsolete("This puppy needs to be rewritten")] public class ProfileAndMachineParser { private readonly IGraphBuilder _builder; Added: trunk/Source/StructureMap/Diagnostics/Tokens.cs =================================================================== --- trunk/Source/StructureMap/Diagnostics/Tokens.cs (rev 0) +++ trunk/Source/StructureMap/Diagnostics/Tokens.cs 2008-04-25 17:26:13 UTC (rev 81) @@ -0,0 +1,270 @@ +using System; +using System.Collections.Generic; +using System.Resources; +using StructureMap.Graph; +using StructureMap.Pipeline; + +namespace StructureMap.Diagnostics +{ + public class GraphLog + { + private List<Error> _errors = new List<Error>(); + private readonly List<Source> _sources = new List<Source>(); + private Source _currentSource; + + public void StartSource(string description) + { + Source source = new Source(description); + _sources.Add(source); + + _currentSource = source; + } + + public void RegisterError(Instance instance, int code, params object[] args) + { + Error error = new Error(code, args); + error.Instance = instance.CreateToken(); + addError(error); + } + + public void RegisterError(int code, params object[] args) + { + Error error = new Error(code, args); + addError(error); + } + + private void addError(Error error) + { + error.Source = _currentSource; + _errors.Add(error); + } + + public void AssertHasError(int errorCode, string message) + { + Error error = Error.FromMessage(errorCode, message); + if (!_errors.Contains(error)) + { + string msg = "Did not have the requested Error. Had:\n\n"; + foreach (Error err in _errors) + { + msg += err.ToString() + "\n"; + } + + throw new ApplicationException(msg); + } + } + } + + public class Source + { + private string _description; + + public Source(string description) + { + _description = description; + } + + public string Description + { + get { return _description; } + set { _description = value; } + } + } + + public class PluginType : IEquatable<PluginType> + { + private readonly List<InstanceToken> _instances = new List<InstanceToken>(); + private readonly string _typeName; + + + public PluginType(string fullName) + { + _typeName = fullName; + } + + public PluginType(TypePath path) : this(path.AssemblyQualifiedName) + { + } + + public PluginType(Type pluginType) : this(pluginType.AssemblyQualifiedName) + { + } + + public string TypeName + { + get { return _typeName; } + } + + #region IEquatable<PluginType> Members + + public bool Equals(PluginType pluginType) + { + if (pluginType == null) return false; + return Equals(_typeName, pluginType._typeName); + } + + #endregion + + public void AddInstance(InstanceToken token) + { + if (!_instances.Contains(token)) + { + _instances.Add(token); + } + } + + public override bool Equals(object obj) + { + if (ReferenceEquals(this, obj)) return true; + return Equals(obj as PluginType); + } + + public override int GetHashCode() + { + return _typeName != null ? _typeName.GetHashCode() : 0; + } + } + + public class InstanceToken : IEquatable<InstanceToken> + { + private readonly string _description; + private readonly string _name; + private PluginType _pluginType; + + public InstanceToken(string name, string description, PluginType pluginType) + { + _name = name; + _description = description; + _pluginType = pluginType; + } + + + public string Name + { + get { return _name; } + } + + public string Description + { + get { return _description; } + } + + + public PluginType PluginType + { + get { return _pluginType; } + } + + #region IEquatable<InstanceToken> Members + + public bool Equals(InstanceToken instanceToken) + { + if (instanceToken == null) return false; + if (!Equals(_name, instanceToken._name)) return false; + if (!Equals(_description, instanceToken._description)) return false; + if (!Equals(_pluginType, instanceToken._pluginType)) return false; + return true; + } + + #endregion + + public override string ToString() + { + return string.Format("Instance {0} ({1})", _name, _description); + } + + public override bool Equals(object obj) + { + if (ReferenceEquals(this, obj)) return true; + return Equals(obj as InstanceToken); + } + + public override int GetHashCode() + { + int result = _name != null ? _name.GetHashCode() : 0; + result = 29*result + (_description != null ? _description.GetHashCode() : 0); + result = 29*result + (_pluginType != null ? _pluginType.GetHashCode() : 0); + return result; + } + } + + public class Error : IEquatable<Error> + { + private int _code; + private string _message; + private string _stackTrace = string.Empty; + public InstanceToken Instance; + public PluginType PluginType; + public Source Source; + + + private Error(int code, string message) + { + _code = code; + _message = message; + } + + public Error(int errorCode, params object[] args) + { + _code = errorCode; + string template = getMessage(errorCode); + if (template == null) template = string.Empty; + + _message = string.Format(template, args); + } + + public Error(StructureMapException exception) + { + _code = exception.ErrorCode; + _message = exception.Message; + _stackTrace = exception.StackTrace; + } + + private string getMessage(int errorCode) + { + ResourceManager resources = new ResourceManager(typeof(StructureMapException)); + return resources.GetString(errorCode.ToString()); + } + + + public bool Equals(Error error) + { + if (error == null) return false; + if (_code != error._code) return false; + if (!Equals(_message, error._message)) return false; + if (!Equals(_stackTrace, error._stackTrace)) return false; + if (!Equals(Instance, error.Instance)) return false; + if (!Equals(PluginType, error.PluginType)) return false; + if (!Equals(Source, error.Source)) return false; + return true; + } + + public override bool Equals(object obj) + { + if (ReferenceEquals(this, obj)) return true; + return Equals(obj as Error); + } + + public override int GetHashCode() + { + int result = _code; + result = 29*result + (_message != null ? _message.GetHashCode() : 0); + result = 29*result + (_stackTrace != null ? _stackTrace.GetHashCode() : 0); + result = 29*result + (Instance != null ? Instance.GetHashCode() : 0); + result = 29*result + (PluginType != null ? PluginType.GetHashCode() : 0); + result = 29*result + (Source != null ? Source.GetHashCode() : 0); + return result; + } + + + public override string ToString() + { + return string.Format("Error {0} -- {1}", _code, _message); + } + + public static Error FromMessage(int code, string message) + { + return new Error(code, message); + } + } +} \ No newline at end of file Modified: trunk/Source/StructureMap/Emitting/InstanceBuilderAssembly.cs =================================================================== --- trunk/Source/StructureMap/Emitting/InstanceBuilderAssembly.cs 2008-04-12 13:45:08 UTC (rev 80) +++ trunk/Source/StructureMap/Emitting/InstanceBuilderAssembly.cs 2008-04-25 17:26:13 UTC (rev 81) @@ -42,9 +42,6 @@ private void configureClassBuilder(ClassBuilder builderClass, Plugin plugin) { builderClass.AddReadonlyStringProperty("ConcreteTypeKey", plugin.ConcreteKey, true); - builderClass.AddReadonlyStringProperty("PluginType", TypePath.GetAssemblyQualifiedName(_pluginType), true); - builderClass.AddReadonlyStringProperty("PluggedType", TypePath.GetAssemblyQualifiedName(plugin.PluggedType), - true); BuildInstanceMethod method = new BuildInstanceMethod(plugin); builderClass.AddMethod(method); Modified: trunk/Source/StructureMap/Emitting/Parameters/ChildParameterEmitter.cs =================================================================== --- trunk/Source/StructureMap/Emitting/Parameters/ChildParameterEmitter.cs 2008-04-12 13:45:08 UTC (rev 80) +++ trunk/Source/StructureMap/Emitting/Parameters/ChildParameterEmitter.cs 2008-04-25 17:26:13 UTC (rev 81) @@ -19,17 +19,20 @@ { Type parameterType = parameter.ParameterType; string parameterName = parameter.Name; - string fullName = parameterType.AssemblyQualifiedName; - - putChildObjectOnStack(ilgen, parameterName, fullName, parameterType); + putChildObjectOnStack(ilgen, parameterName, parameterType); } - private void putChildObjectOnStack(ILGenerator ilgen, string parameterName, string fullName, Type parameterType) + private void putChildObjectOnStack(ILGenerator ilgen, string parameterName, Type parameterType) { ilgen.Emit(OpCodes.Ldarg_1); ilgen.Emit(OpCodes.Ldstr, parameterName); - ilgen.Emit(OpCodes.Ldstr, fullName); + + ilgen.Emit(OpCodes.Ldtoken, parameterType); + + MethodInfo method = typeof (Type).GetMethod("GetTypeFromHandle"); + ilgen.Emit(OpCodes.Call, method); + ilgen.Emit(OpCodes.Ldarg_2); callInstanceMemento(ilgen, "GetChild"); @@ -40,8 +43,7 @@ { ilgen.Emit(OpCodes.Ldloc_0); - putChildObjectOnStack(ilgen, property.Name, property.PropertyType.AssemblyQualifiedName, - property.PropertyType); + putChildObjectOnStack(ilgen, property.Name, property.PropertyType); MethodInfo method = property.GetSetMethod(); ilgen.Emit(OpCodes.Callvirt, method); Modified: trunk/Source/StructureMap/Graph/InstanceDefaultManager.cs =================================================================== --- trunk/Source/StructureMap/Graph/InstanceDefaultManager.cs 2008-04-12 13:45:08 UTC (rev 80) +++ trunk/Source/StructureMap/Graph/InstanceDefaultManager.cs 2008-04-25 17:26:13 UTC (rev 81) @@ -8,7 +8,7 @@ /// InstanceManager for any combination of profile and machine name. /// </summary> [Serializable] - public class InstanceDefaultManager + [Obsolete] public class InstanceDefaultManager { private string _defaultProfileName = string.Empty; private List<InstanceDefault> _defaults; @@ -204,55 +204,6 @@ return answer; } - /// <summary> - /// Returns the defaults for the current machine name and the default profile - /// </summary> - /// <returns></returns> - public Profile CalculateDefaults() - { - string profileName = DefaultProfileName; - string machinceName = GetMachineName(); - - return CalculateDefaults(machinceName, profileName); - } - - /// <summary> - /// Determines ONLY overriden defaults. Used by the Deployment NAnt task to - /// filter a PluginGraph prior to deploying a subset of the StructureMap.config - /// file - /// </summary> - /// <param name="machineName"></param> - /// <param name="profileName"></param> - /// <returns></returns> - public Profile CalculateOverridenDefaults(string machineName, string profileName) - { - MachineOverride machine = GetMachineOverride(machineName); - Profile profile = findCurrentProfile(profileName); - - Profile answer = new Profile("Defaults"); - foreach (InstanceDefault instance in machine.Defaults) - { - answer.AddOverride((InstanceDefault) instance.Clone()); - } - - foreach (InstanceDefault instance in profile.Defaults) - { - answer.AddOverride((InstanceDefault) instance.Clone()); - } - - return answer; - } - - public void ClearMachineOverrides() - { - _machineOverrides.Clear(); - } - - public void ClearProfiles() - { - _profiles.Clear(); - } - public string[] GetMachineNames() { string[] names = new string[_machineOverrides.Count]; Modified: trunk/Source/StructureMap/Graph/InterceptionChain.cs =================================================================== --- trunk/Source/StructureMap/Graph/InterceptionChain.cs 2008-04-12 13:45:08 UTC (rev 80) +++ trunk/Source/StructureMap/Graph/InterceptionChain.cs 2008-04-25 17:26:13 UTC (rev 81) @@ -9,7 +9,7 @@ /// Manages a list of InstanceFactoryInterceptor's. Design-time model of an array /// of decorators to alter the InstanceFactory behavior for a PluginType. /// </summary> - public class InterceptionChain : IEnumerable<InstanceFactoryInterceptor>, IEquatable<InterceptionChain> + [Obsolete] public class InterceptionChain : IEnumerable<InstanceFactoryInterceptor>, IEquatable<InterceptionChain> { private List<InstanceFactoryInterceptor> _interceptorList; Modified: trunk/Source/StructureMap/Graph/PluginFamily.cs =================================================================== --- trunk/Source/StructureMap/Graph/PluginFamily.cs 2008-04-12 13:45:08 UTC (rev 80) +++ trunk/Source/StructureMap/Graph/PluginFamily.cs 2008-04-25 17:26:13 UTC (rev 81) @@ -42,6 +42,7 @@ private Type _pluginType; private string _pluginTypeName; private List<Instance> _instances = new List<Instance>(); + private IBuildPolicy _buildPolicy = new BuildPolicy(); #region constructors @@ -121,6 +122,7 @@ PluginFamily templatedFamily = new PluginFamily(templatedType); templatedFamily._defaultKey = _defaultKey; templatedFamily.Parent = Parent; + templatedFamily._buildPolicy = _buildPolicy.Clone(); foreach (InstanceFactoryInterceptor interceptor in _interceptionChain) { @@ -286,6 +288,11 @@ set { _canUseUnMarkedPlugins = value; } } + public IBuildPolicy Policy + { + get { return _buildPolicy; } + } + #endregion public Instance[] GetAllInstances() @@ -293,7 +300,8 @@ List<Instance> list = new List<Instance>(); foreach (InstanceMemento memento in _mementoList) { - list.Add(memento.ReadInstance(Parent, _pluginType)); + Instance instance = memento.ReadInstance(Parent, _pluginType); + list.Add(instance); } list.AddRange(_instances); @@ -305,5 +313,6 @@ { return _instances.Find(delegate(Instance i) { return i.Name == name; }); } + } } \ No newline at end of file Modified: trunk/Source/StructureMap/Graph/PluginFamilyCollection.cs =================================================================== --- trunk/Source/StructureMap/Graph/PluginFamilyCollection.cs 2008-04-12 13:45:08 UTC (rev 80) +++ trunk/Source/StructureMap/Graph/PluginFamilyCollection.cs 2008-04-25 17:26:13 UTC (rev 81) @@ -10,7 +10,7 @@ public class PluginFamilyCollection : IEnumerable<PluginFamily> { private readonly PluginGraph _pluginGraph; - private Dictionary<Type, PluginFamily> _pluginFamilies; + private readonly Dictionary<Type, PluginFamily> _pluginFamilies; public PluginFamilyCollection(PluginGraph pluginGraph) { @@ -32,22 +32,6 @@ } - public PluginFamily this[TypePath pluginTypePath] - { - get - { - foreach (KeyValuePair<Type, PluginFamily> pair in _pluginFamilies) - { - if (pluginTypePath.Matches(pair.Key)) - { - return pair.Value; - } - } - - return null; - } - } - public PluginFamily this[int index] { get Modified: trunk/Source/StructureMap/Graph/PluginGraph.cs =================================================================== --- trunk/Source/StructureMap/Graph/PluginGraph.cs 2008-04-12 13:45:08 UTC (rev 80) +++ trunk/Source/StructureMap/Graph/PluginGraph.cs 2008-04-25 17:26:13 UTC (rev 81) @@ -4,6 +4,7 @@ using System.Diagnostics; using System.Reflection; using StructureMap.Configuration.DSL; +using StructureMap.Diagnostics; using StructureMap.Interceptors; namespace StructureMap.Graph @@ -21,7 +22,8 @@ private readonly InterceptorLibrary _interceptorLibrary = new InterceptorLibrary(); private readonly PluginFamilyCollection _pluginFamilies; private bool _sealed = false; - private bool _useExternalRegistries = true; + private readonly bool _useExternalRegistries = true; + private readonly GraphLog _log = new GraphLog(); /// <summary> /// Default constructor @@ -48,6 +50,12 @@ get { return _pluginFamilies; } } + + public GraphLog Log + { + get { return _log; } + } + #region seal public bool IsSealed @@ -183,6 +191,7 @@ throw new StructureMapException(300, fullName); } + [Obsolete] public void ReadDefaults() { _defaultManager.ReadDefaultsFromPluginGraph(this); Modified: trunk/Source/StructureMap/InstanceBuilder.cs =================================================================== --- trunk/Source/StructureMap/InstanceBuilder.cs 2008-04-12 13:45:08 UTC (rev 80) +++ trunk/Source/StructureMap/InstanceBuilder.cs 2008-04-25 17:26:13 UTC (rev 81) @@ -15,16 +15,8 @@ { } - public abstract string PluginType { get; } - public abstract string PluggedType { get; } public abstract string ConcreteTypeKey { get; } public abstract object BuildInstance(IConfiguredInstance instance, StructureMap.Pipeline.IInstanceCreator creator); - - public bool IsType(Type type) - { - Type plugged = Type.GetType(PluggedType); - return plugged.Equals(type); - } } } \ No newline at end of file Modified: trunk/Source/StructureMap/InstanceFactory.cs =================================================================== --- trunk/Source/StructureMap/InstanceFactory.cs 2008-04-12 13:45:08 UTC (rev 80) +++ trunk/Source/StructureMap/InstanceFactory.cs 2008-04-25 17:26:13 UTC (rev 81) @@ -2,11 +2,9 @@ using System.Collections; using System.Collections.Generic; using System.Data; -using StructureMap.Configuration.Mementos; using StructureMap.Graph; using StructureMap.Interceptors; using StructureMap.Pipeline; -using StructureMap.Source; namespace StructureMap { @@ -16,11 +14,12 @@ public class InstanceFactory : IInstanceFactory { private readonly InstanceBuilderList _instanceBuilders; + private readonly Dictionary<string, Instance> _instances = new Dictionary<string, Instance>(); private readonly InstanceInterceptor _interceptor = new NulloInterceptor(); private readonly Type _pluginType; - private InstanceManager _manager = new InstanceManager(); - private readonly Dictionary<string, Instance> _instances = new Dictionary<string, Instance>(); private Instance _defaultInstance; + private InstanceManager _manager = new InstanceManager(); + private IBuildPolicy _policy = new BuildPolicy(); #region static constructors @@ -54,6 +53,7 @@ try { _interceptor = family.InstanceInterceptor; + _policy = family.Policy; _pluginType = family.PluginType; _instanceBuilders = new InstanceBuilderList(family.PluginType, family.Plugins.All); @@ -75,11 +75,6 @@ } } - public static InstanceFactory CreateInstanceFactoryForType(Type concreteType) - { - return new InstanceFactory(concreteType); - } - private InstanceFactory(Type concreteType) { _interceptor = new NulloInterceptor(); @@ -94,7 +89,7 @@ Plugin plugin = new Plugin(new TypePath(concreteType), Guid.NewGuid().ToString()); if (plugin.CanBeAutoFilled) { - _instanceBuilders = new InstanceBuilderList(_pluginType, new Plugin[]{plugin}); + _instanceBuilders = new InstanceBuilderList(_pluginType, new Plugin[] {plugin}); ConfiguredInstance instance = new ConfiguredInstance(); instance.PluggedType = concreteType; @@ -106,6 +101,10 @@ } } + public static InstanceFactory CreateInstanceFactoryForType(Type concreteType) + { + return new InstanceFactory(concreteType); + } private void determineDefaultKey(PluginFamily family, bool failOnException) @@ -148,51 +147,6 @@ #endregion - - #region IInstanceCreator Members - - // TODO: This code needs to move somewhere else - //object IInstanceCreator.BuildInstance(InstanceMemento memento) - //{ - // InstanceBuilder builder = memento.FindBuilder(_instanceBuilders); - - // if (builder == null) - // { - // throw new StructureMapException( - // 201, memento.ConcreteKey, memento.InstanceKey, PluginType.FullName); - // } - - // try - // { - // object constructedInstance = builder.BuildInstance(memento, _manager); - // InstanceInterceptor interceptor = _manager.FindInterceptor(constructedInstance.GetType()); - // return interceptor.Process(constructedInstance); - // } - // catch (StructureMapException) - // { - // throw; - // } - // catch (InvalidCastException ex) - // { - // throw new StructureMapException(206, ex, memento.InstanceKey); - // } - // catch (Exception ex) - // { - // throw new StructureMapException(207, ex, memento.InstanceKey, PluginType.FullName); - // } - //} - - //InstanceMemento IInstanceCreator.DefaultMemento - //{ - // get - // { - // throw new NotImplementedException(); - // //return _source.DefaultMemento; - // } - //} - - #endregion - #region IInstanceFactory Members /// <summary> @@ -227,7 +181,7 @@ Instance instance = _instances[instanceKey]; - return instance.Build(_pluginType, _manager); + return _policy.Build(_manager, PluginType, instance); } @@ -268,14 +222,14 @@ /// Builds a new instance of the default instance of the PluginType /// </summary> /// <returns></returns> - public object GetInstance() + [Obsolete("Want to remove this eventually")] public object GetInstance() { if (_defaultInstance == null) { throw new StructureMapException(202, PluginType.FullName); } - object builtObject = _defaultInstance.Build(_pluginType, _manager); + object builtObject = _policy.Build(_manager, PluginType, _defaultInstance); return _interceptor.Process(builtObject); } @@ -315,10 +269,7 @@ /// </summary> public string DefaultInstanceKey { - get - { - return _defaultInstance == null ? string.Empty : _defaultInstance.Name; - } + get { return _defaultInstance == null ? string.Empty : _defaultInstance.Name; } } public IList GetAllInstances() @@ -327,7 +278,7 @@ foreach (KeyValuePair<string, Instance> pair in _instances) { - object instance = pair.Value.Build(_pluginType, _manager); + object instance = _policy.Build(_manager, PluginType, pair.Value); list.Add(instance); } @@ -353,8 +304,8 @@ ConfiguredInstance instance = new ConfiguredInstance(); instance.ConcreteKey = builder.ConcreteTypeKey; instance.Name = instance.ConcreteKey; - + return instance; } @@ -370,6 +321,5 @@ } #endregion - } } \ No newline at end of file Added: trunk/Source/StructureMap/InstanceFamily.cs =================================================================== --- trunk/Source/StructureMap/InstanceFamily.cs (rev 0) +++ trunk/Source/StructureMap/InstanceFamily.cs 2008-04-25 17:26:13 UTC (rev 81) @@ -0,0 +1,11 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace StructureMap +{ + public class InstanceFamily + { + + } +} Modified: trunk/Source/StructureMap/InstanceMemento.cs =================================================================== --- trunk/Source/StructureMap/InstanceMemento.cs 2008-04-12 13:45:08 UTC (rev 80) +++ trunk/Source/StructureMap/InstanceMemento.cs 2008-04-25 17:26:13 UTC (rev 81) @@ -27,19 +27,7 @@ get { return innerConcreteKey; } } - [Obsolete] public InstanceBuilder FindBuilder(InstanceBuilderList builders) - { - if (string.IsNullOrEmpty(innerConcreteKey)) - { - string pluggedTypeName = getPluggedType(); - Type pluggedType = TypePath.GetTypePath(pluggedTypeName).FindType(); - return builders.FindByType(pluggedType); - } - - return builders.FindByConcreteKey(innerConcreteKey); - } - public virtual Plugin FindPlugin(PluginFamily family) { if (string.IsNullOrEmpty(innerConcreteKey)) @@ -97,14 +85,6 @@ } /// <summary> - /// Returns the last key/value retrieved for exception tracing - /// </summary> - public string LastKey - { - get { return _lastKey; } - } - - /// <summary> /// Template pattern property specifying whether the InstanceMemento is simply a reference /// to another named instance. Useful for child objects. /// </summary> @@ -216,37 +196,8 @@ //return returnValue; } - private static object buildDefaultChild(string key, StructureMap.Pipeline.IInstanceCreator manager, string typeName) - { - object returnValue; - try - { - returnValue = manager.CreateInstance(typeName); - } - catch (StructureMapException) - { - throw; - } - catch (Exception ex) - { - throw new StructureMapException(209, ex, key, typeName); - } - return returnValue; - } - /// <summary> - /// Not used yet. - /// </summary> - /// <param name="Key"></param> - /// <returns></returns> - public string[] GetStringArray(string Key) - { - string _value = GetProperty(Key); - return _value.Split(new char[] {','}); - } - - /// <summary> /// This method is made public for testing. It is not necessary for normal usage. /// </summary> /// <returns></returns> @@ -276,6 +227,7 @@ { Instance instance = readInstance(pluginGraph, pluginType); instance.Name = InstanceKey; + instance.PluginType = pluginType; return instance; } Modified: trunk/Source/StructureMap/Interceptors/IInterceptorChainBuilder.cs =================================================================== --- trunk/Source/StructureMap/Interceptors/IInterceptorChainBuilder.cs 2008-04-12 13:45:08 UTC (rev 80) +++ trunk/Source/StructureMap/Interceptors/IInterceptorChainBuilder.cs 2008-04-25 17:26:13 UTC (rev 81) @@ -1,3 +1,4 @@ +using System; using StructureMap.Attributes; using StructureMap.Graph; @@ -3,6 +4,8 @@ namespace StructureMap.Interceptors { + [Obsolete] public interface IInterceptorChainBuilder { + [Obsolete] InterceptionChain Build(InstanceScope scope); } Modified: trunk/Source/StructureMap/Interceptors/InterceptorChainBuilder.cs =================================================================== --- trunk/Source/StructureMap/Interceptors/InterceptorChainBuilder.cs 2008-04-12 13:45:08 UTC (rev 80) +++ trunk/Source/StructureMap/Interceptors/InterceptorChainBuilder.cs 2008-04-25 17:26:13 UTC (rev 81) @@ -1,3 +1,4 @@ +using System; using StructureMap.Attributes; using StructureMap.Graph; @@ -3,4 +4,5 @@ namespace StructureMap.Interceptors { + [Obsolete] public class InterceptorChainBuilder : IInterceptorChainBuilder { @@ -11,6 +13,7 @@ #region IInterceptorChainBuilder Members + [Obsolete] public InterceptionChain Build(InstanceScope scope) { InterceptionChain returnValue = new InterceptionChain(); Added: trunk/Source/StructureMap/Pipeline/BuildStrategies.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/BuildStrategies.cs (rev 0) +++ trunk/Source/StructureMap/Pipeline/BuildStrategies.cs 2008-04-25 17:26:13 UTC (rev 81) @@ -0,0 +1,116 @@ +using System; + +namespace StructureMap.Pipeline +{ + public interface IBuildPolicy + { + object Build(IInstanceCreator instanceCreator, Type pluginType, Instance instance); + IBuildPolicy Clone(); + } + + public class BuildPolicy : IBuildPolicy + { + #region IBuildPolicy Members + + public object Build(IInstanceCreator instanceCreator, Type pluginType, Instance instance) + { + return instance.Build(pluginType, instanceCreator); + } + + public IBuildPolicy Clone() + { + return this; + } + + #endregion + } + + public interface IInstanceInterceptor : IBuildPolicy + { + IBuildPolicy InnerPolicy { get; set; } + } + + public abstract class CacheInterceptor : IInstanceInterceptor + { + private readonly object _locker = new object(); + private IBuildPolicy _innerPolicy = new BuildPolicy(); + + #region IInstanceInterceptor Members + + public IBuildPolicy InnerPolicy + { + get { return _innerPolicy; } + set { _innerPolicy = value; } + } + + + public object Build(IInstanceCreator instanceCreator, Type pluginType, Instance instance) + { + if (!isCached(instance.Name, instance.PluginType)) + { + lock (_locker) + { + if (!isCached(instance.Name, pluginType)) + { + object returnValue = _innerPolicy.Build(instanceCreator, pluginType, instance); + storeInCache(instance.Name, pluginType, returnValue); + } + } + } + + return retrieveFromCache(instance.Name, pluginType); + } + + public IBuildPolicy Clone() + { + CacheInterceptor clonedCache = clone(); + clonedCache.InnerPolicy = _innerPolicy.Clone(); + + return clonedCache; + } + + protected abstract CacheInterceptor clone(); + + #endregion + + protected abstract void storeInCache(string instanceKey, Type pluginType, object instance); + protected abstract bool isCached(string instanceKey, Type pluginType); + protected abstract object retrieveFromCache(string instanceKey, Type pluginType); + } + + public class HybridBuildPolicy : IInstanceInterceptor + { + private readonly IInstanceInterceptor _innerInterceptor; + + + public HybridBuildPolicy() + { + _innerInterceptor = HttpContextBuildPolicy.HasContext() + ? (IInstanceInterceptor) new HttpContextBuildPolicy() + : new ThreadLocalStoragePolicy(); + } + + #region IInstanceInterceptor Members + + public IBuildPolicy InnerPolicy + { + get { return _innerInterceptor.InnerPolicy; } + set { _innerInterceptor.InnerPolicy = value; } + } + + public object Build(IInstanceCreator instanceCreator, Type pluginType, Instance instance) + { + return _innerInterceptor.Build(instanceCreator, pluginType, instance); + } + + public IBuildPolicy Clone() + { + HybridBuildPolicy policy = new HybridBuildPolicy(); + policy.InnerPolicy = InnerPolicy.Clone(); + + return policy; + } + + #endregion + } +} \ No newline at end of file Modified: trunk/Source/StructureMap/Pipeline/ConfiguredInstance.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/ConfiguredInstance.cs 2008-04-12 13:45:08 UTC (rev 80) +++ trunk/Source/StructureMap/Pipeline/ConfiguredInstance.cs 2008-04-25 17:26:13 UTC (rev 81) @@ -73,10 +73,8 @@ return _properties[propertyName]; } - object IConfiguredInstance.GetChild(string propertyName, string typeName, IInstanceCreator instanceCreator) + object IConfiguredInstance.GetChild(string propertyName, Type pluginType, IInstanceCreator instanceCreator) { - Type pluginType = Type.GetType(typeName); - return getChild(propertyName, pluginType, instanceCreator); } Added: trunk/Source/StructureMap/Pipeline/HttpContextBuildPolicy.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/HttpContextBuildPolicy.cs (rev 0) +++ trunk/Source/StructureMap/Pipeline/HttpContextBuildPolicy.cs 2008-04-25 17:26:13 UTC (rev 81) @@ -0,0 +1,38 @@ +using System; +using System.Web; + +namespace StructureMap.Pipeline +{ + public class HttpContextBuildPolicy : CacheInterceptor + { + public static bool HasContext() + { + return HttpContext.Current != null; + } + + protected override void storeInCache(string instanceKey, Type pluginType, object instance) + { + HttpContext.Current.Items.Add(getKey(instanceKey, pluginType), instance); + } + + protected override bool isCached(string instanceKey, Type pluginType) + { + return HttpContext.Current.Items.Contains(getKey(instanceKey, pluginType)); + } + + protected override object retrieveFromCache(string instanceKey, Type pluginType) + { + return HttpContext.Current.Items[getKey(instanceKey, pluginType)]; + } + + private static string getKey(string instanceKey, Type pluginType) + { + return string.Format("{0}:{1}", pluginType.AssemblyQualifiedName, instanceKey); + } + + protected override CacheInterceptor clone() + { + return this; + } + } +} \ No newline at end of file Modified: trunk/Source/StructureMap/Pipeline/IConfiguredInstance.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/IConfiguredInstance.cs 2008-04-12 13:45:08 UTC (rev 80) +++ trunk/Source/StructureMap/Pipeline/IConfiguredInstance.cs 2008-04-25 17:26:13 UTC (rev 81) @@ -1,3 +1,5 @@ +using System; + namespace StructureMap.Pipeline { public interface IConfiguredInstance @@ -4,7 +6,7 @@ { Instance[] GetChildrenArray(string propertyName); string GetProperty(string propertyName); - object GetChild(string propertyName, string typeName, IInstanceCreator instanceCreator); + object GetChild(string propertyName, Type pluginType, IInstanceCreator instanceCreator); InstanceBuilder FindBuilder(InstanceBuilderList builders); string ConcreteKey Added: trunk/Source/StructureMap/Pipeline/ILocationPolicy.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/ILocationPolicy.cs (rev 0) +++ trunk/Source/StructureMap/Pipeline/ILocationPolicy.cs 2008-04-25 17:26:13 UTC (rev 81) @@ -0,0 +1,19 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace StructureMap.Pipeline +{ + public interface ILocationPolicy + { + object Build(IInstanceCreator creator, Instance instance); + } + + //public class DefaultPolicy : ILocationPolicy + //{ + // public object Build(IInstanceCreator creator, Instance instance) + // { + // return instance.Build(creator); + // } + //} +} Modified: trunk/Source/StructureMap/Pipeline/Instance.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/Instance.cs 2008-04-12 13:45:08 UTC (rev 80) +++ trunk/Source/StructureMap/Pipeline/Instance.cs 2008-04-25 17:26:13 UTC (rev 81) @@ -1,6 +1,7 @@ using System; using System.Data; using System.Web.UI; +using StructureMap.Diagnostics; using StructureMap.Graph; using StructureMap.Interceptors; @@ -26,6 +27,7 @@ { private string _name = Guid.NewGuid().ToString(); private InstanceInterceptor _interceptor = new NulloInterceptor(); + private Type _pluginType = typeof(object); public string Name { @@ -39,6 +41,13 @@ set { _interceptor = value; } } + internal Type PluginType + { + get { return _pluginType; } + set { _pluginType = value; } + } + + // TODO : remove pluginType from signature public virtual object Build(Type pluginType, IInstanceCreator creator) { object rawValue = build(pluginType, creator); @@ -69,6 +78,11 @@ { return true; } + + public InstanceToken CreateToken() + { + throw new NotImplementedException(); + } } public abstract class ExpressedInstance<T> : Instance Added: trunk/Source/StructureMap/Pipeline/Profile.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/Profile.cs (rev 0) +++ trunk/Source/StructureMap/Pipeline/Profile.cs 2008-04-25 17:26:13 UTC (rev 81) @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace StructureMap.Pipeline +{ + //public class Profile + //{ + // private readonly Dictionary<Type, Instance> _instances = new Dictionary<Type, Instance>(); + + + //} +} Added: trunk/Source/StructureMap/Pipeline/SingletonPolicy.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/SingletonPolicy.cs (rev 0) +++ trunk/Source/StructureMap/Pipeline/SingletonPolicy.cs 2008-04-25 17:26:13 UTC (rev 81) @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; + +namespace StructureMap.Pipeline +{ + public class SingletonPolicy : CacheInterceptor + { + private readonly Dictionary<string, object> _instances = new Dictionary<string, object>(); + + protected override void storeInCache(string instanceKey, Type pluginType, object instance) + { + _instances.Add(instanceKey, instance); + } + + protected override bool isCached(string instanceKey, Type pluginType) + { + return _instances.ContainsKey(instanceKey); + } + + protected override object retrieveFromCache(string instanceKey, Type pluginType) + { + return _instances[instanceKey]; + } + + protected override CacheInterceptor clone() + { + return new SingletonPolicy(); + } + } +} \ No newline at end of file Added: trunk/Source/StructureMap/Pipeline/ThreadLocalStoragePolicy.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/ThreadLocalStoragePolicy.cs (rev 0) +++ trunk/Source/StructureMap/Pipeline/ThreadLocalStoragePolicy.cs 2008-04-25 17:26:13 UTC (rev 81) @@ -0,0 +1,52 @@ +using System; +using System.Collections.Generic; + +namespace StructureMap.Pipeline +{ + public class ThreadLocalStoragePolicy : CacheInterceptor + { + [ThreadStatic] + private static Dictionary<string, object> _instances; + private object _locker = new object(); + + private void guaranteeHashExists() + { + if (_instances == null) + { + lock (_locker) + { + if (_instances == null) + { + _instances = new Dictionary<string, object>(); + } + } + } + } + + protected override void storeInCache(string instanceKey, Type pluginType, object instance) + { + _instances.Add(getKey(instanceKey, pluginType), instance); + } + + protected override bool isCached(string instanceKey, Type pluginType) + { + guaranteeHashExists(); + return _instances.ContainsKey(getKey(instanceKey, pluginType)); + } + + protected override object retrieveFromCache(string instanceKey, Type pluginType) + { + return _instances[getKey(instanceKey, pluginType)]; + } + + private string getKey(string instanceKey, Type pluginType) + { + return string.Format("{0}:{1}", pluginType.AssemblyQualifiedName, instanceKey); + } + + protected override CacheInterceptor clone() + { + return this; + } + } +} \ No newline at end of file Modified: trunk/Source/StructureMap/StructureMap.csproj =================================================================== --- trunk/Source/StructureMap/StructureMap.csproj 2008-04-12 13:45:08 UTC (rev 80) +++ trunk/Source/StructureMap/StructureMap.csproj 2008-04-25 17:26:13 UTC (rev 81) @@ -115,16 +115,24 @@ <Link>CommonAssemblyInfo.cs</Link> <SubType>Code</SubType> </Compile> + <Compile Include="Diagnostics\Tokens.cs" /> <Compile Include="InstanceBuilderList.cs" /> + <Compile Include="InstanceFamily.cs" /> + <Compile Include="Pipeline\BuildStrategies.cs" /> <Compile Include="Pipeline\ConfiguredInstance.cs" /> <Compile Include="Pipeline\ConstructorInstance.cs" /> <Compile Include="Pipeline\DefaultInstance.cs" /> + <Compile Include="Pipeline\HttpContextBuildPolicy.cs" /> <Compile Include="Pipeline\IConfiguredInstance.cs" /> + <Compile Include="Pipeline\ILocationPolicy.cs" /> <Compile Include="Pipeline\Instance.cs" /> <Compile Include="Pipeline\InstanceMementoPropertyReader.cs" /> <Compile Include="Pipeline\LiteralInstance.cs" /> + <Compile Include="Pipeline\Profile.cs" /> <Compile Include="Pipeline\PrototypeInstance.cs" /> <Compile Include="Pipeline\ReferencedInstance.cs" /> + <Compile Include="Pipeline\SingletonPolicy.cs" /> + <Compile Include="Pipeline\ThreadLocalStoragePolicy.cs" /> <Compile Include="Pipeline\UserControlInstance.cs" /> <Compile Include="Properties\AssemblyInfo.cs"> <SubType>Code</SubType> @@ -443,9 +451,6 @@ <SubType>Code</SubType> </Compile> <Compile Include="StructureMapConfiguration.cs" /> - <Compile Include="XmlMapping\ConfigEditor.cs"> - <SubType>Code</SubType> - </Compile> <EmbeddedResource Include="StructureMapException.resx"> <SubType>Designer</SubType> </EmbeddedResource> Modified: trunk/Source/StructureMap.Testing/Configuration/NormalGraphBuilderTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Configuration/NormalGraphBuilderTester.cs 2008-04-12 13:45:08 UTC (rev 80) +++ trunk/Source/StructureMap.Testing/Configuration/NormalGraphBuilderTester.cs 2008-04-25 17:26:13 UTC (rev 81) @@ -71,10 +71,7 @@ Assert.IsEmpty(machine.ProfileName); } - [Test, - ExpectedException(typeof (StructureMapException), - ExpectedMessage = "StructureMap Exception Code: 195\nThe Profile some profile referenced by Machine some machine does not exist" - )] + [Test] public void AddMachineWithProfileThatDoesNotExist() { NormalGraphBuilder graphBuilder = new NormalGraphBuilder(new Registry[0]); @@ -82,6 +79,8 @@ string theProfileName = "some profile"; graphBuilder.AddMachine(theMachineName, theProfileName); + + graphBuilder.CreatePluginGraph().Log.AssertHasError(195, "The Profile some profile referenced by Machine some machine does not exist"); } [Test] Modified: trunk/Source/StructureMap.Testing/Container/EmittingTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Container/EmittingTester.cs 2008-04-12 13:45:08 UTC (rev 80) +++ trunk/Source/StructureMap.Testing/Container/EmittingTester.cs 2008-04-25 17:26:13 UTC (rev 81) @@ -128,11 +128,6 @@ } } - [Test] - public void PluginType() - { - Assert.AreEqual("StructureMap.Testing.Widget.Rule,StructureMap.Testing.Widget", builder.PluginType); - } [Test] public void String2Property() Modified: trunk/Source/StructureMap.Testing/DataAccess/Debugging.cs =================================================================== --- trunk/Source/StructureMap.Testing/DataAccess/Debugging.cs 2008-04-12 13:45:08 UTC (rev 80) +++ trunk/Source/StructureMap.Testing/DataAccess/Debugging.cs 2008-04-25 17:26:13 UTC (rev 81) @@ -1,4 +1,6 @@ +using System; using System.Diagnostics; +using System.Reflection; using NUnit.Framework; using StructureMap.DataAccess; using StructureMap.DataAccess.MSSQL; @@ -24,5 +26,17 @@ Debug.WriteLine(json); } + + [Test] + public void TryingOutTypeMethod() + { + foreach (MethodInfo info in typeof(Type).GetMethods()) + { + Debug.WriteLine(info.Name); + } + + MethodInfo method = typeof(Type).GetMethod("GetTypeFromHandle"); + Assert.IsNotNull(method); + } } } \ No newline at end of file Added: trunk/Source/StructureMap.Testing/Pipeline/BuildStrategiesTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Pipeline/BuildStrategiesTester.cs (rev 0) +++ trunk/Source/StructureMap.Testing/Pipeline/BuildStrategiesTester.cs 2008-04-25 17:26:13 UTC (rev 81) @@ -0,0 +1,62 @@ +using NUnit.Framework; +using StructureMap.Pipeline; +using StructureMap.Testing.Widget3; + +namespace StructureMap.Testing.Pipeline +{ + [TestFixture] + public class BuildStrategiesTester + { + #region Setup/Teardown + + [SetUp] + public void SetUp() + { + } + + #endregion + + [Test] + public void Singleton_build_policy() + { + SingletonPolicy policy = new SingletonPolicy(); + ConstructorInstance instance1 = new ConstructorInstance(delegate { return new ColorService("Red"); }).WithName("Red"); + ConstructorInstance instance2 = new ConstructorInstance(delegate { return new ColorService("Green"); }).WithName("Green"); + + ColorService red1 = (ColorService) policy.Build(new StubInstanceCreator(), null, instance1); + ColorService green1 = (ColorService)policy.Build(new StubInstanceCreator(), null, instance2); + ColorService red2 = (ColorService)policy.Build(new StubInstanceCreator(), null, instance1); + ColorService green2 = (ColorService)policy.Build(new StubInstanceCreator(), null, instance2); + ColorService red3 = (ColorService)policy.Build(new StubInstanceCreator(), null, instance1); + ColorService green3 = (ColorService)policy.Build(new StubInstanceCreator(), null, instance2); + + Assert.AreSame(red1, red2); + Assert.AreSame(red1, red3); + Assert.AreSame(green1, green2); + Assert.AreSame(green1, green3); + } + + [Test] + public void CloneSingleton() + { + SingletonPolicy policy = new SingletonPolicy(); + + SingletonPolicy clone = (SingletonPolicy) policy.Clone(); + Assert.AreNotSame(policy, clone); + Assert.IsInstanceOfType(typeof(BuildPolicy), clone.InnerPolicy); + } + + [Test] + public void CloneHybrid() + { + HybridBuildPolicy policy = new HybridBuildPolicy(); + + HybridBuildPolicy clone = (HybridBuildPolicy)policy.Clone(); + Assert.AreNotSame(policy, clone); + Assert.IsInstanceOfType(typeof(BuildPolicy), clone.InnerPolicy); + } + + + + } +} \ No newline at end of file Modified: trunk/Source/StructureMap.Testing/Pipeline/InstanceTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Pipeline/InstanceTester.cs 2008-04-12 13:45:08 UTC (rev 80) +++ trunk/Source/StructureMap.Testing/Pipeline/InstanceTester.cs 2008-04-25 17:26:13 UTC (rev 81) @@ -1,6 +1,7 @@ using System; using NUnit.Framework; using Rhino.Mocks; +using StructureMap.Graph; using StructureMap.Interceptors; using StructureMap.Pipeline; @@ -43,6 +44,8 @@ Assert.AreEqual(objectReturnedByInterceptor, instanceUnderTest.Build(typeof (object), instanceCreator)); } } + + } public class InstanceUnderTest : Instance Added: trunk/Source/StructureMap.Testing/Pipeline/ThreadLocalStoragePolicyTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Pipeline/ThreadLocalStoragePolicyTester.cs (rev 0) +++ trunk/Source/StructureMap.Testing/Pipeline/ThreadLocalStoragePolicyTester.cs 2008-04-25 17:26:13 UTC (rev 81) @@ -0,0 +1,87 @@ +using System.Threading; +using NUnit.Framework; +using StructureMap.Pipeline; +using StructureMap.Testing.Widget; +using StructureMap.Testing.Widget3; + +namespace StructureMap.Testing.Pipeline +{ + [TestFixture] + public class ThreadLocalStoragePolicyTester + { + #region Setup/Teardown + + [SetUp] + public void SetUp() + { + _policy = new ThreadLocalStoragePolicy(); + _instance = new ConstructorInstance(delegate() { return new ColorRule("Red"); }).WithName("Red"); + + } + + #endregion + + private ThreadLocalStoragePolicy _policy; + private ColorRule _rule1; + private ColorRule _rule2; + private ColorRule _rule3; + private ConstructorInstance _instance; + + + private void findRule1() + { + _rule1 = (ColorRule) _policy.Build(new StubInstanceCreator(), typeof(IService), _instance); + + ColorRule rule = (ColorRule)_policy.Build(new StubInstanceCreator(), typeof(IService), _instance); + Assert.AreSame(_rule1, rule); + } + + private void findRule2() + { + _rule2 = (ColorRule)_policy.Build(new StubInstanceCreator(), typeof(IService), _instance); + + ColorRule rule = (ColorRule)_policy.Build(new StubInstanceCreator(), typeof(IService), _instance); + Assert.AreSame(_rule2, rule); + } + + private void findRule3() + { + _rule3 = (ColorRule)_policy.Build(new StubInstanceCreator(), typeof(IService), _instance); + + ColorRule rule = (ColorRule)_policy.Build(new StubInstanceCreator(), typeof(IService), _instance); + Assert.AreSame(_rule3, rule); + + rule = (ColorRule)_policy.Build(new StubInstanceCreator(), typeof(IService), _instance); + Assert.AreSame(_rule3, rule); + + rule = (ColorRule)_policy.Build(new StubInstanceCreator(), typeof(IService), _instance); + Assert.AreSame(_rule3, rule); + + rule = (ColorRule)_policy.Build(new StubInstanceCreator(), typeof(IService), _instance); + Assert.AreSame(_rule3, rule); + } + + [Test] + public void FindUniqueInstancePerThread() + { + Thread t1 = new Thread(new ThreadStart(findRule1)); + Thread t2 = new Thread(new ThreadStart(findRule2)); + Thread t3 = new Thread(new ThreadStart(findRule3)); + + t1.Start(); + t2.Start(); + t3.Start(); + + t1.Join(); + t2.Join(); + t3.Join(); + + Assert.AreNotSame(_rule1, _rule2); + Assert.AreNotSame(_rule1, _rule3); + Assert.AreNotSame(_rule2, _rule3); + Assert.IsTrue(_rule1.ID != _rule2.ID); + Assert.IsTrue(_rule1.ID != _rule3.ID); + Assert.IsTrue(_rule2.ID != _rule3.ID); + } + } +} \ No newline at end of file Modified: trunk/Source/StructureMap.Testing/St... [truncated message content] |