Skip to content

Commit

Permalink
test OpenGenericWithConstraints added with corresponding bug-fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmitry Kononchuk committed May 9, 2011
1 parent 1c3cb8c commit 746f0fb
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 20 deletions.
48 changes: 48 additions & 0 deletions Source/StructureMap.Testing/Bugs/OpenGenericWithConstraints.cs
@@ -0,0 +1,48 @@
using System;
using System.Collections;
using System.Collections.Generic;
using NUnit.Framework;
using System.Linq;
using NUnit.Framework.SyntaxHelpers;

namespace StructureMap.Testing.Bugs
{
public class ClosedGenericForStruct<T>: IAmOpenGeneric<T>
where T: struct {}

public class ClosedGenericForEnumerable<T>: IAmOpenGeneric<T>
where T: IEnumerable {}

public struct EnumerableStruct: IEnumerable
{
#region IEnumerable Members

public IEnumerator GetEnumerator()
{
throw new NotImplementedException();
}

#endregion
}

[TestFixture]
public class OpenGenericWithConstraints
{
[Test]
public void RegisterTwoInheritorsWithDifferentTypeConstraints()
{
var container = new Container(x => {
x.Scan(y => y.TheCallingAssembly());
x.For(typeof(IAmOpenGeneric<>)).Add(typeof(ClosedGenericForEnumerable<>));
x.For(typeof(IAmOpenGeneric<>)).Add(typeof(ClosedGenericForStruct<>));
});
container.GetInstance<IAmOpenGeneric<int>>().ShouldBeOfType<ClosedGenericForStruct<int>>();
container.GetInstance<IAmOpenGeneric<ArrayList>>().ShouldBeOfType<ClosedGenericForEnumerable<ArrayList>>();

IList<IAmOpenGeneric<EnumerableStruct>> amOpenGenerics = container.GetAllInstances<IAmOpenGeneric<EnumerableStruct>>();
amOpenGenerics.Single(x => x.GetType() == typeof(ClosedGenericForStruct<EnumerableStruct>));
amOpenGenerics.Single(x => x.GetType() == typeof(ClosedGenericForEnumerable<EnumerableStruct>));
Assert.That(amOpenGenerics.Count, Is.EqualTo(2));
}
}
}
3 changes: 2 additions & 1 deletion Source/StructureMap.Testing/StructureMap.Testing.csproj
@@ -1,7 +1,7 @@
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="3.5">
<PropertyGroup>
<ProjectType>Local</ProjectType>
<ProductVersion>9.0.30729</ProductVersion>
<ProductVersion>9.0.21022</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{63C2742D-B6E2-484F-AFDB-346873075C5E}</ProjectGuid>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
Expand Down Expand Up @@ -198,6 +198,7 @@
<Compile Include="Bugs\InjectByFuncWithNoPublicConstructors.cs" />
<Compile Include="Bugs\LambdaCreatesNullBugTester.cs" />
<Compile Include="Bugs\MixedConfigureAndInitializeMissingInstanceProblem.cs" />
<Compile Include="Bugs\OpenGenericWithConstraints.cs" />
<Compile Include="Bugs\ScanIndexerBugTester.cs" />
<Compile Include="Bugs\SingletonShouldBeLazy.cs" />
<Compile Include="Bugs\SpecifyScopeInConfigureTester.cs" />
Expand Down
11 changes: 7 additions & 4 deletions Source/StructureMap/InstanceFactory.cs
Expand Up @@ -56,15 +56,18 @@ public InstanceFactory(PluginFamily family)

public static InstanceFactory CreateFactoryForType(Type concreteType, ProfileManager profileManager)
{
var family = new PluginFamily(concreteType);
return CreateFactoryForFamily(new PluginFamily(concreteType), profileManager);
}

public static InstanceFactory CreateFactoryForFamily(PluginFamily family, ProfileManager profileManager)
{
family.Seal();

var factory = new InstanceFactory(family);

Instance instance = family.GetDefaultInstance();
if (instance != null)
{
profileManager.SetDefault(concreteType, instance);
if(instance != null) {
profileManager.SetDefault(family.PluginType, instance);
}

return factory;
Expand Down
31 changes: 17 additions & 14 deletions Source/StructureMap/Pipeline/ConstructorInstance.cs
Expand Up @@ -253,23 +253,26 @@ public static ConstructorInstance For<T>()

public override Instance CloseType(Type[] types)
{
if (_plugin.PluggedType.IsOpenGeneric())
{
Type closedType = _plugin.PluggedType.MakeGenericType(types);
var closedInstance = new ConstructorInstance(closedType);

_dependencies.Each((key, i) =>
{
if (i.CopyAsIsWhenClosingInstance)
{
closedInstance.SetChild(key, i);
}
});
if(!_plugin.PluggedType.IsOpenGeneric())
return null;

return closedInstance;
Type closedType;
try {
closedType = _plugin.PluggedType.MakeGenericType(types);
}
catch {
return null;
}

var closedInstance = new ConstructorInstance(closedType);

_dependencies.Each((key, i) => {
if(i.CopyAsIsWhenClosingInstance) {
closedInstance.SetChild(key, i);
}
});

return null;
return closedInstance;
}

public override string ToString()
Expand Down
2 changes: 1 addition & 1 deletion Source/StructureMap/PipelineGraph.cs
Expand Up @@ -159,7 +159,7 @@ protected virtual InstanceFactory createFactory(Type pluggedType)
if (pluggedType.IsGenericType)
{
PluginFamily family = _genericsGraph.CreateTemplatedFamily(pluggedType, _profileManager);
if (family != null) return new InstanceFactory(family);
if (family != null) return InstanceFactory.CreateFactoryForFamily(family, _profileManager);
}

return InstanceFactory.CreateFactoryForType(pluggedType, _profileManager);
Expand Down

0 comments on commit 746f0fb

Please sign in to comment.