|
From: <jer...@us...> - 2008-05-04 02:59:59
|
Revision: 92
http://structuremap.svn.sourceforge.net/structuremap/?rev=92&view=rev
Author: jeremydmiller
Date: 2008-05-03 19:59:57 -0700 (Sat, 03 May 2008)
Log Message:
-----------
Cleaned up PluginFamilyAttribute
Modified Paths:
--------------
trunk/Source/StructureMap/Attributes/PluginFamilyAttribute.cs
trunk/Source/StructureMap/Graph/AssemblyGraph.cs
trunk/Source/StructureMap/Graph/PluginFamily.cs
trunk/Source/StructureMap/Graph/PluginGraph.cs
trunk/Source/StructureMap/StructureMap.csproj
trunk/Source/StructureMap.Testing/Attributes/PluginFamilyAttributeTester.cs
trunk/Source/StructureMap.Testing/Container/ImplicitDefaultTest.cs
Modified: trunk/Source/StructureMap/Attributes/PluginFamilyAttribute.cs
===================================================================
--- trunk/Source/StructureMap/Attributes/PluginFamilyAttribute.cs 2008-05-04 02:28:19 UTC (rev 91)
+++ trunk/Source/StructureMap/Attributes/PluginFamilyAttribute.cs 2008-05-04 02:59:57 UTC (rev 92)
@@ -1,8 +1,6 @@
using System;
using StructureMap.Attributes;
using StructureMap.Graph;
-using StructureMap.Interceptors;
-using StructureMap.Source;
namespace StructureMap
{
@@ -60,24 +58,6 @@
set { _scope = value ? InstanceScope.Singleton : InstanceScope.PerRequest; }
}
- public MementoSource CreateSource(Type exportedType)
- {
- if (SourceType != null)
- {
- try
- {
- return (MementoSource) Activator.CreateInstance(SourceType);
- }
- catch (Exception ex)
- {
- throw new StructureMapException(122, ex, SourceType.FullName, exportedType.FullName);
- }
- }
- else
- {
- return new MemoryMementoSource();
- }
- }
/// <summary>
/// Determines if a Type object is marked as a PluginFamily
@@ -91,84 +71,36 @@
return (att != null);
}
- /// <summary>
- /// Gets the default instance key from a Type marked as a PluginFamily
- /// </summary>
- /// <param name="objectType"></param>
- /// <returns></returns>
- public static string GetDefaultKey(Type objectType)
+ public static void ConfigureFamily(IPluginFamily family)
{
PluginFamilyAttribute att =
- GetCustomAttribute(objectType, typeof (PluginFamilyAttribute), false) as PluginFamilyAttribute;
- if (att == null)
- {
- return string.Empty;
- }
- else
- {
- return att.DefaultKey;
- }
- }
-
- /// <summary>
- /// Interrogates the attribute on the pluginType and determines if the PluginFamily is
- /// marked as a Singleton
- /// </summary>
- /// <param name="pluginType"></param>
- /// <returns></returns>
- public static bool IsMarkedAsSingleton(Type pluginType)
- {
- bool returnValue = false;
-
- PluginFamilyAttribute att =
- GetCustomAttribute(
- pluginType,
- typeof (PluginFamilyAttribute), false)
+ GetCustomAttribute(family.PluginType, typeof (PluginFamilyAttribute), false)
as PluginFamilyAttribute;
-
if (att != null)
{
- returnValue = att.IsSingleton;
+ att.Configure(family);
}
-
- return returnValue;
}
- public PluginFamily BuildPluginFamily(Type exportedType)
+ public void Configure(IPluginFamily family)
{
- if (!MarkedAsPluginFamily(exportedType))
+ if (SourceType != null)
{
- return new PluginFamily(exportedType);
+ try
+ {
+ MementoSource source = (MementoSource) Activator.CreateInstance(SourceType);
+ family.AddMementoSource(source);
+ }
+ catch (Exception ex)
+ {
+ throw new StructureMapException(122, ex, SourceType.FullName, family.PluginTypeName);
+ }
}
- MementoSource source = CreateSource(exportedType);
- PluginFamily family = new PluginFamily(exportedType);
- family.DefaultInstanceKey = DefaultKey;
- family.AddMementoSource(source);
- family.SetScopeTo(Scope);
-
- return family;
+ if (!string.IsNullOrEmpty(DefaultKey)) family.DefaultInstanceKey = DefaultKey;
+ if (Scope != InstanceScope.PerRequest) family.SetScopeTo(Scope);
}
-
- public static PluginFamily CreatePluginFamily(Type exportedType)
- {
- PluginFamilyAttribute att = GetAttribute(exportedType);
-
- if (att == null)
- {
- return new PluginFamily(exportedType);
- }
-
- PluginFamily family = att.BuildPluginFamily(exportedType);
-
- return family;
- }
-
- public static PluginFamilyAttribute GetAttribute(Type exportedType)
- {
- return GetCustomAttribute(exportedType, typeof (PluginFamilyAttribute), false) as PluginFamilyAttribute;
- }
}
}
\ No newline at end of file
Modified: trunk/Source/StructureMap/Graph/AssemblyGraph.cs
===================================================================
--- trunk/Source/StructureMap/Graph/AssemblyGraph.cs 2008-05-04 02:28:19 UTC (rev 91)
+++ trunk/Source/StructureMap/Graph/AssemblyGraph.cs 2008-05-04 02:59:57 UTC (rev 92)
@@ -98,7 +98,7 @@
{
if (PluginFamilyAttribute.MarkedAsPluginFamily(exportedType))
{
- PluginFamily family = PluginFamilyAttribute.CreatePluginFamily(exportedType);
+ PluginFamily family = new PluginFamily(exportedType);
list.Add(family);
}
}
Modified: trunk/Source/StructureMap/Graph/PluginFamily.cs
===================================================================
--- trunk/Source/StructureMap/Graph/PluginFamily.cs 2008-05-04 02:28:19 UTC (rev 91)
+++ trunk/Source/StructureMap/Graph/PluginFamily.cs 2008-05-04 02:59:57 UTC (rev 92)
@@ -13,7 +13,7 @@
/// the system. A PluginFamily defines a CLR Type that StructureMap can build, and all of the possible
/// Plugin\x92s implementing the CLR Type.
/// </summary>
- public class PluginFamily
+ public class PluginFamily : IPluginFamily
{
public const string CONCRETE_KEY = "CONCRETE";
private readonly List<InstanceMemento> _mementoList = new List<InstanceMemento>();
@@ -39,13 +39,7 @@
_pluginTypeName = TypePath.GetAssemblyQualifiedName(_pluginType);
_plugins = new PluginCollection(this);
- // TODO -- Merge functionality with PluginFamilyAttribute
- PluginFamilyAttribute attribute = PluginFamilyAttribute.GetAttribute(pluginType);
- if (attribute != null)
- {
- SetScopeTo(attribute.Scope);
- }
-
+ PluginFamilyAttribute.ConfigureFamily(this);
}
Modified: trunk/Source/StructureMap/Graph/PluginGraph.cs
===================================================================
--- trunk/Source/StructureMap/Graph/PluginGraph.cs 2008-05-04 02:28:19 UTC (rev 91)
+++ trunk/Source/StructureMap/Graph/PluginGraph.cs 2008-05-04 02:59:57 UTC (rev 92)
@@ -169,7 +169,7 @@
{
if (!_pluginFamilies.Contains(pluginType))
{
- PluginFamily family = PluginFamilyAttribute.CreatePluginFamily(pluginType);
+ PluginFamily family = new PluginFamily(pluginType);
_pluginFamilies.Add(family);
attachImplicitPlugins(family);
}
Modified: trunk/Source/StructureMap/StructureMap.csproj
===================================================================
--- trunk/Source/StructureMap/StructureMap.csproj 2008-05-04 02:28:19 UTC (rev 91)
+++ trunk/Source/StructureMap/StructureMap.csproj 2008-05-04 02:59:57 UTC (rev 92)
@@ -118,6 +118,7 @@
<Compile Include="Configuration\ProfileBuilder.cs" />
<Compile Include="Diagnostics\Tokens.cs" />
<Compile Include="Emitting\ConstructorEmitter.cs" />
+ <Compile Include="Graph\IPluginFamily.cs" />
<Compile Include="InstanceBuilderList.cs" />
<Compile Include="InstanceFamily.cs" />
<Compile Include="Pipeline\BuildStrategies.cs" />
Modified: trunk/Source/StructureMap.Testing/Attributes/PluginFamilyAttributeTester.cs
===================================================================
--- trunk/Source/StructureMap.Testing/Attributes/PluginFamilyAttributeTester.cs 2008-05-04 02:28:19 UTC (rev 91)
+++ trunk/Source/StructureMap.Testing/Attributes/PluginFamilyAttributeTester.cs 2008-05-04 02:59:57 UTC (rev 92)
@@ -1,5 +1,7 @@
using System;
using NUnit.Framework;
+using Rhino.Mocks;
+using Rhino.Mocks.Constraints;
using StructureMap.Attributes;
using StructureMap.Graph;
using StructureMap.Interceptors;
@@ -16,17 +18,19 @@
PluginFamilyAttribute att = new PluginFamilyAttribute("something");
att.Scope = scope;
- PluginFamily family = att.BuildPluginFamily(typeof (Target1));
+ PluginFamily family = new PluginFamily(typeof(TypeThatDoesNotHaveCustomMementoSource));
+ att.Configure(family);
+
Assert.IsInstanceOfType(interceptorType, family.Policy);
}
[PluginFamily]
- public class Target1
+ public class TypeThatDoesNotHaveCustomMementoSource
{
}
[PluginFamily(SourceType = typeof (CustomMementoSource))]
- public class Target2
+ public class TypeWithDesignatedMementoSource
{
}
@@ -56,36 +60,63 @@
[Test]
public void CreateMemoryMementoSourceWhenTheMementoSourceIsExplicitlyDefinedInAttribute()
{
- PluginFamilyAttribute att = PluginFamilyAttribute.GetAttribute(typeof (Target2));
+ MockRepository mocks = new MockRepository();
+ IPluginFamily family = mocks.DynamicMock<IPluginFamily>();
- MementoSource source = att.CreateSource(typeof (Target2));
- Assert.IsTrue(source is CustomMementoSource);
+ using (mocks.Record())
+ {
+ SetupResult.For(family.PluginType).Return(typeof (TypeWithDesignatedMementoSource));
+
+ family.AddMementoSource(null);
+ LastCall.Constraints(Is.TypeOf<CustomMementoSource>());
+ }
+
+ using (mocks.Playback())
+ {
+ PluginFamilyAttribute.ConfigureFamily(family);
+ }
}
[Test]
- public void CreatesAConfigInstanceMementoSourceByDefault()
+ public void Do_not_add_a_memento_source_if_it_is_not_defined()
{
- PluginFamilyAttribute att = PluginFamilyAttribute.GetAttribute(typeof (Target1));
+ MockRepository mocks = new MockRepository();
+ IPluginFamily family = mocks.DynamicMock<IPluginFamily>();
- MementoSource source = att.CreateSource(typeof (Target1));
- Assert.IsTrue(source is MemoryMementoSource);
+ using (mocks.Record())
+ {
+ SetupResult.For(family.PluginType).Return(typeof(TypeThatDoesNotHaveCustomMementoSource));
+
+ family.AddMementoSource(null);
+ LastCall.Repeat.Never();
+ }
+
+ using (mocks.Playback())
+ {
+ PluginFamilyAttribute.ConfigureFamily(family);
+ }
}
+
[Test]
- public void PerRequestDoesNotHaveAnyInterceptors()
+ public void PerRequest_DoesNot_call_SetScopeTo_on_family()
{
PluginFamilyAttribute att = new PluginFamilyAttribute("something");
att.Scope = InstanceScope.PerRequest;
- PluginFamily family = att.BuildPluginFamily(typeof (Target1));
- Assert.IsInstanceOfType(typeof(BuildPolicy), family.Policy);
- }
+ MockRepository mocks = new MockRepository();
+ IPluginFamily family = mocks.DynamicMock<IPluginFamily>();
- [Test]
- public void ScopeIsPerRequestByDefault()
- {
- PluginFamilyAttribute att = new PluginFamilyAttribute();
- Assert.AreEqual(InstanceScope.PerRequest, att.Scope);
+ using (mocks.Record())
+ {
+ family.SetScopeTo(InstanceScope.PerRequest);
+ LastCall.Repeat.Never();
+ }
+
+ using (mocks.Playback())
+ {
+ att.Configure(family);
+ }
}
[Test]
Modified: trunk/Source/StructureMap.Testing/Container/ImplicitDefaultTest.cs
===================================================================
--- trunk/Source/StructureMap.Testing/Container/ImplicitDefaultTest.cs 2008-05-04 02:28:19 UTC (rev 91)
+++ trunk/Source/StructureMap.Testing/Container/ImplicitDefaultTest.cs 2008-05-04 02:59:57 UTC (rev 92)
@@ -13,16 +13,5 @@
Assert.IsNotNull(gateway);
}
- [Test]
- public void GetTheDefaultInstanceKeyFromType()
- {
- string default1 = PluginFamilyAttribute.GetDefaultKey(typeof (IGateway));
- string default2 = PluginFamilyAttribute.GetDefaultKey(typeof (IService));
- string default3 = PluginFamilyAttribute.GetDefaultKey(typeof (IWorker));
-
- Assert.AreEqual("Default", default1);
- Assert.AreEqual(string.Empty, default2);
- Assert.AreEqual(string.Empty, default3);
- }
}
}
\ No newline at end of file
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|