From: <jer...@us...> - 2009-12-28 02:57:31
|
Revision: 308 http://structuremap.svn.sourceforge.net/structuremap/?rev=308&view=rev Author: jeremydmiller Date: 2009-12-28 02:57:22 +0000 (Mon, 28 Dec 2009) Log Message: ----------- enhanced the Model a bit to start being able to remove types. Ripped out that old silly Machine specific default crap. Modified Paths: -------------- trunk/Source/StructureMap/Configuration/IGraphBuilder.cs trunk/Source/StructureMap/Configuration/ProfileAndMachineParser.cs trunk/Source/StructureMap/Configuration/ProfileBuilder.cs trunk/Source/StructureMap/Configuration/XmlConstants.cs trunk/Source/StructureMap/Graph/PluginFamily.cs trunk/Source/StructureMap/IInstanceFactory.cs trunk/Source/StructureMap/InstanceFactory.cs trunk/Source/StructureMap/Pipeline/Profile.cs trunk/Source/StructureMap/Pipeline/ProfileManager.cs trunk/Source/StructureMap/PipelineGraph.cs trunk/Source/StructureMap/Query/EmptyConfiguration.cs trunk/Source/StructureMap/Query/GenericFamilyConfiguration.cs trunk/Source/StructureMap/Query/IPluginTypeConfiguration.cs trunk/Source/StructureMap/Query/InstanceFactoryTypeConfiguration.cs trunk/Source/StructureMap/Query/InstanceRef.cs trunk/Source/StructureMap.Testing/Configuration/InlineInstanceDefinitionInProfileAndMachineNodesTester.cs trunk/Source/StructureMap.Testing/Configuration/ProfileBuilderTester.cs trunk/Source/StructureMap.Testing/Pipeline/ProfileManagerTester.cs trunk/Source/StructureMap.Testing/Pipeline/ProfileTester.cs trunk/Source/StructureMap.Testing/Query/GenericFamilyConfigurationTester.cs trunk/Source/StructureMap.Testing/Query/InstanceFactoryTypeConfigurationTester.cs Modified: trunk/Source/StructureMap/Configuration/IGraphBuilder.cs =================================================================== --- trunk/Source/StructureMap/Configuration/IGraphBuilder.cs 2009-12-28 01:43:19 UTC (rev 307) +++ trunk/Source/StructureMap/Configuration/IGraphBuilder.cs 2009-12-28 02:57:22 UTC (rev 308) @@ -7,8 +7,6 @@ { void AddProfile(string profileName); void OverrideProfile(TypePath typePath, string instanceKey); - void AddMachine(string machineName, string profileName); - void OverrideMachine(TypePath typePath, string instanceKey); void SetDefaultProfileName(string profileName); } Modified: trunk/Source/StructureMap/Configuration/ProfileAndMachineParser.cs =================================================================== --- trunk/Source/StructureMap/Configuration/ProfileAndMachineParser.cs 2009-12-28 01:43:19 UTC (rev 307) +++ trunk/Source/StructureMap/Configuration/ProfileAndMachineParser.cs 2009-12-28 02:57:22 UTC (rev 308) @@ -25,6 +25,11 @@ _structureMapNode.ForAttributeValue(DEFAULT_PROFILE, profileName => _profileBuilder.SetDefaultProfileName(profileName)); + readProfileNodes(); + } + + private void readProfileNodes() + { forEachNode(PROFILE_NODE).Do(element => { string profileName = element.GetAttribute(NAME); @@ -34,19 +39,6 @@ (fullName, defaultKey) => _profileBuilder.OverrideProfile(new TypePath(fullName), defaultKey), profileName); }); - - - forEachNode(MACHINE_NODE).Do(element => - { - string machineName = element.GetAttribute(NAME); - string profileName = element.GetAttribute(PROFILE_NODE); - - _profileBuilder.AddMachine(machineName, profileName); - - writeOverrides(element, - (fullName, defaultKey) => - _profileBuilder.OverrideMachine(new TypePath(fullName), defaultKey), machineName); - }); } Modified: trunk/Source/StructureMap/Configuration/ProfileBuilder.cs =================================================================== --- trunk/Source/StructureMap/Configuration/ProfileBuilder.cs 2009-12-28 01:43:19 UTC (rev 307) +++ trunk/Source/StructureMap/Configuration/ProfileBuilder.cs 2009-12-28 02:57:22 UTC (rev 308) @@ -6,28 +6,17 @@ { public class ProfileBuilder : IProfileBuilder { - private static string _overriden_machine_name; - - private readonly string _machineName; private readonly PluginGraph _pluginGraph; private readonly ProfileManager _profileManager; private string _lastProfile; - private bool _useMachineOverrides; - public ProfileBuilder(PluginGraph pluginGraph, string machineName) + public ProfileBuilder(PluginGraph pluginGraph) { _pluginGraph = pluginGraph; _profileManager = pluginGraph.ProfileManager; - _machineName = machineName; } - - public ProfileBuilder(PluginGraph pluginGraph) - : this(pluginGraph, GetMachineName()) - { - } - #region IProfileBuilder Members public void AddProfile(string profileName) @@ -44,32 +33,6 @@ }); } - public void AddMachine(string machineName, string profileName) - { - _useMachineOverrides = machineName == _machineName; - - if (_useMachineOverrides) - { - _profileManager.DefaultMachineProfileName = profileName; - } - } - - public void OverrideMachine(TypePath typePath, string instanceKey) - { - if (!_useMachineOverrides) - { - return; - } - - _pluginGraph.Log.WithType(typePath, - "trying to configure a Machine Override", - pluginType => - { - var instance = new ReferencedInstance(instanceKey); - _profileManager.SetMachineDefault(pluginType, instance); - }); - } - public void SetDefaultProfileName(string profileName) { _profileManager.DefaultProfileName = profileName; @@ -77,33 +40,5 @@ #endregion - public static void OverrideMachineName(string machineName) - { - _overriden_machine_name = machineName; - } - - public static void ResetMachineName() - { - _overriden_machine_name = string.Empty; - } - - public static string GetMachineName() - { - if (!string.IsNullOrEmpty(_overriden_machine_name)) - { - return _overriden_machine_name; - } - - string machineName = string.Empty; - try - { - machineName = Environment.MachineName.ToUpper(); - } - finally - { - } - - return machineName; - } } } \ No newline at end of file Modified: trunk/Source/StructureMap/Configuration/XmlConstants.cs =================================================================== --- trunk/Source/StructureMap/Configuration/XmlConstants.cs 2009-12-28 01:43:19 UTC (rev 307) +++ trunk/Source/StructureMap/Configuration/XmlConstants.cs 2009-12-28 02:57:22 UTC (rev 308) @@ -18,7 +18,6 @@ public const string INSTANCE_NODE = "Instance"; public const string INTERCEPTORS_NODE = "Interceptors"; public const string KEY_ATTRIBUTE = "Key"; - public const string MACHINE_NODE = "Machine"; public const string MEMENTO_SOURCE_NODE = "Source"; public const string MEMENTO_STYLE = "MementoStyle"; public const string NAME = "Name"; Modified: trunk/Source/StructureMap/Graph/PluginFamily.cs =================================================================== --- trunk/Source/StructureMap/Graph/PluginFamily.cs 2009-12-28 01:43:19 UTC (rev 307) +++ trunk/Source/StructureMap/Graph/PluginFamily.cs 2009-12-28 02:57:22 UTC (rev 308) @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using StructureMap.Pipeline; +using StructureMap.Query; using StructureMap.TypeRules; using StructureMap.Util; @@ -350,5 +351,15 @@ public string DefaultInstanceKey { get { return _defaultKey; } set { _defaultKey = value ?? string.Empty; } } #endregion + + public void RemoveInstance(Instance instance) + { + _instances.Remove(instance.Name); + if (_defaultKey == instance.Name) + { + _defaultKey = null; + } + + } } } \ No newline at end of file Modified: trunk/Source/StructureMap/IInstanceFactory.cs =================================================================== --- trunk/Source/StructureMap/IInstanceFactory.cs 2009-12-28 01:43:19 UTC (rev 307) +++ trunk/Source/StructureMap/IInstanceFactory.cs 2009-12-28 02:57:22 UTC (rev 308) @@ -27,5 +27,6 @@ void EjectAllInstances(); IInstanceFactory Clone(); + void RemoveInstance(Instance instance); } } \ No newline at end of file Modified: trunk/Source/StructureMap/InstanceFactory.cs =================================================================== --- trunk/Source/StructureMap/InstanceFactory.cs 2009-12-28 01:43:19 UTC (rev 307) +++ trunk/Source/StructureMap/InstanceFactory.cs 2009-12-28 02:57:22 UTC (rev 308) @@ -133,6 +133,11 @@ return factory; } + public void RemoveInstance(Instance instance) + { + _instances.Remove(instance.Name); + } + #endregion public void Dispose() Modified: trunk/Source/StructureMap/Pipeline/Profile.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/Profile.cs 2009-12-28 01:43:19 UTC (rev 307) +++ trunk/Source/StructureMap/Pipeline/Profile.cs 2009-12-28 02:57:22 UTC (rev 308) @@ -114,5 +114,15 @@ { _instances.Clear(); } + + public void Remove(Type pluginType, Instance instance) + { + if (!_instances.ContainsKey(pluginType)) return; + + if (_instances[pluginType] == instance) + { + _instances.Remove(pluginType); + } + } } } \ No newline at end of file Modified: trunk/Source/StructureMap/Pipeline/ProfileManager.cs =================================================================== --- trunk/Source/StructureMap/Pipeline/ProfileManager.cs 2009-12-28 01:43:19 UTC (rev 307) +++ trunk/Source/StructureMap/Pipeline/ProfileManager.cs 2009-12-28 02:57:22 UTC (rev 308) @@ -9,14 +9,10 @@ { private readonly Profile _default = new Profile(""); private readonly object _locker = new object(); - private readonly Profile _machineProfile = new Profile("MACHINE"); private readonly Dictionary<string, Profile> _profiles = new Dictionary<string, Profile>(); private Profile _currentProfile = new Profile(string.Empty); - private string _defaultMachineProfileName; private string _defaultProfileName; - public string DefaultMachineProfileName { get { return _defaultMachineProfileName; } set { _defaultMachineProfileName = value; } } - public string DefaultProfileName { get { return _defaultProfileName; } set { _defaultProfileName = value; } } public string CurrentProfile @@ -38,18 +34,25 @@ } } - public void Dispose() + private IEnumerable<Profile> profiles { - _currentProfile.Clear(); - _machineProfile.Clear(); - _default.Clear(); - - foreach (var profile in _profiles) + get { - profile.Value.Clear(); + yield return _currentProfile; + yield return _default; + + foreach (Profile profile in _profiles.Values) + { + yield return profile; + } } } + public void Dispose() + { + profiles.Each(p => p.Clear()); + } + private void setProfile(Profile profile) { lock (_locker) @@ -83,7 +86,7 @@ { Profile profile = getProfile(profileName); profile.SetDefault(pluginType, instance); - if (profileName == _defaultProfileName || profileName == _defaultMachineProfileName) + if (profileName == _defaultProfileName) { _default.FillTypeInto(pluginType, instance); _currentProfile.FillTypeInto(pluginType, instance); @@ -101,16 +104,6 @@ return _profiles[profileName]; } - public Instance GetMachineDefault(Type pluginType) - { - return _machineProfile.GetDefault(pluginType); - } - - public void SetMachineDefault(Type pluginType, Instance instance) - { - _machineProfile.SetDefault(pluginType, instance); - } - public void SetDefault(Type pluginType, Instance instance) { _currentProfile.SetDefault(pluginType, instance); @@ -128,8 +121,6 @@ setProfileDefaults(graph.Log); - processMachineDefaults(graph); - findFamilyDefaults(graph); backfillProfiles(); @@ -171,28 +162,13 @@ private void findMasterInstances(PluginGraph graph) { - _machineProfile.FindMasterInstances(graph); foreach (var pair in _profiles) { pair.Value.FindMasterInstances(graph); } } - private void processMachineDefaults(PluginGraph graph) - { - _machineProfile.FillAllTypesInto(_default); - if (!string.IsNullOrEmpty(_defaultMachineProfileName)) - { - if (!_profiles.ContainsKey(_defaultMachineProfileName)) - { - graph.Log.RegisterError(280, _defaultMachineProfileName); - } - Profile profile = getProfile(_defaultMachineProfileName); - profile.FillAllTypesInto(_default); - } - } - public void CopyDefaults(Type basicType, Type templatedType, PluginFamily family) { _default.CopyDefault(basicType, templatedType, family); @@ -232,7 +208,6 @@ { var clone = new ProfileManager { - DefaultMachineProfileName = DefaultMachineProfileName, DefaultProfileName = DefaultProfileName }; @@ -249,5 +224,10 @@ pair.Value.Remove<T>(); } } + + public void RemoveInstance(Type pluginType, Instance instance) + { + profiles.Each(x => x.Remove(pluginType, instance)); + } } } \ No newline at end of file Modified: trunk/Source/StructureMap/PipelineGraph.cs =================================================================== --- trunk/Source/StructureMap/PipelineGraph.cs 2009-12-28 01:43:19 UTC (rev 307) +++ trunk/Source/StructureMap/PipelineGraph.cs 2009-12-28 02:57:22 UTC (rev 308) @@ -30,18 +30,13 @@ _profileManager = graph.ProfileManager; _log = graph.Log; - foreach (PluginFamily family in graph.PluginFamilies) + + graph.PluginFamilies.Where(x => x.IsGenericTemplate).Each(_genericsGraph.AddFamily); + graph.PluginFamilies.Where(x => !x.IsGenericTemplate).Each(family => { - if (family.IsGenericTemplate) - { - _genericsGraph.AddFamily(family); - } - else - { - var factory = new InstanceFactory(family); - _factories.Add(family.PluginType, factory); - } - } + var factory = new InstanceFactory(family); + _factories.Add(family.PluginType, factory); + }); } private PipelineGraph(ProfileManager profileManager, GenericsPluginGraph genericsGraph, GraphLog log) @@ -231,5 +226,11 @@ ? _transientCache : lifecycle.FindCache(); } + + public void Remove(Type pluginType, Instance instance) + { + ForType(pluginType).RemoveInstance(instance); + _profileManager.RemoveInstance(pluginType, instance); + } } } \ No newline at end of file Modified: trunk/Source/StructureMap/Query/EmptyConfiguration.cs =================================================================== --- trunk/Source/StructureMap/Query/EmptyConfiguration.cs 2009-12-28 01:43:19 UTC (rev 307) +++ trunk/Source/StructureMap/Query/EmptyConfiguration.cs 2009-12-28 02:57:22 UTC (rev 308) @@ -39,5 +39,10 @@ { return false; } + + public void EjectAndRemove(InstanceRef instance) + { + } + } } \ No newline at end of file Modified: trunk/Source/StructureMap/Query/GenericFamilyConfiguration.cs =================================================================== --- trunk/Source/StructureMap/Query/GenericFamilyConfiguration.cs 2009-12-28 01:43:19 UTC (rev 307) +++ trunk/Source/StructureMap/Query/GenericFamilyConfiguration.cs 2009-12-28 02:57:22 UTC (rev 308) @@ -53,7 +53,7 @@ /// All of the <see cref="InstanceRef">InstanceRef</see>'s registered /// for this PluginType /// </summary> - public IEnumerable<InstanceRef> Instances { get { return _family.Instances.Select(x => new InstanceRef(x, this)); } } + public IEnumerable<InstanceRef> Instances { get { return _family.Instances.Select(x => new InstanceRef(x, this)).ToArray(); } } /// <summary> /// Simply query to see if there are any implementations registered @@ -63,5 +63,13 @@ { return _family.InstanceCount > 0; } + + public void EjectAndRemove(InstanceRef instance) + { + if (instance == null) return; + _family.RemoveInstance(instance.Instance); + } + + } } \ No newline at end of file Modified: trunk/Source/StructureMap/Query/IPluginTypeConfiguration.cs =================================================================== --- trunk/Source/StructureMap/Query/IPluginTypeConfiguration.cs 2009-12-28 01:43:19 UTC (rev 307) +++ trunk/Source/StructureMap/Query/IPluginTypeConfiguration.cs 2009-12-28 02:57:22 UTC (rev 308) @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Linq; namespace StructureMap.Query { @@ -29,5 +30,42 @@ /// </summary> /// <returns></returns> bool HasImplementations(); + + /// <summary> + /// Ejects any instances of this instance from the current container + /// and permanently removes the instance from the container configuration + /// </summary> + /// <param name="instance"></param> + void EjectAndRemove(InstanceRef instance); + } + + public static class PluginTypeConfigurationExtensions + { + public static InstanceRef Find(this IPluginTypeConfiguration configuration, string instanceName) + { + return configuration.Instances.FirstOrDefault(x => x.Name == instanceName); + } + + /// <summary> + /// Ejects and removes all objects and the configuration for the named instance from the + /// container + /// </summary> + /// <param name="configuration"></param> + /// <param name="instanceName"></param> + public static void EjectAndRemove(this IPluginTypeConfiguration configuration, string instanceName) + { + configuration.EjectAndRemove(configuration.Find(instanceName)); + } + + /// <summary> + /// Ejects and removes all objects and configuration for the instances that match the filter + /// </summary> + /// <param name="configuration"></param> + /// <param name="filter"></param> + public static void EjectAndRemove(this IPluginTypeConfiguration configuration, Func<InstanceRef, bool> filter) + { + configuration.Instances.Where(filter).Each(configuration.EjectAndRemove); + } + } } \ No newline at end of file Modified: trunk/Source/StructureMap/Query/InstanceFactoryTypeConfiguration.cs =================================================================== --- trunk/Source/StructureMap/Query/InstanceFactoryTypeConfiguration.cs 2009-12-28 01:43:19 UTC (rev 307) +++ trunk/Source/StructureMap/Query/InstanceFactoryTypeConfiguration.cs 2009-12-28 02:57:22 UTC (rev 308) @@ -63,7 +63,7 @@ /// All of the <see cref="InstanceRef">InstanceRef</see>'s registered /// for this PluginType /// </summary> - public IEnumerable<InstanceRef> Instances { get { return factory.AllInstances.Select(x => toRef(x)); } } + public IEnumerable<InstanceRef> Instances { get { return factory.AllInstances.Select(x => toRef(x)).ToArray(); } } /// <summary> /// Simply query to see if there are any implementations registered @@ -74,6 +74,13 @@ return factory.AllInstances.Any(); } + public void EjectAndRemove(InstanceRef instance) + { + instance.EjectObject(); + _graph.Remove(_pluginType, instance.Instance); + } + + private InstanceRef toRef(Instance instance) { if (instance == null) return null; Modified: trunk/Source/StructureMap/Query/InstanceRef.cs =================================================================== --- trunk/Source/StructureMap/Query/InstanceRef.cs 2009-12-28 01:43:19 UTC (rev 307) +++ trunk/Source/StructureMap/Query/InstanceRef.cs 2009-12-28 02:57:22 UTC (rev 308) @@ -14,6 +14,7 @@ _family = family; } + internal Instance Instance { get { return _instance; } } public string Name { get { return _instance.Name; } } Modified: trunk/Source/StructureMap.Testing/Configuration/InlineInstanceDefinitionInProfileAndMachineNodesTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Configuration/InlineInstanceDefinitionInProfileAndMachineNodesTester.cs 2009-12-28 01:43:19 UTC (rev 307) +++ trunk/Source/StructureMap.Testing/Configuration/InlineInstanceDefinitionInProfileAndMachineNodesTester.cs 2009-12-28 02:57:22 UTC (rev 308) @@ -53,7 +53,6 @@ public void TearDown() { - ProfileBuilder.ResetMachineName(); } [Test] @@ -75,26 +74,6 @@ } [Test] - public void GettingTheRightInstanceKeyWhenUsingAMAchineOverrideInCombinationWithProfile() - { - ProfileBuilder.OverrideMachineName("GREEN-BOX"); - var manager = new Container(graph); - - var widget = (ColorWidget) manager.GetInstance<IWidget>(); - Assert.AreEqual("Green", widget.Color); - } - - [Test] - public void GotTheInstanceForTheMachineOverride() - { - ProfileBuilder.OverrideMachineName("ORANGE-BOX"); - var manager = new Container(graph); - - var widget = (ColorWidget) manager.GetInstance<IWidget>(); - Assert.AreEqual("Orange", widget.Color); - } - - [Test] public void HasADefaultInstanceKey() { var manager = new Container(graph); @@ -118,28 +97,7 @@ Assert.AreEqual("Blue", widget.Color); } - [Test] - public void InlineMachine1() - { - ProfileBuilder.OverrideMachineName("ORANGE-BOX"); - var manager = new Container(graph); - - var widget = (ColorWidget) manager.GetInstance(typeof (IWidget)); - Assert.AreEqual("Orange", widget.Color); - } - - [Test] - public void InlineMachine2() - { - ProfileBuilder.OverrideMachineName("GREEN-BOX"); - var manager = new Container(graph); - - var widget = (ColorWidget) manager.GetInstance(typeof (IWidget)); - Assert.AreEqual("Green", widget.Color); - } - - [Test] public void SetTheProfile() { var manager = new Container(graph); Modified: trunk/Source/StructureMap.Testing/Configuration/ProfileBuilderTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Configuration/ProfileBuilderTester.cs 2009-12-28 01:43:19 UTC (rev 307) +++ trunk/Source/StructureMap.Testing/Configuration/ProfileBuilderTester.cs 2009-12-28 02:57:22 UTC (rev 308) @@ -14,44 +14,15 @@ public void SetUp() { _graph = new PluginGraph(); - _builder = new ProfileBuilder(_graph, THE_MACHINE_NAME); + _builder = new ProfileBuilder(_graph); } #endregion private PluginGraph _graph; private ProfileBuilder _builder; - private const string THE_MACHINE_NAME = "TheMachineName"; [Test] - public void Do_not_register_a_machine_override_if_it_is_NOT_the_matching_machine() - { - _builder.AddMachine("Some other machine", "TheProfile"); - _builder.OverrideMachine(new TypePath(GetType()), "Purple"); - - Assert.IsNull(_graph.ProfileManager.GetMachineDefault(GetType())); - } - - [Test] - public void Ignore_any_information_from_a_different_machine() - { - _builder.AddMachine("DifferentMachine", "TheProfile"); - Assert.IsTrue(string.IsNullOrEmpty(_graph.ProfileManager.DefaultMachineProfileName)); - } - - [Test] - public void Log_131_if_trying_to_register_override_for_a_machine_when_the_PluginType_cannot_be_found() - { - _builder.AddMachine(THE_MACHINE_NAME, "TheProfile"); - - _graph.Log.AssertHasNoError(131); - - _builder.OverrideMachine(new TypePath("not a real type"), "Purple"); - - _graph.Log.AssertHasError(131); - } - - [Test] public void Log_131_if_trying_to_register_override_for_a_profile_when_the_PluginType_cannot_be_found() { _builder.AddProfile("TheProfile"); @@ -80,16 +51,6 @@ } [Test] - public void Register_a_machine_override_if_it_is_the_matching_machine() - { - _builder.AddMachine(THE_MACHINE_NAME, "TheProfile"); - _builder.OverrideMachine(new TypePath(GetType()), "Purple"); - - var instance = new ReferencedInstance("Purple"); - Assert.AreEqual(instance, _graph.ProfileManager.GetMachineDefault(GetType())); - } - - [Test] public void SetDefaultProfileName() { string theProfileName = "some profile name"; @@ -99,12 +60,6 @@ } [Test] - public void smoke_test_get_machine_name() - { - ProfileBuilder.GetMachineName().ShouldNotBeEmpty(); - } - - [Test] public void Throw_280_if_requesting_an_invalid_profile() { try @@ -120,11 +75,5 @@ } } - [Test] - public void Use_the_machine_profile_name_if_the_machine_name_matches() - { - _builder.AddMachine(THE_MACHINE_NAME, "TheProfile"); - Assert.AreEqual("TheProfile", _graph.ProfileManager.DefaultMachineProfileName); - } } } \ No newline at end of file Modified: trunk/Source/StructureMap.Testing/Pipeline/ProfileManagerTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Pipeline/ProfileManagerTester.cs 2009-12-28 01:43:19 UTC (rev 307) +++ trunk/Source/StructureMap.Testing/Pipeline/ProfileManagerTester.cs 2009-12-28 02:57:22 UTC (rev 308) @@ -38,14 +38,7 @@ family.AddInstance(new ObjectInstance(0).WithName(name)); } - private void addDefaultToMachine<T>(string name) - { - ObjectInstance instance = new ObjectInstance(0).WithName(name); - PluginFamily family = _pluginGraph.FindFamily(typeof (T)); - family.AddInstance(instance); - _manager.SetMachineDefault(typeof (T), new ReferencedInstance(name)); - } private void assertNoDefaultForType<T>() { @@ -85,59 +78,6 @@ [Test] - public void DefaultMachineProfileNotSet_and_pickUp_default_from_family_machine() - { - addDefaultToProfile<ISomething>("Machine", "Red"); - addDefaultToPluginFamily<ISomething>("Blue"); - _manager.DefaultMachineProfileName = null; - - seal(); - - assertDefaultInstanceNameIs<ISomething>("Blue"); - } - - [Test] - public void Got_machine_default_machine_profile_and_family_default_the_machine_wins() - { - addDefaultToProfile<ISomething>("MachineProfile", "MachineProfile"); - addDefaultToPluginFamily<ISomething>("Family"); - addDefaultToMachine<ISomething>("Machine"); - _manager.DefaultMachineProfileName = "MachineProfile"; - - seal(); - - assertDefaultInstanceNameIs<ISomething>("Machine"); - } - - - [Test] - public void Got_profile_and_family_and_machine_and_machine_profile_so_profile_wins() - { - addDefaultToProfile<ISomething>("TheProfile", "Profile"); - addDefaultToProfile<ISomething>("TheMachineProfile", "MachineProfile"); - addDefaultToPluginFamily<ISomething>("Family"); - addDefaultToMachine<ISomething>("Machine"); - _manager.DefaultProfileName = "TheProfile"; - _manager.DefaultMachineProfileName = "TheMachineProfile"; - - seal(); - - assertDefaultInstanceNameIs<ISomething>("Profile"); - } - - [Test] - public void Got_profile_and_family_and_machine_so_profile_wins() - { - addDefaultToProfile<ISomething>("TheProfile", "Profile"); - addDefaultToPluginFamily<ISomething>("Family"); - addDefaultToMachine<ISomething>("Machine"); - _manager.DefaultProfileName = "TheProfile"; - seal(); - - assertDefaultInstanceNameIs<ISomething>("Profile"); - } - - [Test] public void Got_profile_and_family_so_profile_wins() { addDefaultToProfile<ISomething>("TheProfile", "Profile"); @@ -158,39 +98,8 @@ assertDefaultInstanceNameIs<ISomething>("Profile"); } - [Test] - public void Have_a_machine_default_and_a_base_default_the_machine_wins() - { - addDefaultToPluginFamily<ISomething>("Red"); - addDefaultToMachine<ISomething>("Blue"); - seal(); - - assertDefaultInstanceNameIs<ISomething>("Blue"); - } - [Test] - public void Have_a_machine_default_but_not_any_other_default_for_a_type() - { - addDefaultToMachine<ISomething>("Blue"); - seal(); - - assertDefaultInstanceNameIs<ISomething>("Blue"); - } - - [Test] - public void Have_machine_profile_and_default_from_family_machine_profile_wins() - { - addDefaultToProfile<ISomething>("Machine", "MachineProfile"); - addDefaultToPluginFamily<ISomething>("Family"); - _manager.DefaultMachineProfileName = "Machine"; - - seal(); - - assertDefaultInstanceNameIs<ISomething>("MachineProfile"); - } - - [Test] public void If_the_profile_is_set_and_there_is_a_default_in_that_profile_use_the_profile_default() { addDefaultToProfile<ISomething>("TheProfile", "Profile"); @@ -215,18 +124,6 @@ } [Test] - public void Log_280_if_the_machine_default_profile_cannot_be_found() - { - var manager = new ProfileManager(); - manager.DefaultMachineProfileName = "something that doesn't exist"; - - var graph = new PluginGraph(); - manager.Seal(graph); - - graph.Log.AssertHasError(280); - } - - [Test] public void Only_programmatic_override_so_use_the_programmatic_override() { _manager.SetDefault(typeof (ISomething), new ConfiguredInstance(typeof (SomethingOne)).WithName("Red")); Modified: trunk/Source/StructureMap.Testing/Pipeline/ProfileTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Pipeline/ProfileTester.cs 2009-12-28 01:43:19 UTC (rev 307) +++ trunk/Source/StructureMap.Testing/Pipeline/ProfileTester.cs 2009-12-28 02:57:22 UTC (rev 308) @@ -78,6 +78,40 @@ } [Test] + public void remove_if_the_instance_does_not_match() + { + var profile = new Profile("something"); + var instance = new ConfiguredInstance(typeof(SomethingOne)).WithName("Red"); + profile.SetDefault(typeof(ISomething), instance); + + profile.Remove(typeof(ISomething), new NullInstance()); + + profile.GetDefault(typeof (ISomething)).ShouldBeTheSameAs(instance); + } + + [Test] + public void remove_if_the_instance_does_match() + { + var profile = new Profile("something"); + var instance = new ConfiguredInstance(typeof(SomethingOne)).WithName("Red"); + profile.SetDefault(typeof(ISomething), instance); + + profile.Remove(typeof(ISomething), instance); + + profile.GetDefault(typeof(ISomething)).ShouldBeNull(); + } + + [Test] + public void do_not_blow_up_if_the_profile_does_not_have_the_plugin_type_being_removed() + { + var profile = new Profile("something"); + + var instance = new ConfiguredInstance(typeof(SomethingOne)).WithName("Red"); + + profile.Remove(typeof(ISomething), instance); + } + + [Test] public void A_call_to_fill_sets_the_default_for_a_plugin_type_if_no_previous_default_is_known() { var profile = new Profile("something"); Modified: trunk/Source/StructureMap.Testing/Query/GenericFamilyConfigurationTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Query/GenericFamilyConfigurationTester.cs 2009-12-28 01:43:19 UTC (rev 307) +++ trunk/Source/StructureMap.Testing/Query/GenericFamilyConfigurationTester.cs 2009-12-28 02:57:22 UTC (rev 308) @@ -26,6 +26,37 @@ private GenericFamilyConfiguration configuration; [Test] + public void eject_and_remove_an_instance() + { + var instance = new ConfiguredInstance(typeof(Service<>)); + family.AddInstance(instance); + family.AddInstance(new ConfiguredInstance(typeof(Service2<>))); + + var iRef = configuration.Instances.FirstOrDefault(x => x.Name == instance.Name); + + configuration.EjectAndRemove(iRef); + + family.InstanceCount.ShouldEqual(1); + configuration.Instances.Count().ShouldEqual(1); + + configuration.Instances.Any(x => x.Name == instance.Name).ShouldBeFalse(); + } + + [Test] + public void eject_and_remove_the_default_value() + { + var instance = new ConfiguredInstance(typeof(Service<>)); + family.SetDefault(instance); + family.AddInstance(new ConfiguredInstance(typeof(Service2<>))); + + var iRef = configuration.Instances.FirstOrDefault(x => x.Name == instance.Name); + + configuration.EjectAndRemove(iRef); + + family.GetDefaultInstance().ShouldBeNull(); + } + + [Test] public void build_should_return_null() { configuration.As<IFamily>().Build(null).ShouldBeNull(); Modified: trunk/Source/StructureMap.Testing/Query/InstanceFactoryTypeConfigurationTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Query/InstanceFactoryTypeConfigurationTester.cs 2009-12-28 01:43:19 UTC (rev 307) +++ trunk/Source/StructureMap.Testing/Query/InstanceFactoryTypeConfigurationTester.cs 2009-12-28 02:57:22 UTC (rev 308) @@ -1,5 +1,6 @@ using System.Linq; using NUnit.Framework; +using StructureMap.Query; using StructureMap.Testing.Configuration.DSL; using StructureMap.Testing.Graph; using StructureMap.Testing.Widget; @@ -36,6 +37,46 @@ private Container container; [Test] + public void eject_and_remove_an_instance_should_remove_it_from_the_model() + { + var iRef = container.Model.For<Rule>().Instances.First(); + container.Model.For<Rule>().EjectAndRemove(iRef); + + container.Model.For<Rule>().Instances.Select(x => x.ConcreteType) + .ShouldHaveTheSameElementsAs(typeof(ARule), typeof(ColorRule)); + + container.GetAllInstances<Rule>().Select(x => x.GetType()) + .ShouldHaveTheSameElementsAs(typeof(ARule), typeof(ColorRule)); + } + + + [Test] + public void eject_and_remove_an_instance_should_remove_it_from_the_model_by_name() + { + var iRef = container.Model.For<Rule>().Instances.First(); + container.Model.For<Rule>().EjectAndRemove(iRef.Name); + + container.Model.For<Rule>().Instances.Select(x => x.ConcreteType) + .ShouldHaveTheSameElementsAs(typeof(ARule), typeof(ColorRule)); + + container.GetAllInstances<Rule>().Select(x => x.GetType()) + .ShouldHaveTheSameElementsAs(typeof(ARule), typeof(ColorRule)); + } + + [Test] + public void eject_and_remove_an_instance_by_filter_should_remove_it_from_the_model() + { + var iRef = container.Model.For<Rule>().Instances.First(); + container.Model.For<Rule>().EjectAndRemove(x => x.Name == iRef.Name); + + container.Model.For<Rule>().Instances.Select(x => x.ConcreteType) + .ShouldHaveTheSameElementsAs(typeof(ARule), typeof(ColorRule)); + + container.GetAllInstances<Rule>().Select(x => x.GetType()) + .ShouldHaveTheSameElementsAs(typeof(ARule), typeof(ColorRule)); + } + + [Test] public void build_when_the_cast_does_not_work() { container.Model.For<IWidget>().Default.Get<Rule>().ShouldBeNull(); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |