From: <cm...@us...> - 2008-07-19 23:43:16
|
Revision: 127 http://structuremap.svn.sourceforge.net/structuremap/?rev=127&view=rev Author: cmyers Date: 2008-07-19 23:43:04 +0000 (Sat, 19 Jul 2008) Log Message: ----------- Added some tests around using a non-intrinsic value type (for example, System.Guid) as a child parameter to a plugged type (i.e. class Foo with ctor like: public Foo(Guid someGuid)). Fixed a bug in the ParameterEmitter.cast() method that assumed the ParameterType was a reference type. For value types, you must use the Unbox_Any IL opcode. For reference types, Castcall must be used. For more context, see Paul Segaro's post on the Sm-user's mailing list here: http://groups.google.com/group/structuremap-users/browse_thread/thread/34ddaf549ebb14f7?hl=en Modified Paths: -------------- trunk/Source/StructureMap/Emitting/Parameters/ParameterEmitter.cs trunk/Source/StructureMap.Testing/Configuration/DSL/CreatePluginFamilyTester.cs trunk/Source/StructureMap.Testing/StructureMapConfigurationTester.cs Modified: trunk/Source/StructureMap/Emitting/Parameters/ParameterEmitter.cs =================================================================== --- trunk/Source/StructureMap/Emitting/Parameters/ParameterEmitter.cs 2008-07-10 20:49:46 UTC (rev 126) +++ trunk/Source/StructureMap/Emitting/Parameters/ParameterEmitter.cs 2008-07-19 23:43:04 UTC (rev 127) @@ -20,7 +20,19 @@ protected void cast(ILGenerator ilgen, Type parameterType) { - ilgen.Emit(OpCodes.Castclass, parameterType); + //NOTE: According to the docs, Unbox_Any, when called on a ref type, will just do a Castclass + // but it's probably better to err on the side of being explicit rather than relying + // on non-obvious side effects + + if( parameterType.IsValueType ) + { + ilgen.Emit(OpCodes.Unbox_Any, parameterType); + } + else + { + ilgen.Emit(OpCodes.Castclass, parameterType); + } + } } } \ No newline at end of file Modified: trunk/Source/StructureMap.Testing/Configuration/DSL/CreatePluginFamilyTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/Configuration/DSL/CreatePluginFamilyTester.cs 2008-07-10 20:49:46 UTC (rev 126) +++ trunk/Source/StructureMap.Testing/Configuration/DSL/CreatePluginFamilyTester.cs 2008-07-19 23:43:04 UTC (rev 127) @@ -233,6 +233,16 @@ Assert.IsInstanceOfType(typeof (DefaultGateway), gateway); } + + [Test(Description = "Guid test based on problems encountered by Paul Segaro. See http://groups.google.com/group/structuremap-users/browse_thread/thread/34ddaf549ebb14f7?hl=en")] + public void TheDefaultInstanceIsALambdaForGuidNewGuid() + { + Container manager = + new Container( + registry => registry.ForRequestedType<Guid>().TheDefaultIs(()=>Guid.NewGuid())); + + Assert.IsInstanceOfType(typeof(Guid), manager.GetInstance<Guid>()); + } } public class StubbedInstanceFactoryInterceptor : IBuildInterceptor Modified: trunk/Source/StructureMap.Testing/StructureMapConfigurationTester.cs =================================================================== --- trunk/Source/StructureMap.Testing/StructureMapConfigurationTester.cs 2008-07-10 20:49:46 UTC (rev 126) +++ trunk/Source/StructureMap.Testing/StructureMapConfigurationTester.cs 2008-07-19 23:43:04 UTC (rev 127) @@ -120,8 +120,44 @@ PluginGraph graph = StructureMapConfiguration.GetPluginGraph(); Assert.IsTrue(graph.FamilyCount > 0); } + + [Test(Description = "Guid test based on problems encountered by Paul Segaro. See http://groups.google.com/group/structuremap-users/browse_thread/thread/34ddaf549ebb14f7?hl=en")] + public void TheDefaultInstanceIsALambdaForGuidNewGuid() + { + StructureMapConfiguration.ResetAll(); + StructureMapConfiguration.IgnoreStructureMapConfig = true; + + StructureMapConfiguration.ForRequestedType<Guid>().TheDefaultIs(() => Guid.NewGuid()); + + Assert.That(ObjectFactory.GetInstance<Guid>() != Guid.Empty); + } + + [Test(Description = "Guid test based on problems encountered by Paul Segaro. See http://groups.google.com/group/structuremap-users/browse_thread/thread/34ddaf549ebb14f7?hl=en")] + public void TheDefaultInstance_has_a_dependency_upon_a_Guid_NewGuid_lambda_generated_instance() + { + StructureMapConfiguration.ResetAll(); + StructureMapConfiguration.IgnoreStructureMapConfig = true; + + StructureMapConfiguration.ForRequestedType<Guid>().TheDefaultIs(() => Guid.NewGuid()); + StructureMapConfiguration.ForRequestedType<IFoo>().TheDefaultIsConcreteType<Foo>(); + + Assert.That(ObjectFactory.GetInstance<IFoo>().SomeGuid != Guid.Empty); + } } + public interface IFoo { Guid SomeGuid { get; set; } } + + public class Foo : IFoo + { + public Foo(Guid someGuid) + { + SomeGuid = someGuid; + } + + public Guid SomeGuid { get; set; } + + } + public interface ISomething { } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |