10.0
$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)
diff --git a/examples/Spring/Spring.Mvc4QuickStart/Spring.Mvc4QuickStart/Views/Count/Index.cshtml b/examples/Spring/Spring.Mvc4QuickStart/Spring.Mvc4QuickStart/Views/Count/Index.cshtml
new file mode 100644
index 00000000..c39a9569
--- /dev/null
+++ b/examples/Spring/Spring.Mvc4QuickStart/Spring.Mvc4QuickStart/Views/Count/Index.cshtml
@@ -0,0 +1,2 @@
+Singleton Counter object is keeping track of requests for this page.
+Current Count: @ViewBag.Message
diff --git a/examples/Spring/Spring.Mvc4QuickStart/Spring.Mvc4QuickStart/Web.config b/examples/Spring/Spring.Mvc4QuickStart/Spring.Mvc4QuickStart/Web.config
index 43da5d0f..9adf53d8 100644
--- a/examples/Spring/Spring.Mvc4QuickStart/Spring.Mvc4QuickStart/Web.config
+++ b/examples/Spring/Spring.Mvc4QuickStart/Spring.Mvc4QuickStart/Web.config
@@ -14,6 +14,7 @@
+
diff --git a/src/Spring/Spring.Web.Mvc4/SpringMvcDependencyResolver.cs b/src/Spring/Spring.Web.Mvc4/SpringMvcDependencyResolver.cs
index 7bf504c1..59cb4558 100644
--- a/src/Spring/Spring.Web.Mvc4/SpringMvcDependencyResolver.cs
+++ b/src/Spring/Spring.Web.Mvc4/SpringMvcDependencyResolver.cs
@@ -60,7 +60,7 @@ namespace Spring.Web.Mvc
/// Defaults to using the root (default) Application Context.
///
/// The name of the application context.
- public static string ApplicationContextName { get; set; }
+ public string ApplicationContextName { get; set; }
///
diff --git a/src/Spring/Spring.Web.Mvc4/SpringWebApiDependencyResolver.cs b/src/Spring/Spring.Web.Mvc4/SpringWebApiDependencyResolver.cs
index bb87dcc7..d5528008 100644
--- a/src/Spring/Spring.Web.Mvc4/SpringWebApiDependencyResolver.cs
+++ b/src/Spring/Spring.Web.Mvc4/SpringWebApiDependencyResolver.cs
@@ -1,9 +1,11 @@
using System;
using System.Collections.Generic;
+using System.Linq;
using System.Web.Http.Dependencies;
using System.Web.Http.Services;
using Spring.Context;
using Spring.Context.Support;
+using Spring.Core.IO;
namespace Spring.Web.Mvc
@@ -38,16 +40,28 @@ namespace Spring.Web.Mvc
///
public virtual IDependencyScope BeginScope()
{
- var abstractXmlApplicationContext = ApplicationContext as AbstractXmlApplicationContext;
-
- if (abstractXmlApplicationContext != null)
+ if (HasApplicationContext && (HasChildApplicationContextConfigurationLocations || HasChildApplicationContextConfigurationResources))
{
- var locations = abstractXmlApplicationContext.ConfigurationLocations;
- var resources = abstractXmlApplicationContext.ConfigurationResources;
+ string[] configurationLocations = null;
+ if (HasChildApplicationContextConfigurationLocations)
+ {
+ configurationLocations = ChildApplicationContextConfigurationLocations.ToArray();
+ }
- var args = new MvcApplicationContextArgs(string.Format("child_of_{0}", ApplicationContext.Name), ApplicationContext, locations, resources, false);
+ IResource[] configurationResources = null;
+ if (HasChildApplicationContextConfigurationResources)
+ {
+ configurationResources = ChildApplicationContextConfigurationResources.ToArray();
+ }
+
+ var childContextName = string.Format("child_of_{0}", ApplicationContext.Name);
+ var args = new MvcApplicationContextArgs(childContextName, ApplicationContext, configurationLocations, configurationResources, false);
+
+ var childContext = new MvcApplicationContext(args);
+ var newResolver = new SpringWebApiDependencyResolver(childContext) { ApplicationContextName = childContextName };
+
+ RegisterContextIfNeeded(childContext);
- var newResolver = new SpringWebApiDependencyResolver(new MvcApplicationContext(args));
return newResolver;
}
else
@@ -55,5 +69,76 @@ namespace Spring.Web.Mvc
return this;
}
}
+
+ private void RegisterContextIfNeeded(IApplicationContext childContext)
+ {
+ if (!ContextRegistry.IsContextRegistered(childContext.Name))
+ {
+ ContextRegistry.RegisterContext(childContext);
+ }
+ }
+
+ private bool HasChildApplicationContextConfigurationResources
+ {
+ get
+ {
+ return ChildApplicationContextConfigurationResources != null &&
+ ChildApplicationContextConfigurationResources.Count > 0;
+ }
+ }
+
+ private bool HasChildApplicationContextConfigurationLocations
+ {
+ get
+ {
+ return ChildApplicationContextConfigurationLocations != null &&
+ ChildApplicationContextConfigurationLocations.Count > 0;
+ }
+ }
+
+ private bool HasApplicationContext
+ {
+ get { return ApplicationContext != null; }
+ }
+
+ ///
+ /// Gets or sets the child configuration locations.
+ ///
+ /// The child configuration locations.
+ public virtual IList ChildApplicationContextConfigurationLocations { protected get; set; }
+
+ ///
+ /// Gets or sets the child configuration resources.
+ ///
+ /// The child configuration resources.
+ public virtual IList ChildApplicationContextConfigurationResources { protected get; set; }
+
+ ///
+ /// Adds the child configuration resource.
+ ///
+ /// The resource.
+ public virtual void AddChildApplicationContextConfigurationResource(IResource resource)
+ {
+ if (null == ChildApplicationContextConfigurationResources)
+ {
+ ChildApplicationContextConfigurationResources = new List();
+ }
+
+ ChildApplicationContextConfigurationResources.Add(resource);
+ }
+
+ ///
+ /// Adds the child configuration location.
+ ///
+ /// The location.
+ public virtual void AddChildApplicationContextConfigurationLocation(string location)
+ {
+ if (null == ChildApplicationContextConfigurationLocations)
+ {
+ ChildApplicationContextConfigurationLocations = new List();
+ }
+
+ ChildApplicationContextConfigurationLocations.Add(location);
+ }
}
}
\ No newline at end of file
diff --git a/test/Spring/Spring.Web.Mvc3.Tests/SpringMvcDependencyResolverTests.cs b/test/Spring/Spring.Web.Mvc3.Tests/SpringMvcDependencyResolverTests.cs
index 953b6ee9..b9a75a42 100644
--- a/test/Spring/Spring.Web.Mvc3.Tests/SpringMvcDependencyResolverTests.cs
+++ b/test/Spring/Spring.Web.Mvc3.Tests/SpringMvcDependencyResolverTests.cs
@@ -29,10 +29,6 @@ using Spring.Web.Mvc.Tests.Controllers;
namespace Spring.Web.Mvc.Tests
{
-// internal class MockContext : HttpContextBase
-// {
-// }
-
[TestFixture]
public class SpringMvcDependencyResolverTests
{
diff --git a/test/Spring/Spring.Web.Mvc4.Tests/Spring.Web.Mvc4.Tests.2010.csproj b/test/Spring/Spring.Web.Mvc4.Tests/Spring.Web.Mvc4.Tests.2010.csproj
index aa6540a8..fb299ca5 100644
--- a/test/Spring/Spring.Web.Mvc4.Tests/Spring.Web.Mvc4.Tests.2010.csproj
+++ b/test/Spring/Spring.Web.Mvc4.Tests/Spring.Web.Mvc4.Tests.2010.csproj
@@ -67,9 +67,7 @@
Controllers\SecondContainerRegisteredController.cs
-
- SpringMvcDependencyResolverTests.cs
-
+
@@ -78,7 +76,7 @@
True
- {904C97E0-3667-4D12-A55F-6CC2E6F68A0A}
+ {CB351EB2-049D-40BE-BB09-1CE4ED68FE67}
Spring.Web.Mvc4.2010
diff --git a/test/Spring/Spring.Web.Mvc4.Tests/Spring.Web.Mvc4.Tests.build b/test/Spring/Spring.Web.Mvc4.Tests/Spring.Web.Mvc4.Tests.build
index 532e7ff9..4d8b7b44 100644
--- a/test/Spring/Spring.Web.Mvc4.Tests/Spring.Web.Mvc4.Tests.build
+++ b/test/Spring/Spring.Web.Mvc4.Tests/Spring.Web.Mvc4.Tests.build
@@ -25,6 +25,8 @@
+
+
diff --git a/test/Spring/Spring.Web.Mvc4.Tests/SpringMvcDependencyResolverTests.cs b/test/Spring/Spring.Web.Mvc4.Tests/SpringMvcDependencyResolverTests.cs
new file mode 100644
index 00000000..0c8d6a0e
--- /dev/null
+++ b/test/Spring/Spring.Web.Mvc4.Tests/SpringMvcDependencyResolverTests.cs
@@ -0,0 +1,117 @@
+#region License
+
+/*
+ * Copyright © 2002-2011 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#endregion
+
+using System.Collections.Generic;
+using System.Linq;
+using System.Web;
+using System.Web.Mvc;
+using System.Web.Routing;
+using NUnit.Framework;
+using Spring.Context.Support;
+using Spring.Web.Mvc.Tests.Controllers;
+
+namespace Spring.Web.Mvc.Tests
+{
+ [TestFixture]
+ public class SpringMvcDependencyResolverTests
+ {
+ #region Setup/Teardown
+
+ [SetUp]
+ public void _TestSetup()
+ {
+ ContextRegistry.Clear();
+ _context = new MvcApplicationContext("file://objectsMvc.xml");
+ _mvcNamedContext = new MvcApplicationContext("named", false, "file://namedContextObjectsMvc.xml");
+
+ ContextRegistry.RegisterContext(_context);
+ ContextRegistry.RegisterContext(_mvcNamedContext);
+
+ _resolver = new SpringMvcDependencyResolver(_context);
+
+ _resolver.ApplicationContextName = string.Empty;
+ }
+
+ #endregion
+
+ private MvcApplicationContext _context;
+ private MvcApplicationContext _mvcNamedContext;
+ private SpringMvcDependencyResolver _resolver;
+
+ [Test]
+ public void CanUseNamedContextToResolveType()
+ {
+ _resolver.ApplicationContextName = "named";
+ var service = _resolver.GetService(typeof (NamedContextController));
+
+ Assert.NotNull(service);
+ }
+
+ [Test]
+ public void CanUseUnnamedContextToResolveType()
+ {
+ Assume.That(_resolver.ApplicationContextName, Is.Empty, "Resolver should not have a named-context set for this test!");
+
+ var service = _resolver.GetService();
+ Assert.NotNull(service);
+ }
+
+ [Test]
+ public void RequestForNullTypeReturnsNull()
+ {
+ var service = _resolver.GetService(null);
+ Assert.That(service, Is.Null);
+ }
+
+ [Test]
+ public void CanReturnCollectionOfTypes()
+ {
+ var services = _resolver.GetServices();
+ Assert.That(services.Count(), Is.EqualTo(2));
+
+ foreach (var service in services)
+ {
+ Assert.That(service, Is.TypeOf(typeof(FirstContainerRegisteredController)), "Service Returned was not of the expected Type.");
+ }
+ }
+
+ [Test]
+ public void RequestForSingleInstanceOfMultiplyRegisteredTypeReturnsFirstDefinition()
+ {
+ var service = _resolver.GetService();
+ Assert.That(service.TestValue, Is.EqualTo("First Definition of Type"));
+ }
+
+ [Test]
+ public void RequestForSingleUndefinedTypeReturnsNull()
+ {
+ var service = _resolver.GetService();
+ Assert.That(service, Is.Null);
+ }
+
+ [Test]
+ public void RequestForCollectionOfUndefinedTypesReturnsEmptyCollection()
+ {
+ var services = _resolver.GetServices();
+ Assert.That(services, Is.Empty);
+ }
+
+ }
+}
\ No newline at end of file