From 044acfc13e63ff08a1448c8c1d0e5ba64b3d6e93 Mon Sep 17 00:00:00 2001 From: Jason Zhu Date: Thu, 19 Aug 2021 16:43:04 +1000 Subject: [PATCH] 7.1.3 Setting Up the DI Controller --- SportsStore/SportsStore.WebUI/Global.asax.cs | 5 +-- .../NinjectControllerFactory.cs | 32 +++++++++++++++++++ .../SportsStore.WebUI.csproj | 1 + SportsStore/SportsStore.sln | 24 ++++++++++++++ 4 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 SportsStore/SportsStore.WebUI/Infrastructure/NinjectControllerFactory.cs diff --git a/SportsStore/SportsStore.WebUI/Global.asax.cs b/SportsStore/SportsStore.WebUI/Global.asax.cs index c66e2a1..207dd93 100644 --- a/SportsStore/SportsStore.WebUI/Global.asax.cs +++ b/SportsStore/SportsStore.WebUI/Global.asax.cs @@ -1,4 +1,5 @@ -using System; +using SportsStore.WebUI.Infrastructure; +using System; using System.Collections.Generic; using System.Linq; using System.Web; @@ -15,7 +16,7 @@ namespace SportsStore.WebUI AreaRegistration.RegisterAllAreas(); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); - BundleConfig.RegisterBundles(BundleTable.Bundles); + ControllerBuilder.Current.SetControllerFactory(new NinjectControllerFactory()); } } } diff --git a/SportsStore/SportsStore.WebUI/Infrastructure/NinjectControllerFactory.cs b/SportsStore/SportsStore.WebUI/Infrastructure/NinjectControllerFactory.cs new file mode 100644 index 0000000..9a03ced --- /dev/null +++ b/SportsStore/SportsStore.WebUI/Infrastructure/NinjectControllerFactory.cs @@ -0,0 +1,32 @@ +using Ninject; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Web; +using System.Web.Mvc; +using System.Web.Routing; + +namespace SportsStore.WebUI.Infrastructure +{ + public class NinjectControllerFactory : DefaultControllerFactory + { + private IKernel ninjectKernel; + + public NinjectControllerFactory() + { + ninjectKernel = new StandardKernel(); + AddBindings(); + } + + protected override IController GetControllerInstance(RequestContext requestContext, + Type controllerType) + { + return controllerType == null ? null : (IController)ninjectKernel.Get(controllerType); + } + + private void AddBindings() + { + // put additional bindings here + } + } +} \ No newline at end of file diff --git a/SportsStore/SportsStore.WebUI/SportsStore.WebUI.csproj b/SportsStore/SportsStore.WebUI/SportsStore.WebUI.csproj index 665bf58..d50ea15 100644 --- a/SportsStore/SportsStore.WebUI/SportsStore.WebUI.csproj +++ b/SportsStore/SportsStore.WebUI/SportsStore.WebUI.csproj @@ -139,6 +139,7 @@ Global.asax + diff --git a/SportsStore/SportsStore.sln b/SportsStore/SportsStore.sln index 9c62800..7edd282 100644 --- a/SportsStore/SportsStore.sln +++ b/SportsStore/SportsStore.sln @@ -3,7 +3,31 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 16 VisualStudioVersion = 16.0.31410.357 MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SportsStore.Domain", "SportsStore.Domain\SportsStore.Domain.csproj", "{CFEDEBCD-DE26-4A14-8CF2-95F735B1BB12}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SportsStore.UnitTests", "SportsStore.UnitTests\SportsStore.UnitTests.csproj", "{52446ADB-3D85-43F0-91E8-512BED0C0FA2}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SportsStore.WebUI", "SportsStore.WebUI\SportsStore.WebUI.csproj", "{D823EC39-331C-4734-92E5-325A37A682CA}" +EndProject Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {CFEDEBCD-DE26-4A14-8CF2-95F735B1BB12}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {CFEDEBCD-DE26-4A14-8CF2-95F735B1BB12}.Debug|Any CPU.Build.0 = Debug|Any CPU + {CFEDEBCD-DE26-4A14-8CF2-95F735B1BB12}.Release|Any CPU.ActiveCfg = Release|Any CPU + {CFEDEBCD-DE26-4A14-8CF2-95F735B1BB12}.Release|Any CPU.Build.0 = Release|Any CPU + {52446ADB-3D85-43F0-91E8-512BED0C0FA2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {52446ADB-3D85-43F0-91E8-512BED0C0FA2}.Debug|Any CPU.Build.0 = Debug|Any CPU + {52446ADB-3D85-43F0-91E8-512BED0C0FA2}.Release|Any CPU.ActiveCfg = Release|Any CPU + {52446ADB-3D85-43F0-91E8-512BED0C0FA2}.Release|Any CPU.Build.0 = Release|Any CPU + {D823EC39-331C-4734-92E5-325A37A682CA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D823EC39-331C-4734-92E5-325A37A682CA}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D823EC39-331C-4734-92E5-325A37A682CA}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D823EC39-331C-4734-92E5-325A37A682CA}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection