From: <jer...@us...> - 2008-01-23 15:39:49
|
Revision: 60 http://structuremap.svn.sourceforge.net/structuremap/?rev=60&view=rev Author: jeremydmiller Date: 2008-01-23 07:39:45 -0800 (Wed, 23 Jan 2008) Log Message: ----------- building things by default without configuration Modified Paths: -------------- trunk/README.TXT trunk/Source/StructureMap/Configuration/ConfigurationParserCollection.cs trunk/Source/StructureMap/InstanceManager.cs trunk/Source/StructureMap/Source/XmlTemplater.cs trunk/Source/StructureMap/StructureMapConfiguration.cs trunk/Source/StructureMap.Testing/AlternativeConfigurationTester.cs trunk/Source/StructureMap.Testing/Configuration/ConfigurationParserCollectionTester.cs trunk/Source/StructureMap.Testing/Container/InstanceManagerTester.cs trunk/Source/StructureMap.Testing/TestData/DataMother.cs Modified: trunk/README.TXT =================================================================== --- trunk/README.TXT 2008-01-17 17:46:06 UTC (rev 59) +++ trunk/README.TXT 2008-01-23 15:39:45 UTC (rev 60) @@ -1,4 +1,4 @@ -To start using StructureMap, either use the DLL's in the build folder or click on the RunBuild.BAT file to run the full NAnt build. There is a known issue with the build "sticking" on the custom NAnt tasks. If this happens, delete the copies of StructureMap.Dll and StructureMap.DeploymentTasks.Dll in the bin\NAnt folder. +To start using StructureMap, either use the DLL's in the build folder or click on the RunBuild.BAT file to run the full NAnt build. There is a known issue with the build "sticking" on the custom NAnt tasks. If this happens, delete the copies of StructureMap.Dll and StructureMap.DeploymentTasks.Dll in the bin\NAnt folder. Look in the "build" directory for the build products. A copy of the StructureMap website and documentation is in the "Docs" folder. Modified: trunk/Source/StructureMap/Configuration/ConfigurationParserCollection.cs =================================================================== --- trunk/Source/StructureMap/Configuration/ConfigurationParserCollection.cs 2008-01-17 17:46:06 UTC (rev 59) +++ trunk/Source/StructureMap/Configuration/ConfigurationParserCollection.cs 2008-01-23 15:39:45 UTC (rev 60) @@ -11,21 +11,31 @@ { private List<FetchNodeDelegate> _fetchers = new List<FetchNodeDelegate>(); private List<string> _otherFiles = new List<string>(); - private bool _useDefaultFile = true; + private bool _UseAndEnforceExistenceOfDefaultFile = false; + private bool _ignoreDefaultFile = false; - public bool UseDefaultFile + public bool UseAndEnforceExistenceOfDefaultFile { - get { return _useDefaultFile; } - set { _useDefaultFile = value; } + get { return _UseAndEnforceExistenceOfDefaultFile; } + set { _UseAndEnforceExistenceOfDefaultFile = value; } } + + public bool IgnoreDefaultFile + { + get { return _ignoreDefaultFile; } + set { _ignoreDefaultFile = value; } + } + public ConfigurationParser[] GetParsers() { List<ConfigurationParser> list = new List<ConfigurationParser>(); - if (_useDefaultFile) + // Pick up the configuration in the default StructureMap.config + string pathToStructureMapConfig = StructureMapConfiguration.GetStructureMapConfigurationPath(); + if ( (_UseAndEnforceExistenceOfDefaultFile || File.Exists(pathToStructureMapConfig)) && !_ignoreDefaultFile) { - addParsersFromFile(StructureMapConfiguration.GetStructureMapConfigurationPath(), list); + addParsersFromFile(pathToStructureMapConfig, list); } foreach (string file in _otherFiles) Modified: trunk/Source/StructureMap/InstanceManager.cs =================================================================== --- trunk/Source/StructureMap/InstanceManager.cs 2008-01-17 17:46:06 UTC (rev 59) +++ trunk/Source/StructureMap/InstanceManager.cs 2008-01-23 15:39:45 UTC (rev 60) @@ -80,17 +80,25 @@ { get { + // Preprocess a GenericType if (pluginType.IsGenericType && !_factories.ContainsKey(pluginType)) { PluginFamily family = _genericsGraph.CreateTemplatedFamily(pluginType); return registerPluginFamily(family); } + // Create a new InstanceFactory for a Concrete type if (!_factories.ContainsKey(pluginType)) { - throw new StructureMapException(208, pluginType.FullName); + if (pluginType.IsInterface || pluginType.IsAbstract) + { + throw new StructureMapException(208, pluginType.FullName); + } + + return getOrCreateFactory(pluginType); } + // Normal usage return _factories[pluginType]; } set { _factories[pluginType] = value; } @@ -390,15 +398,20 @@ throw new StructureMapException(230); } - IInstanceFactory factory = getOrCreateFactory(type, delegate(Type t) - { - PluginFamily family = - PluginFamily.CreateAutoFilledPluginFamily(t); - return new InstanceFactory(family, true); - }); + IInstanceFactory factory = getOrCreateFactory(type); return factory.GetInstance(); } + private IInstanceFactory getOrCreateFactory(Type type) + { + return getOrCreateFactory(type, delegate(Type t) + { + PluginFamily family = + PluginFamily.CreateAutoFilledPluginFamily(t); + return new InstanceFactory(family, true); + }); + } + protected IInstanceFactory getOrCreateFactory(Type type, CreateFactoryDelegate createFactory) { if (!_factories.ContainsKey(type)) Modified: trunk/Source/StructureMap/Source/XmlTemplater.cs =================================================================== --- trunk/Source/StructureMap/Source/XmlTemplater.cs 2008-01-17 17:46:06 UTC (rev 59) +++ trunk/Source/StructureMap/Source/XmlTemplater.cs 2008-01-23 15:39:45 UTC (rev 60) @@ -1,4 +1,4 @@ -using System.Collections; +using System.Collections.Generic; using System.Text; using System.Xml; @@ -6,8 +6,8 @@ { public class XmlTemplater { - private string[] _substitutions; - private string _templateXml; + private readonly string[] _substitutions; + private readonly string _templateXml; public XmlTemplater(XmlNode templateNode) { @@ -62,7 +62,7 @@ private class Collector { private readonly XmlNode _templateNode; - private ArrayList _substitutionList = new ArrayList(); + private readonly List<string> _substitutionList = new List<string>(); public Collector(XmlNode templateNode) { @@ -73,7 +73,7 @@ { searchNode(_templateNode); - return (string[]) _substitutionList.ToArray(typeof (string)); + return _substitutionList.ToArray(); } private void searchNode(XmlNode node) Modified: trunk/Source/StructureMap/StructureMapConfiguration.cs =================================================================== --- trunk/Source/StructureMap/StructureMapConfiguration.cs 2008-01-17 17:46:06 UTC (rev 59) +++ trunk/Source/StructureMap/StructureMapConfiguration.cs 2008-01-23 15:39:45 UTC (rev 60) @@ -30,10 +30,17 @@ /// </summary> public static bool UseDefaultStructureMapConfigFile { - get { return _collection.UseDefaultFile; } - set { _collection.UseDefaultFile = value; } + get { return _collection.UseAndEnforceExistenceOfDefaultFile; } + set { _collection.UseAndEnforceExistenceOfDefaultFile = value; } } + + public static bool IgnoreStructureMapConfig + { + get { return _collection.IgnoreDefaultFile; } + set { _collection.IgnoreDefaultFile = value; } + } + public static bool PullConfigurationFromAppConfig { get { return _pullConfigurationFromAppConfig; } @@ -76,6 +83,8 @@ _registries.Add(_registry); _startUp = null; _pullConfigurationFromAppConfig = false; + UseDefaultStructureMapConfigFile = false; + IgnoreStructureMapConfig = false; } /// <summary> Modified: trunk/Source/StructureMap.Testing/AlternativeConfigurationTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/AlternativeConfigurationTester.cs 2008-01-17 17:46:06 UTC (rev 59) +++ trunk/Source/StructureMap.Testing/AlternativeConfigurationTester.cs 2008-01-23 15:39:45 UTC (rev 60) @@ -14,6 +14,8 @@ [SetUp] public void SetUp() { + DataMother.BackupStructureMapConfig(); + ObjectFactory.ReInitialize(); StructureMapConfiguration.ResetAll(); DataMother.WriteDocument("Config1.xml"); @@ -26,6 +28,9 @@ { StructureMapConfiguration.ResetAll(); ObjectFactory.Reset(); + + + DataMother.RestoreStructureMapConfig(); } #endregion @@ -65,11 +70,19 @@ [Test] public void NotTheDefault() { - StructureMapConfiguration.UseDefaultStructureMapConfigFile = false; - StructureMapConfiguration.IncludeConfigurationFromFile("Config1.xml"); - ObjectFactory.Reset(); + try + { + StructureMapConfiguration.UseDefaultStructureMapConfigFile = false; + StructureMapConfiguration.IgnoreStructureMapConfig = true; + StructureMapConfiguration.IncludeConfigurationFromFile("Config1.xml"); + ObjectFactory.Reset(); - assertTheDefault("Orange"); + assertTheDefault("Orange"); + } + finally + { + DataMother.RestoreStructureMapConfig(); + } } [Test] Modified: trunk/Source/StructureMap.Testing/Configuration/ConfigurationParserCollectionTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Configuration/ConfigurationParserCollectionTester.cs 2008-01-17 17:46:06 UTC (rev 59) +++ trunk/Source/StructureMap.Testing/Configuration/ConfigurationParserCollectionTester.cs 2008-01-23 15:39:45 UTC (rev 60) @@ -1,4 +1,5 @@ using System; +using System.IO; using System.Xml; using NUnit.Framework; using StructureMap.Configuration; @@ -15,8 +16,15 @@ public void SetUp() { _collection = new ConfigurationParserCollection(); + DataMother.BackupStructureMapConfig(); } + [TearDown] + public void TearDown() + { + DataMother.RestoreStructureMapConfig(); + } + #endregion private ConfigurationParserCollection _collection; @@ -38,7 +46,11 @@ [Test] public void DoNotUseDefaultAndUseADifferentFile() { - _collection.UseDefaultFile = false; + DataMother.RemoveStructureMapConfig(); + + _collection.UseAndEnforceExistenceOfDefaultFile = false; + _collection.IgnoreDefaultFile = true; + DataMother.WriteDocument("GenericsTesting.xml"); _collection.IncludeFile("GenericsTesting.xml"); @@ -51,7 +63,8 @@ )] public void FileDoesNotExist() { - _collection.UseDefaultFile = false; + _collection.UseAndEnforceExistenceOfDefaultFile = false; + _collection.IgnoreDefaultFile = true; _collection.IncludeFile("DoesNotExist.xml"); _collection.GetParsers(); } @@ -59,11 +72,14 @@ [Test] public void GetIncludes() { + DataMother.RemoveStructureMapConfig(); + DataMother.WriteDocument("Include1.xml"); DataMother.WriteDocument("Include2.xml"); DataMother.WriteDocument("Master.xml"); - _collection.UseDefaultFile = false; + _collection.UseAndEnforceExistenceOfDefaultFile = false; + _collection.IgnoreDefaultFile = true; _collection.IncludeFile("Master.xml"); assertParserIdList("Include1", "Include2", "Master"); @@ -79,7 +95,7 @@ DataMother.WriteDocument("GenericsTesting.xml"); _collection.IncludeFile("GenericsTesting.xml"); - _collection.UseDefaultFile = true; + _collection.UseAndEnforceExistenceOfDefaultFile = true; _collection.IncludeFile("Master.xml"); assertParserIdList("Generics", "Include1", "Include2", "Main", "Master"); @@ -88,6 +104,8 @@ [Test] public void GetXmlFromSomewhereElse() { + DataMother.RemoveStructureMapConfig(); + string xml = "<StructureMap Id=\"Somewhere\"/>"; XmlDocument doc = new XmlDocument(); doc.LoadXml(xml); @@ -95,7 +113,8 @@ FetchNodeDelegate fetcher = delegate { return doc.DocumentElement; }; _collection.IncludeNode(fetcher); - _collection.UseDefaultFile = false; + _collection.UseAndEnforceExistenceOfDefaultFile = false; + _collection.IgnoreDefaultFile = true; assertParserIdList("Somewhere"); } @@ -103,14 +122,14 @@ [Test] public void SimpleDefaultConfigurationParser() { - _collection.UseDefaultFile = true; + _collection.UseAndEnforceExistenceOfDefaultFile = true; assertParserIdList("Main"); } [Test] public void UseDefaultIsTrueUponConstruction() { - Assert.IsTrue(_collection.UseDefaultFile); + Assert.IsFalse(_collection.UseAndEnforceExistenceOfDefaultFile); } } } \ No newline at end of file Modified: trunk/Source/StructureMap.Testing/Container/InstanceManagerTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Container/InstanceManagerTester.cs 2008-01-17 17:46:06 UTC (rev 59) +++ trunk/Source/StructureMap.Testing/Container/InstanceManagerTester.cs 2008-01-23 15:39:45 UTC (rev 60) @@ -1,5 +1,6 @@ using System; using NUnit.Framework; +using StructureMap.Configuration.DSL; using StructureMap.Configuration.Mementos; using StructureMap.Graph; using StructureMap.Interceptors; @@ -145,5 +146,56 @@ InstanceManager manager = new InstanceManager(pluginGraph); Assert.AreSame(pluginGraph.DefaultManager, manager.DefaultManager); } + + [Test] + public void CanBuildConcreteTypesThatAreNotPreviouslyRegistered() + { + // Create a new InstanceManager that has a default instance configured for only the + // IProvider interface. InstanceManager is the real "container" behind ObjectFactory + Registry registry = new Registry(); + registry.ForRequestedType<IProvider>().TheDefaultIsConcreteType<Provider>(); + InstanceManager manager = (InstanceManager) registry.BuildInstanceManager(); + + // Now, have that same InstanceManager create a ClassThatUsesProvider. StructureMap will + // see that ClassThatUsesProvider is concrete, determine its constructor args, and build one + // for you with the default IProvider. No other configuration necessary. + ClassThatUsesProvider classThatUsesProvider = manager.CreateInstance<ClassThatUsesProvider>(); + Assert.IsInstanceOfType(typeof(Provider), classThatUsesProvider.Provider); + } + + [Test] + public void CanBuildConcreteTypesThatAreNotPreviouslyRegisteredWithArgumentsProvided() + { + Registry registry = new Registry(); + registry.ForRequestedType<IProvider>().TheDefaultIsConcreteType<Provider>(); + InstanceManager manager = (InstanceManager)registry.BuildInstanceManager(); + + DifferentProvider differentProvider = new DifferentProvider(); + ExplicitArguments args = new ExplicitArguments(); + args.Set<IProvider>(differentProvider); + + ClassThatUsesProvider classThatUsesProvider = manager.CreateInstance<ClassThatUsesProvider>(args); + Assert.AreSame(differentProvider, classThatUsesProvider.Provider); + } + + public interface IProvider{} + public class Provider : IProvider {} + public class ClassThatUsesProvider + { + private readonly IProvider _provider; + + public ClassThatUsesProvider(IProvider provider) + { + _provider = provider; + } + + + public IProvider Provider + { + get { return _provider; } + } + } + + public class DifferentProvider : IProvider{} } } \ No newline at end of file Modified: trunk/Source/StructureMap.Testing/TestData/DataMother.cs =================================================================== --- trunk/Source/StructureMap.Testing/TestData/DataMother.cs 2008-01-17 17:46:06 UTC (rev 59) +++ trunk/Source/StructureMap.Testing/TestData/DataMother.cs 2008-01-23 15:39:45 UTC (rev 60) @@ -17,6 +17,26 @@ { } + public static void BackupStructureMapConfig() + { + if (File.Exists("StructureMap.config.bak")) File.Delete("StructureMap.config.bak"); + File.Copy("StructureMap.config", "StructureMap.config.bak"); + } + + public static void RestoreStructureMapConfig() + { + if (!File.Exists("StructureMap.config")) + { + File.Copy("StructureMap.config.bak", "StructureMap.config"); + } + + } + + public static void RemoveStructureMapConfig() + { + if (File.Exists("StructureMap.config")) File.Delete("StructureMap.config"); + } + public static XmlDocument GetXmlDocument(string fileName) { XmlDocument document = new XmlDocument(); This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |