7.1.3 Setting Up the DI Controller

chap07
Jason Zhu 2021-08-19 16:43:04 +10:00
parent 2c80c21441
commit 044acfc13e
4 changed files with 60 additions and 2 deletions

View File

@ -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());
}
}
}

View File

@ -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
}
}
}

View File

@ -139,6 +139,7 @@
<Compile Include="Global.asax.cs">
<DependentUpon>Global.asax</DependentUpon>
</Compile>
<Compile Include="Infrastructure\NinjectControllerFactory.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>

View File

@ -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