Skip to content

Commit

Permalink
Merge pull request #86 from twsouthwick/service-provider
Browse files Browse the repository at this point in the history
Use dependency injection for RequestProviders
  • Loading branch information
pkgw authored Sep 17, 2020
2 parents 42378af + e0e2f43 commit 66d14e2
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 5 deletions.
15 changes: 15 additions & 0 deletions WWTMVC5/App_Start/UnityConfig.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Linq;
using Microsoft.Practices.Unity;
using WWT.Providers;

namespace WWTMVC5
{
Expand Down Expand Up @@ -31,11 +33,24 @@ public static IUnityContainer GetConfiguredContainer()
/// change the defaults), as Unity allows resolving a concrete type even if it was not previously registered.</remarks>
public static void RegisterTypes(IUnityContainer container)
{
RegisterRequestProviders(container);

// NOTE: To load from web.config uncomment the line below. Make sure to add a Microsoft.Practices.Unity.Configuration to the using statements.
// container.LoadConfiguration();

// TODO: Register your types here
// container.RegisterType<IProductRepository, ProductRepository>();
}

private static void RegisterRequestProviders(IUnityContainer container)
{
var types = typeof(RequestProvider).Assembly.GetTypes()
.Where(t => !t.IsAbstract && typeof(RequestProvider).IsAssignableFrom(t));

foreach (var type in types)
{
container.RegisterType(type);
}
}
}
}
21 changes: 19 additions & 2 deletions WWTMVC5/App_Start/UnityMvcActivator.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Linq;
using System.Web.Mvc;
using Microsoft.Practices.Unity.Mvc;
using WWT.Providers;
using WWTMVC5;

[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(UnityWebActivator), "Start")]
Expand All @@ -12,14 +14,17 @@ namespace WWTMVC5
public static class UnityWebActivator
{
/// <summary>Integrates Unity when the application starts.</summary>
public static void Start()
public static void Start()
{
var container = UnityConfig.GetConfiguredContainer();

FilterProviders.Providers.Remove(FilterProviders.Providers.OfType<FilterAttributeFilterProvider>().First());
FilterProviders.Providers.Add(new UnityFilterAttributeFilterProvider(container));

DependencyResolver.SetResolver(new UnityDependencyResolver(container));
var provider = new UnityDependencyResolver(container);

DependencyResolver.SetResolver(provider);
RequestProvider.SetServiceProvider(new DependencyResolverServiceProvider(provider));

// TODO: Uncomment if you want to use PerRequestLifetimeManager
// Microsoft.Web.Infrastructure.DynamicModuleHelper.DynamicModuleUtility.RegisterModule(typeof(UnityPerRequestHttpModule));
Expand All @@ -31,5 +36,17 @@ public static void Shutdown()
var container = UnityConfig.GetConfiguredContainer();
container.Dispose();
}

private class DependencyResolverServiceProvider : IServiceProvider
{
private readonly IDependencyResolver _resolver;

public DependencyResolverServiceProvider(IDependencyResolver resolver)
{
_resolver = resolver;
}

public object GetService(Type serviceType) => _resolver.GetService(serviceType);
}
}
}
12 changes: 9 additions & 3 deletions src/WWT.Providers/RequestProvider.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
using System.Web.UI;
using System;
using System.Web.UI;

namespace WWT.Providers
{
public abstract class RequestProvider
{
private static IServiceProvider _provider;

public void Run(Page page)
=> Run(new WwtContext(page));

public abstract void Run(WwtContext context);

public static RequestProvider Get<TProvider>()
where TProvider : RequestProvider, new()
=> new TProvider();
where TProvider : RequestProvider
=> (RequestProvider)_provider.GetService(typeof(TProvider));

public static void SetServiceProvider(IServiceProvider provider)
=> _provider = provider;
}
}

0 comments on commit 66d14e2

Please sign in to comment.