Skip to content

Commit

Permalink
patch from Kyle Malloy that adds IContext.BuildUp() and fixes a bug w…
Browse files Browse the repository at this point in the history
…here Lifecycle was not correctly set from a Configure() block
  • Loading branch information
jeremydmiller committed Feb 4, 2010
1 parent 76612c3 commit 84f7fa6
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 3 deletions.
16 changes: 15 additions & 1 deletion Source/StructureMap.Testing/Graph/PluginFamilyTester.cs
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,18 @@ public void SetScopeToThreadLocal()
family.SetScopeTo(InstanceScope.ThreadLocal);
Assert.IsInstanceOfType(typeof (ThreadLocalStorageLifecycle), family.Lifecycle);
}

[Test]
public void Lifecycle_is_imported_from_the_source_when_merging_PluginFamilies()
{
var source = new PluginFamily(typeof(GenericType<>));
source.SetScopeTo(InstanceScope.Unique);
var importInto = new PluginFamily(typeof(GenericType<>));

importInto.ImportFrom(source);

importInto.Lifecycle.ShouldBeOfType(source.Lifecycle.GetType());
}
}


Expand Down Expand Up @@ -313,4 +325,6 @@ public class RandomClass
public interface IDevice
{
}
}

public class GenericType<T> { }
}
14 changes: 13 additions & 1 deletion Source/StructureMap/BuildSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using StructureMap.Interceptors;
using StructureMap.Pipeline;
using StructureMap.Util;
using StructureMap.Construction;

namespace StructureMap
{
Expand Down Expand Up @@ -61,6 +62,17 @@ public Type ParentType
}
}

public void BuildUp(object target)
{
Type pluggedType = target.GetType();
IConfiguredInstance instance = _pipelineGraph.GetDefault(pluggedType) as IConfiguredInstance
?? new ConfiguredInstance(pluggedType);

IInstanceBuilder builder = PluginCache.FindBuilder(pluggedType);
var arguments = new Arguments(instance, this);
builder.BuildUp(arguments, target);
}

public T GetInstance<T>()
{
return (T) CreateInstance(typeof (T));
Expand Down Expand Up @@ -186,4 +198,4 @@ private IInstanceFactory forType(Type pluginType)
return _pipelineGraph.ForType(pluginType);
}
}
}
}
5 changes: 5 additions & 0 deletions Source/StructureMap/Graph/PluginFamily.cs
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,11 @@ public void FillDefault(Profile profile)

public void ImportFrom(PluginFamily source)
{
if (source.Lifecycle != null)
{
SetScopeTo(source.Lifecycle);
}

source.Instances.Each(instance => _instances.Fill(instance.Name, instance));

source._pluggedTypes.Each((key, plugin) => _pluggedTypes.Fill(key, plugin));
Expand Down
10 changes: 9 additions & 1 deletion Source/StructureMap/IContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ public interface IContext
/// </summary>
string RequestedName { get; }

/// <summary>
/// The "BuildUp" method takes in an already constructed object
/// and uses Setter Injection to push in configured dependencies
/// of that object
/// </summary>
/// <param name="target"></param>
void BuildUp(object target);

/// <summary>
/// Get the object of type T that is valid for this build session.
/// </summary>
Expand Down Expand Up @@ -81,4 +89,4 @@ public interface IContext
/// <returns></returns>
IEnumerable<T> GetAllInstances<T>();
}
}
}

0 comments on commit 84f7fa6

Please sign in to comment.