diff --git a/src/Spring/Spring.Web.Mvc3/SpringMvcDependencyResolver.cs b/src/Spring/Spring.Web.Mvc3/SpringMvcDependencyResolver.cs index 42f660b61..51acfa95f 100644 --- a/src/Spring/Spring.Web.Mvc3/SpringMvcDependencyResolver.cs +++ b/src/Spring/Spring.Web.Mvc3/SpringMvcDependencyResolver.cs @@ -13,7 +13,7 @@ namespace Spring.Web.Mvc /// public class SpringMvcDependencyResolver : IDependencyResolver { - + private static readonly string IgnoreViewNamespace = "ASP."; private IApplicationContext _context; /// @@ -67,6 +67,10 @@ public IApplicationContext ApplicationContext /// The requested service or object. public object GetService(Type serviceType) { + if (serviceType.FullName.StartsWith(IgnoreViewNamespace)) + { + return null; + } object service = null; if (serviceType != null) diff --git a/src/Spring/Spring.Web.Mvc4/SpringMvcApplication.cs b/src/Spring/Spring.Web.Mvc4/SpringMvcApplication.cs index 04fb6f47f..c0a70657f 100644 --- a/src/Spring/Spring.Web.Mvc4/SpringMvcApplication.cs +++ b/src/Spring/Spring.Web.Mvc4/SpringMvcApplication.cs @@ -20,15 +20,9 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; using System.Web; using System.Web.Mvc; -using Spring.Objects.Factory; -using Spring.Core.IO; -using Spring.Objects.Factory.Xml; -using System.Web.Routing; + using Spring.Context.Support; namespace Spring.Web.Mvc @@ -78,10 +72,10 @@ protected virtual System.Web.Mvc.IDependencyResolver BuildDependencyResolver() /// /// Builds the dependency resolver. /// - /// The instance. + /// The instance. /// You must override this method in a derived class to control the manner in which the - /// is created. - protected virtual System.Web.Http.Services.IDependencyResolver BuildWebApiDependencyResolver() + /// is created. + protected virtual System.Web.Http.Dependencies.IDependencyResolver BuildWebApiDependencyResolver() { return new SpringWebApiDependencyResolver(ContextRegistry.GetContext()); } @@ -115,10 +109,10 @@ public virtual void RegisterDependencyResolver(System.Web.Mvc.IDependencyResolve /// Registers the DependencyResolver implementation with the MVC runtime. /// /// You must override this method in a derived class to control the manner in which the - /// is registered. + /// is registered. /// /// - public virtual void RegisterDependencyResolver(System.Web.Http.Services.IDependencyResolver resolver) + public virtual void RegisterDependencyResolver(System.Web.Http.Dependencies.IDependencyResolver resolver) { ThreadSafeDependencyResolverRegistrar.Register(resolver); } @@ -128,9 +122,9 @@ public virtual void RegisterDependencyResolver(System.Web.Http.Services.IDepende /// protected static class ThreadSafeDependencyResolverRegistrar { - private static bool _isMvcResolverRegistered = false; - private static bool _isWebApiResolverRegistered = false; - private static readonly Object Lock = new Object(); + private static bool isMvcResolverRegistered; + private static bool isWebApiResolverRegistered; + private static readonly object Lock = new object(); /// /// Registers the specified . @@ -138,45 +132,45 @@ protected static class ThreadSafeDependencyResolverRegistrar /// The resolver. public static void Register(IDependencyResolver resolver) { - if (_isMvcResolverRegistered) + if (isMvcResolverRegistered) { return; } lock (Lock) { - if (_isMvcResolverRegistered) + if (isMvcResolverRegistered) { return; } DependencyResolver.SetResolver(resolver); - _isMvcResolverRegistered = true; + isMvcResolverRegistered = true; } } /// - /// Registers the specified . + /// Registers the specified . /// /// The resolver. - public static void Register(System.Web.Http.Services.IDependencyResolver resolver) + public static void Register(System.Web.Http.Dependencies.IDependencyResolver resolver) { - if (_isWebApiResolverRegistered) + if (isWebApiResolverRegistered) { return; } lock (Lock) { - if (_isWebApiResolverRegistered) + if (isWebApiResolverRegistered) { return; } - System.Web.Http.GlobalConfiguration.Configuration.ServiceResolver.SetResolver(resolver); + System.Web.Http.GlobalConfiguration.Configuration.DependencyResolver = resolver; - _isWebApiResolverRegistered = true; + isWebApiResolverRegistered = true; } } } diff --git a/src/Spring/Spring.Web.Mvc4/SpringMvcDependencyResolver.cs b/src/Spring/Spring.Web.Mvc4/SpringMvcDependencyResolver.cs index 3b75bdff0..b1548c66c 100644 --- a/src/Spring/Spring.Web.Mvc4/SpringMvcDependencyResolver.cs +++ b/src/Spring/Spring.Web.Mvc4/SpringMvcDependencyResolver.cs @@ -13,8 +13,11 @@ namespace Spring.Web.Mvc /// public class SpringMvcDependencyResolver : IDependencyResolver { - - private IApplicationContext _context; + private static readonly string IgnoreViewNamespace = "ASP."; + /// + /// The to be used by the resolver + /// + protected IApplicationContext _context; /// /// Initializes a new instance of the class. @@ -48,6 +51,7 @@ public IApplicationContext ApplicationContext return _context; } + protected set { _context = value; } } /// @@ -57,7 +61,7 @@ public IApplicationContext ApplicationContext /// 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; } /// @@ -67,6 +71,10 @@ public IApplicationContext ApplicationContext /// The requested service or object. public object GetService(Type serviceType) { + if (serviceType.FullName.StartsWith(IgnoreViewNamespace)) + { + return null; + } object service = null; if (serviceType != null) diff --git a/src/Spring/Spring.Web.Mvc4/SpringWebApiDependencyResolver.cs b/src/Spring/Spring.Web.Mvc4/SpringWebApiDependencyResolver.cs index 7f6ea6a71..76ae83c1e 100644 --- a/src/Spring/Spring.Web.Mvc4/SpringWebApiDependencyResolver.cs +++ b/src/Spring/Spring.Web.Mvc4/SpringWebApiDependencyResolver.cs @@ -1,22 +1,144 @@ using System; using System.Collections.Generic; -using System.Web.Http.Services; +using System.Linq; +using System.Web.Http.Dependencies; + using Spring.Context; +using Spring.Context.Support; +using Spring.Core.IO; namespace Spring.Web.Mvc { /// - /// Spring-based implementation of the interface for ASP.NET MVC Web API. + /// Spring-based implementation of the interface for ASP.NET MVC Web API. /// - public class SpringWebApiDependencyResolver : SpringMvcDependencyResolver, System.Web.Http.Services.IDependencyResolver + public class SpringWebApiDependencyResolver : SpringMvcDependencyResolver, System.Web.Http.Dependencies.IDependencyResolver { /// /// Initializes a new instance of the class. /// /// The to be used by the resolver - public SpringWebApiDependencyResolver(IApplicationContext context) : base(context) + public SpringWebApiDependencyResolver(IApplicationContext context) + : base(context) + { + } + + + /// + /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. + /// + public virtual void Dispose() + { + //no unmanaged resources to dispose + } + + /// + /// + /// + /// The initialized instance. + /// + public virtual IDependencyScope BeginScope() { + if (HasApplicationContext && (HasChildApplicationContextConfigurationLocations || HasChildApplicationContextConfigurationResources)) + { + string[] configurationLocations = null; + if (HasChildApplicationContextConfigurationLocations) + { + configurationLocations = ChildApplicationContextConfigurationLocations.ToArray(); + } + + 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); + + return newResolver; + } + else + { + 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