Skip to content

Commit

Permalink
Merge pull request #90 from kamiff/sprnet-generics
Browse files Browse the repository at this point in the history
Improve MVC3,4 application performance,fix mvc4 BUG
  • Loading branch information
sbohlen committed Feb 1, 2015
2 parents 30038f2 + 5f2b2b8 commit ba09005
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 32 deletions.
6 changes: 5 additions & 1 deletion src/Spring/Spring.Web.Mvc3/SpringMvcDependencyResolver.cs
Expand Up @@ -13,7 +13,7 @@ namespace Spring.Web.Mvc
/// </summary>
public class SpringMvcDependencyResolver : IDependencyResolver
{

private static readonly string IgnoreViewNamespace = "ASP.";
private IApplicationContext _context;

/// <summary>
Expand Down Expand Up @@ -67,6 +67,10 @@ public IApplicationContext ApplicationContext
/// <returns>The requested service or object.</returns>
public object GetService(Type serviceType)
{
if (serviceType.FullName.StartsWith(IgnoreViewNamespace))
{
return null;
}
object service = null;

if (serviceType != null)
Expand Down
42 changes: 18 additions & 24 deletions src/Spring/Spring.Web.Mvc4/SpringMvcApplication.cs
Expand Up @@ -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
Expand Down Expand Up @@ -78,10 +72,10 @@ protected virtual System.Web.Mvc.IDependencyResolver BuildDependencyResolver()
/// <summary>
/// Builds the dependency resolver.
/// </summary>
/// <returns>The <see cref="System.Web.Http.Services.IDependencyResolver"/> instance.</returns>
/// <returns>The <see cref="System.Web.Http.Dependencies.IDependencyResolver"/> instance.</returns>
/// You must override this method in a derived class to control the manner in which the
/// <see cref="System.Web.Http.Services.IDependencyResolver"/> is created.
protected virtual System.Web.Http.Services.IDependencyResolver BuildWebApiDependencyResolver()
/// <see cref="System.Web.Http.Dependencies.IDependencyResolver"/> is created.
protected virtual System.Web.Http.Dependencies.IDependencyResolver BuildWebApiDependencyResolver()
{
return new SpringWebApiDependencyResolver(ContextRegistry.GetContext());
}
Expand Down Expand Up @@ -115,10 +109,10 @@ public virtual void RegisterDependencyResolver(System.Web.Mvc.IDependencyResolve
/// Registers the DependencyResolver implementation with the MVC runtime.
/// <remarks>
/// You must override this method in a derived class to control the manner in which the
/// <see cref="System.Web.Http.Services.IDependencyResolver"/> is registered.
/// <see cref="System.Web.Http.Dependencies.IDependencyResolver"/> is registered.
/// </remarks>
/// </summary>
public virtual void RegisterDependencyResolver(System.Web.Http.Services.IDependencyResolver resolver)
public virtual void RegisterDependencyResolver(System.Web.Http.Dependencies.IDependencyResolver resolver)
{
ThreadSafeDependencyResolverRegistrar.Register(resolver);
}
Expand All @@ -128,55 +122,55 @@ public virtual void RegisterDependencyResolver(System.Web.Http.Services.IDepende
/// </summary>
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();

/// <summary>
/// Registers the specified <see cref="System.Web.Mvc.IDependencyResolver"/>.
/// </summary>
/// <param name="resolver">The resolver.</param>
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;
}
}

/// <summary>
/// Registers the specified <see cref="System.Web.Http.Services.IDependencyResolver"/>.
/// Registers the specified <see cref="System.Web.Http.Dependencies.IDependencyResolver"/>.
/// </summary>
/// <param name="resolver">The resolver.</param>
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;
}
}
}
Expand Down
14 changes: 11 additions & 3 deletions src/Spring/Spring.Web.Mvc4/SpringMvcDependencyResolver.cs
Expand Up @@ -13,8 +13,11 @@ namespace Spring.Web.Mvc
/// </summary>
public class SpringMvcDependencyResolver : IDependencyResolver
{

private IApplicationContext _context;
private static readonly string IgnoreViewNamespace = "ASP.";
/// <summary>
/// The <see cref="IApplicationContext"/> to be used by the resolver
/// </summary>
protected IApplicationContext _context;

/// <summary>
/// Initializes a new instance of the <see cref="SpringMvcDependencyResolver"/> class.
Expand Down Expand Up @@ -48,6 +51,7 @@ public IApplicationContext ApplicationContext

return _context;
}
protected set { _context = value; }
}

/// <summary>
Expand All @@ -57,7 +61,7 @@ public IApplicationContext ApplicationContext
/// Defaults to using the root (default) Application Context.
/// </remarks>
/// <value>The name of the application context.</value>
public static string ApplicationContextName { get; set; }
public string ApplicationContextName { get; set; }


/// <summary>
Expand All @@ -67,6 +71,10 @@ public IApplicationContext ApplicationContext
/// <returns>The requested service or object.</returns>
public object GetService(Type serviceType)
{
if (serviceType.FullName.StartsWith(IgnoreViewNamespace))
{
return null;
}
object service = null;

if (serviceType != null)
Expand Down
130 changes: 126 additions & 4 deletions 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
{
/// <summary>
/// Spring-based implementation of the <see cref="IDependencyResolver"/> interface for ASP.NET MVC Web API.
/// Spring-based implementation of the <see cref="System.Web.Http.Dependencies.IDependencyResolver"/> interface for ASP.NET MVC Web API.
/// </summary>
public class SpringWebApiDependencyResolver : SpringMvcDependencyResolver, System.Web.Http.Services.IDependencyResolver
public class SpringWebApiDependencyResolver : SpringMvcDependencyResolver, System.Web.Http.Dependencies.IDependencyResolver
{
/// <summary>
/// Initializes a new instance of the <see cref="SpringMvcDependencyResolver"/> class.
/// </summary>
/// <param name="context">The <see cref="IApplicationContext"/> to be used by the resolver</param>
public SpringWebApiDependencyResolver(IApplicationContext context) : base(context)
public SpringWebApiDependencyResolver(IApplicationContext context)
: base(context)
{
}


/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
public virtual void Dispose()
{
//no unmanaged resources to dispose
}

/// <summary>
///
/// </summary>
/// <returns>The initialized <see cref="IDependencyScope"/> instance.</returns>
/// <exception cref="NotImplementedException"></exception>
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; }
}

/// <summary>
/// Gets or sets the child configuration locations.
/// </summary>
/// <value>The child configuration locations.</value>
public virtual IList<string> ChildApplicationContextConfigurationLocations { protected get; set; }

/// <summary>
/// Gets or sets the child configuration resources.
/// </summary>
/// <value>The child configuration resources.</value>
public virtual IList<IResource> ChildApplicationContextConfigurationResources { protected get; set; }

/// <summary>
/// Adds the child configuration resource.
/// </summary>
/// <param name="resource">The resource.</param>
public virtual void AddChildApplicationContextConfigurationResource(IResource resource)
{
if (null == ChildApplicationContextConfigurationResources)
{
ChildApplicationContextConfigurationResources = new List<IResource>();
}

ChildApplicationContextConfigurationResources.Add(resource);
}

/// <summary>
/// Adds the child configuration location.
/// </summary>
/// <param name="location">The location.</param>
public virtual void AddChildApplicationContextConfigurationLocation(string location)
{
if (null == ChildApplicationContextConfigurationLocations)
{
ChildApplicationContextConfigurationLocations = new List<string>();
}

ChildApplicationContextConfigurationLocations.Add(location);
}
}
}

0 comments on commit ba09005

Please sign in to comment.