diff --git a/extras/Spring.Web.Mvc5/README.md b/extras/Spring.Web.Mvc5/README.md index 75e4639..034e56b 100644 --- a/extras/Spring.Web.Mvc5/README.md +++ b/extras/Spring.Web.Mvc5/README.md @@ -1 +1,24 @@ # Unravel.Spring.Web.Mvc5 + +Unravel provides a `SpringMvcApplication` to support dependency injection with [Spring](https://springframework.net/). + +- `Unravel.Spring.Web.Mvc.SpringMvcApplication` + - Inherits from `Unravel.Application` + - Provides `virtual` methods from `Spring.Web.SpringMvcApplication` for compatibility: + - `ConfigureApplicationContext()` + - `BuildDependencyResolver()` + - `BuildApiDependencyResolver()` + +Need `SpringControllerFactory` and `SpringActionInvoker`? **Open an issue.** + +## Setup + +1. Install `Unravel.Spring.Web.Mvc5` in your Web Application project + +1. Continue with setup for [Unravel.Startup](https://www.nuget.org/packages/Unravel.Startup), + except inherit from `Unravel.Spring.Web.Mvc.SpringMvcApplication` + + ```diff + -public class MvcApplication : SpringMvcApplication + +public partial class Startup : Unravel.Spring.Web.Mvc.SpringMvcApplication + ``` diff --git a/extras/Spring.Web.Mvc5/SpringMvcApplication.cs b/extras/Spring.Web.Mvc5/SpringMvcApplication.cs new file mode 100644 index 0000000..e05fc55 --- /dev/null +++ b/extras/Spring.Web.Mvc5/SpringMvcApplication.cs @@ -0,0 +1,142 @@ +using System; +using Spring.Context; +using Spring.Web.Mvc; +using System.Web.Mvc; +using Microsoft.AspNetCore.Hosting; +using Microsoft.Extensions.DependencyInjection; +using Spring.Context.Support; +using MvcDependencyResolver = Unravel.AspNet.Mvc.DependencyInjection.Internal.RequestServicesDependencyResolver; +using WebApiDependencyResolver = Unravel.AspNet.WebApi.DependencyInjection.Internal.RequestServicesDependencyResolver; + +namespace Unravel.Spring.Web.Mvc +{ + public class SpringMvcApplication : Application + { + /// + /// Calls , + /// then calls + /// with . + /// + /// + protected override IWebHostBuilder CreateWebHostBuilder() + { + return base.CreateWebHostBuilder() + .ConfigureServices(ConfigureSpringServices); + } + + /// + /// Calls , + /// then adds Spring services to the specified , including: + /// + /// The root from + /// An MVC IDependencyResolver with the result of as fallback. + /// A WebApi IDependencyResolver with the result of as fallback. + /// + /// + /// The to configure. + protected virtual void ConfigureSpringServices(IServiceCollection services) + { + ConfigureApplicationContext(); + services.AddSingleton(ContextRegistry.GetContext()); + + services.AddSingleton( + new MvcDependencyResolver { Fallback = BuildDependencyResolver() }); + + services.AddSingleton( + new WebApiDependencyResolver { Fallback = BuildWebApiDependencyResolver() }); + } + + /// + /// Builds the dependency resolver. + /// + /// The instance. + /// + /// You must override this method in a derived class to control the manner in which the + /// is created. + /// + protected virtual IDependencyResolver BuildDependencyResolver() + { + return new SpringMvcDependencyResolver(ContextRegistry.GetContext()); + } + + /// + /// Builds the dependency resolver. + /// + /// 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.Dependencies.IDependencyResolver BuildWebApiDependencyResolver() + { + return new SpringWebApiDependencyResolver(ContextRegistry.GetContext()); + } + + /// + /// Configures the instance. + /// + /// + /// You must override this method in a derived class to control the manner in which the + /// is configured. + /// + protected virtual void ConfigureApplicationContext() + { + } + + /// + /// Unused by Unravel. + /// Formerly registered the DependencyResolver implementation with the MVC runtime. + /// + /// + /// Override ConfigureServices() to control the manner in which the + /// is registered. + /// The MVC runtime is automatically configured from Services. + /// + [Obsolete("Override ConfigureServices() to customize the dependency resolver.")] + public virtual void RegisterDependencyResolver(IDependencyResolver resolver) + { + ThreadSafeDependencyResolverRegistrar.Register(resolver); + } + + /// + /// Unused by Unravel. + /// Formerly registered the DependencyResolver implementation with the WebApi runtime. + /// + /// + /// Override ConfigureServices() to control the manner in which the + /// is registered. + /// The WebApi runtime is automatically configured from Services. + /// + [Obsolete("Override ConfigureServices() to customize the dependency resolver.")] + public virtual void RegisterDependencyResolver(System.Web.Http.Dependencies.IDependencyResolver resolver) + { + ThreadSafeDependencyResolverRegistrar.Register(resolver); + } + + [Obsolete("Dependency resolvers are automatically registered from Services.")] + protected static class ThreadSafeDependencyResolverRegistrar + { + /// + /// Registers the specified . + /// + /// The resolver. + /// MVC IDependencyResolver is automatically registered from Services. + [Obsolete("Dependency resolver is automatically registered from Services.")] + public static void Register(IDependencyResolver resolver) + { + throw new NotSupportedException("MVC IDependencyResolver is automatically registered from Services."); + } + + /// + /// Registers the specified . + /// + /// The resolver. + /// WebApi IDependencyResolver is automatically registered from Services. + [Obsolete("Dependency resolver is automatically registered from Services.")] + public static void Register(System.Web.Http.Dependencies.IDependencyResolver resolver) + { + throw new NotSupportedException("WebApi IDependencyResolver is automatically registered from Services."); + } + } + } +}